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

Hi Anthony,



Thank you very much for your help. It worked when I change the method to patch. I was able to change field status from Installed to Retired.


I have not tried to perform the second code you provided to use the method   POST to add a new record into table. I will let you know when I test that one.


Just one thing, It is working for me when i give the user 'admin' role. But when I just give 'rest_service' and 'web_service_admin' role, it doesn't have permission to do update the table. I don't want the user to give full admin permission.


Is there anything else I can do to avoid giving full permission?



Thanks once again for your help.



Regards


Zubair


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Zubair,


Thanks for updating with results and indeed great news it is working for you.


PUT and PATCH should both work OK.



ServiceNow KB: Table API FAQs (KB0534905)


11.


What is the difference between PUT and PATCH?


In the REST world, PUT and PATCH have different semantics. PUT means replace the entire resource with given data (so null out fields if they are not provided in the request), while PATCH means replace only specified fields.   For the Table API, however, PUT and PATCH mean the same thing.   PUT and PATCH modify only the fields specified in the request.



With regards to your inquiry about avoiding giving the user admin role, the rest user should have sufficient privileges be able to carry out the required crud operations on the target tables as an interactive ie logged in user - plus privileges to update via REST.


ACLs could be part of the solution.


I would suggest creating a new Community post to get some specific info.



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





Line that reads

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

Does not appear to be a valid Powershell statement

Is this correct?