简体   繁体   English

如何使用 PowerShell 通过 REST API 将 CSV 文件附加到 Service Now 事件?

[英]How to attach CSV file to Service Now incident via REST API using PowerShell?

I need to attach the file either xlsx or CSV to a particular incident via SNOW REST API using PowerShell script.我需要使用 PowerShell 脚本通过 SNOW REST API 将 xlsx 或 CSV 文件附加到特定事件。 I have tried with the below code:我已尝试使用以下代码:

if (!$script:ServiceNowCreds) {
    $script:ServiceNowCreds = Get-Credential
}
$snow_url = 'https://dev652xx.service-now.com/api/now/table/incident'
$Body = @{
    'number' = 'INC00xx059'    
}
$result = Invoke-RestMethod -Uri  $snow_url -Credential $script:ServiceNowCreds -Body $Body -ContentType "application/json" 
$result.result | select sys_id, number | ForEach-Object {
    $Upload_snow_url ='https://dev652xx.servicenow.com/api/now/attachment/upload'
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add('Content-Type','text/csv')
    $headers.Add('Accept','*/*')
    $sys_id = $_.sys_id
    $incident_number = $_.number
    $UploadBody = @{
        'table_name'='incident'; 
        'table_sys_id'=$sys_id;
        'file_name' = 'C:\Users\suganthanraj.p\Documents\Servers.csv'
    }
    $uploadParam = $UploadBody | ConvertTo-JSon
    Write-Host $sys_id
    Write-Host $incident_number
    $UploadResult = Invoke-RestMethod -Uri $Upload_snow_url -Credential $script:ServiceNowCreds -Body $uploadParam -Method Post -Headers $headers
    $UploadResult
}

When I execute the above script I am getting the below error:当我执行上述脚本时,出现以下错误:

Invoke-RestMethod : The remote server returned an error: (415) Unsupported 
Media Type.
At C:\Users\suganthanraj.p\Desktop\SNOW-UploadAttachment.ps1:39 char:21
+ ... oadResult = Invoke-RestMethod -Uri  $Upload_snow_url -Credential $scr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Try changing you content type to "multipart/form-data"尝试将您的内容类型更改为“multipart/form-data”

$headers.Add('Content-Type','multipart/form-data')

$UploadBody = @{
    'table_name'='incident'; 
    'record_sys_id'=$sys_id;
    'uploadFile' = 'C:\Users\suganthanraj.p\Documents\Servers.csv'
}

The error says "The remote server returned an error: (415) Unsupported Media Type."错误显示“远程服务器返回错误:(415) 不支持的媒体类型。”

Doco on the api can be found here:可以在此处找到 api 上的 Doco:

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/integrate/inbound_rest/reference/r_AttachmentAPI-POSTmultipart.html https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/integrate/inbound_rest/reference/r_AttachmentAPI-POSTmultipart.html

Your best option would be leverage the OOB Attachment API in ServiceNow.您最好的选择是利用 ServiceNow 中的 OOB 附件 API You will need to make a post call from powershell.您将需要从 powershell 进行后期调用。 Powershell has two options for this Invoke-RestMethod and Invoke-WebRequest . Powershell 对此Invoke-RestMethodInvoke-WebRequest有两个选项。 I have had better luck with the latter when trying to POST.尝试发布时,我对后者的运气更好。 You might also first build your rest call in Postman make sure you can get the attachment into ServiceNow, then worry about writing your PS.您也可以先在Postman 中构建您的 rest 调用,确保您可以将附件放入 ServiceNow,然后再担心编写您的 PS。

$Body = @{
User = 'jdoe'
password = 'P@S$w0rd!'
}
$LoginResponse = Invoke-WebRequest 'http://www.contoso.com/login/' - SessionVariable 'Session' -Body $Body -Method 'POST'

$Session
$ProfileResponse = Invoke-WebRequest 'http://www.contoso.com/profile/' -`WebSession $Session $ProfileResponse`

Finally i found answer from the below link最后我从下面的链接中找到了答案

https://community.servicenow.com/community?id=community_question&sys_id=d3707023dbaceb8023f4a345ca961949 and below is the code: https://community.servicenow.com/community?id=community_question&sys_id=d3707023dbaceb8023f4a345ca961949以下是代码:

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

 # 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://dev652XX.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=850XXXXX2200e0ef563dbb9a71c1&file_name=TreeSizeReport.csv"

# Specifiy file to attach
$fileToAttach = "C:\Users\suganthanraj.p\Desktop\TreeSizeReport.csv"

# Specify HTTP method (POST, PATCH, PUT)
$method = "POST"

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

# Print response
$response.RawContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 立即服务Geneva Rest API Java代码将文件附加到事件 - Service Now Geneva Rest API Java Code To attach file to incident Service Now现在通过rest api为事件添加标签 - Service Now add tag to incident through rest api 通过 Rest API 发送 CSV 文件 - Send CSV file via Rest API 使用 Python/Rest 将附件添加到 BMC Remedy 事件 API - Add an Attachment to BMC Remedy Incident using Python/Rest API 立即服务集成 - 如何使用 API/TableAPI 使用 HTTP 方法创建出站 REST 消息 - Service Now Integration - How to create Outbound REST message with HTTP method by using API/TableAPI 如何通过REST API现在获取与服务中的用户名关联的组名 - How to get the group name associated with username in service now by REST API 如何使用rest api将数据集作为csv文件导入到power bi? - How to import datasets as csv file to power bi using rest api? 如何使用 REST ZDB974238714CA8DE144A7CE1DZF083 在 Django 中上传 CSV 文件? - How to upload a CSV file in Django using the REST API? 如何使用SharePoint 2013中的REST API将附件文件附加到列表项 - How to attach a attachment file to a list item using REST API in SharePoint 2013 如何使用 Devops REST API 搜索带有 Powershell 的 Azure Devops 代码存储库并输出到 CSV - How to search Azure Devops Code Repos w/ Powershell using Devops REST API and output to CSV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM