I am trying to use Powershell SOAP to create a ticket in ServiceNow. I have valid XML that I have successfully tested with the SoapUI tool using basic auth. But when I try to post that same XML to ServiceNow via Powershell I get an error. I'm not sure if its a problem with the auth header (same creds in both SoapUI and PS) or something else because I'm getting a 500 error from the post, which doesn't sound auth related.

Using PS v4. Long time powershell user. First time doing SOAP and scripted interaction with ServiceNow. Their wiki's have no powershell examples :(
Thanks

$uri = "https://fubar.service-now.com/u_smarts_notification.do?SOAP"
$username = 'looseLips'
$password = 'sinkShips'

$xml = [xml]@"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://www.service-now.com/u_smarts_notification">
   <soapenv:Header/>
   <soapenv:Body>
      <u:insert>
         <u_assignment_group>Some Group</u_assignment_group>
         <u_impact>4</u_impact>
         <u_urgency>3</u_urgency>
         <u_op_tier_1>Malfunction</u_op_tier_1>
         <u_op_tier_2>Error</u_op_tier_2>
         <u_op_tier_3>Break Fix</u_op_tier_3>
         <u_short_description>Subject</u_short_description>
         <u_work_notes>Body</u_work_notes>
      </u:insert>
   </soapenv:Body>
</soapenv:Envelope>
"@

$header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password))}
$post = Invoke-WebRequest -Uri $uri -Headers $header -Method Post -Body $xml



Invoke-WebRequest : The remote server returned an error: (500) Internal Server
Error.
At R:\ps1\serviceNowINC.ps1:30 char:9
+ $post = Invoke-WebRequest -Uri $uri -Headers $header -Method Post -Body $xml
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:Htt
   pWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
   ll.Commands.InvokeWebRequestCommand
有帮助吗?

解决方案

Try setting content-type like

$post = Invoke-WebRequest -Uri $uri -Headers $header -Method Post -Body $xml -ContentType "application/xml"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top