Powershell command to update cmdb_ci_win_server table's record using API.

zoobear
Kilo Contributor

Hi,

I am trying to write a script to update the cmdb table which will change the status field from 'Installed' to 'Retired'. Also to add a new server into the table.

I am looking for a powershell command which can be called from my powershell script. The table that I want to update is cmdb_ci_win_server.

I was trying to use this command to update the status but getting error. If anyone can help me to do this, that would be really helpful.

$data = @{  

install_status = "Retired"

}

$dataJson = $data | ConvertTo-Json

$URI = 'https://instance.service-now.com/api/now/table/cmdb_ci_win_server/287cd51a9c1a1000b0a817541dc9889e'

Invoke-RestMethod -Uri $URI -Headers $headers -Proxy http://proxy:port -ProxyUseDefaultCredentials -Method POST -Body $dataJson -ContentType "application/json"

here '287cd51a9c1a1000b0a817541dc9889e' is sys_id.

I am getting following error.

Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed.

Thanks

Zubair

1 ACCEPTED SOLUTION

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Zubair,


You mention


"I am trying to write a script to update the cmdb table which will change the status field from 'Installed' to 'Retired'."


What you can do is to use the REST api explorer to patch - or put - install_status of the cmdb_ci record of the windows server.


Then generate the powershell code.


I just tried that and the install_status was indeed updated from Installed to Retired.


Here is the powershell generated the only thing that has been changed is the password and the instance name.



# Eg. User name="admin", Password="admin" for this code sample.


$user = "admin"


$pass = "admin"



# Build auth header


$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))



# Set proper headers


$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"


$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))


$headers.Add('Accept','application/json')


$headers.Add('Content-Type','application/json')



# Specify endpoint uri


$uri = "https://instance_name.service-now.com/api/now/table/cmdb_ci/27e3a47cc0a8000b001d28ab291fa65b"



# Specify HTTP method


$method = "patch"



# Specify request body


{request.body ? "$body = \"" :""}}{\"install_status\":\"Retired\"}"



# Send HTTP request


$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $body



# Print response


$response.RawContent



I will add some screenshots shortly.



If the reply was informational, please like, mark as helpful or mark as correct!


View solution in original post

7 REPLIES 7

gauravchoudhury
Tera Guru

Hi Zubair,



This is something I have come across that is not the exact requirement but it will help you prep for one.



#Used my baseline here: https://thwack.solarwinds.com/message/300039#300039


#


#Posted this script here: https://thwack.solarwinds.com/docs/DOC-188533


#Modified by Thwack user: Chad.Every Date: 2016/04/21


#



# Since the certificate in Orion for SWIS is self-signed we'll need this to ignore it.


add-type @"


  using System.Net;


  using System.Security.Cryptography.X509Certificates;


  public class TrustAllCertsPolicy : ICertificatePolicy {


  public bool CheckValidationResult(


  ServicePoint srvPoint, X509Certificate certificate,


  WebRequest request, int certificateProblem) {


  return true;


  }


  }


"@


[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy



$NodeID=$args[0]


$AppID=$args[1]



#Variables


$hostname = "10.199.21.25"


$username = "api"


$SecurePassword = ConvertTo-SecureString -AsPlainText -Force -String "Password1"


#$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString


$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $Securepassword


$swisuri = "swis://$($hostname)/Orion/Orion.Nodes/NodeID=$($NodeID)/Applications/ApplicationID=$($AppID)/CustomProperties"


$url = "https://$($hostname):17778/SolarWinds/InformationService/v3/Json/$swisuri"



# There is a Application Custom Property (Text) that i created called 'SendAlert'


$var="test text here"


$json = @{"SendAlert"="$var"}


$json = ConvertTo-Json $json



#Other examples


#$json = '{"SendAlert":"Custom text here"}'


#$json = '{"SendAlert": "False"}'


#$json = '{"SendAlert": "True"}'



Invoke-WebRequest -Uri $url -Credential $cred -Method Post -Body $json -ContentType application/json


exit 0



Source: Link


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Zubair,


You mention


"I am trying to write a script to update the cmdb table which will change the status field from 'Installed' to 'Retired'."


What you can do is to use the REST api explorer to patch - or put - install_status of the cmdb_ci record of the windows server.


Then generate the powershell code.


I just tried that and the install_status was indeed updated from Installed to Retired.


Here is the powershell generated the only thing that has been changed is the password and the instance name.



# Eg. User name="admin", Password="admin" for this code sample.


$user = "admin"


$pass = "admin"



# Build auth header


$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))



# Set proper headers


$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"


$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))


$headers.Add('Accept','application/json')


$headers.Add('Content-Type','application/json')



# Specify endpoint uri


$uri = "https://instance_name.service-now.com/api/now/table/cmdb_ci/27e3a47cc0a8000b001d28ab291fa65b"



# Specify HTTP method


$method = "patch"



# Specify request body


{request.body ? "$body = \"" :""}}{\"install_status\":\"Retired\"}"



# Send HTTP request


$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $body



# Print response


$response.RawContent



I will add some screenshots shortly.



If the reply was informational, please like, mark as helpful or mark as correct!


Screen Shot 2017-08-29 at 6.12.05 AM.pngScreen Shot 2017-08-29 at 6.12.25 AM.png



Screen Shot 2017-08-29 at 6.13.13 AM.png


You can also use the same approach to carry out   post to table cmdb_ci_win_server



# Eg. User name="admin", Password="admin" for this code sample.


$user = "admin"


$pass = "admin"



# Build auth header


$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))



# Set proper headers


$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"


$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))


$headers.Add('Accept','application/json')


$headers.Add('Content-Type','application/json')



# Specify endpoint uri


$uri = "https://instance_name.service-now.com/api/now/table/cmdb_ci_win_server"



# Specify HTTP method


$method = "post"



# Specify request body


{request.body ? "$body = \"" :""}}{\"name\":\"new windows server\"}"



# Send HTTP request


$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $body



# Print response


$response.RawContent



Screen Shot 2017-08-29 at 6.25.56 AM.png