Powershell script RestAPI

ragz
Tera Expert

Hi ,

Using powershell scripts I need to insert data into INCIDENT table using REST API .

Please let me know how to achieve this ?

9 REPLIES 9

Antanis
Tera Contributor

Hi try this:



$SNowUser = "USERNAME"


$SNowPass = ConvertTo-SecureString —String "PASSWORD"   —AsPlainText -Force


$SNowCreds = New-Object —TypeName System.Management.Automation.PSCredential —ArgumentList $SNowUser, $SNowPass


$headers = @{ Accept = "application/xml" }


$uri "https://YOR-LINK.servicenow.com/api/YOURS_API"


$method   = "POST"   # to Create Incident use POST , to update use PATCH


$body = @{     #Create body of the POST request


description= "Simple API Test"


}


$bodyJson = $body | ConvertTo-Json


Invoke-RestMethod -Credential $SNowCreds -Headers $headers -Method $method -Uri $uri   -Body   $bodyJson -ContentType "application/json"


$response.result


Hi ,



Thanks for your response .


I am able POST (add) the entry but I am unable to update the same with the web_service_admin, rest_api_explorer roles .


Only with Admin role I am able to update (PUT or PATCH) .


May I know how to resolve this ?


Dave Smith1
ServiceNow Employee
ServiceNow Employee

Only with Admin role I am able to update (PUT or PATCH) .


That's incorrect - check the ACLs for table accessibility.   You'll find there are a number of roles that can perform updates (e.g.: itil role).



Your best bet is to create a separate role that has write access to this table and confer that to the account being used for the API call.


Hi Dave ,



Current I have default permissions . I did not make any changes to the ACLs.



I tried with all the roles like   web_service_admin, rest_api_explorer and ITIL   but while updating I am getting 403 Forbidden error .



I believe I am having write access to the table as I am able to POST (add) the entry in incident table but only problem with updating the record .




find_real_file.png


find_real_file.png