簡體   English   中英

使用Invoke-WebRequest通過Powershell將數據發布到ServiceNow

[英]POSTing data to ServiceNow via Powershell using Invoke-WebRequest

我正在嘗試通過Powershell腳本將信息發布到ServiceNow中的表中。 當我運行它時,我得到一個錯誤

Invoke-WebRequest:遠程服務器返回錯誤:(500)Internal Server Error。

有人可以幫我找出解決方法嗎? 謝謝大家。

$userName = 'helpMe'
$password = 'iAmStuck' | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($userName, $password)
$uri = 'stuff'
$postParams = "test"
#[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Invoke-WebRequest -Uri $uri -Method Post -Body $postParams -Credential $cred 

ServiceNow有一個REST API資源管理器,其中包含各種代碼示例以供您開始使用。

下面是一個示例,我將其匯總在一起,並使用管理員帳戶發布到事件表中。 這里有兩個重要的因素,用戶必須具有角色(此處為信息https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/integrate/inbound-rest/reference/r_RESTAPIRoles.html )才能使用API並且必須有權訪問要發布到的表。 另外,請注意,帖子的主體需要為RAW JSON,並且URL中提供了所有正確的標題數據。 如果成功,ServiceNow將返回有關帖子的JSON數據。

# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "noPassword"

# 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://xxxxx.service-now.com/api/now/table/incident"

# Specify HTTP method
$method = "post"

# Specify request body
{request.body ? "$body = \"" :""}}{\"active\":\"true\",\"number\":\"123\",\"short_description\":\"test\"}"

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $body

# Print response
$response.RawContent

即使您發布了代碼,也沒有發布與該問題相關的任何內容。 無論ServiceNow是什么,都可能有一個API供其參考。 與基於Web的API交互時,API通常需要一種結構才能理解您在$ postParams中提供的數據。 在平坦的HTTP POST情況下,有時它可能只是鍵->值對,但是對於RESTful API,通常您需要構造API文檔定義的JSON標頭。

如果您搜索“ servicenow powershell交互”,則似乎有一個GitHub項目通過PowerShell與ServiceNow進行交互,並且還有一個專門涵蓋此主題的PDF。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM