简体   繁体   English

Invoke-RestMethod在ServiceNow中上传CSV

[英]Invoke-RestMethod uploading CSV in ServiceNow

I tried many ways of uploading a CSV file to ServiceNow using PowerShell via Invoke-RestMethod and Invoke-WebRequest; 我尝试了多种通过PowerShell通过Invoke-RestMethod和Invoke-WebRequest将CSV文件上传到ServiceNow的方法; however, I have hit a wall. 但是,我碰壁了。 When I call the functions, I receive the following error: 调用函数时,出现以下错误:

"Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed." “ Invoke-RestMethod:远程服务器返回错误:(405)不允许使用方法。”

"Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed." “ Invoke-WebRequest:远程服务器返回错误:(405)不允许的方法。”

I have tried the code below: 我试过下面的代码:

Attempt 1) 尝试1)

$Headers = @{'Auth_token'=$envCred};
$FileContent = [IO.File]::ReadAllText('C:\temp\test.csv');
$Fields = @{'uploadFile'=$FileContent};

Invoke-WebRequest -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Headers $Headers -Body $Fields;

Attempt 2) 尝试2)

Invoke-RestMethod -Method Post -Uri $uri -Credential $envCred -ContentType 'multipart/form-data -InFile "C:\temp\test.csv" ' 

I know for a fact that the API is working, because after I call: Invoke-RestMethod -Method 'get' -Uri $uri -Credential $snCred -body $body it returns the proper information. 我知道API可以正常工作的事实,因为在调用之后: Invoke-RestMethod -Method 'get' -Uri $uri -Credential $snCred -body $body它会返回正确的信息。

I also tried the [-Method Patch] with the following: "Invoke-RestMethod -Uri $uri -Credential $snCred -Method Patch -ContentType 'text/csv' -InFile "C:\\temp\\test.csv" - Also tried with the -ContentType 'multipart/form-data ' - I get the following error: "Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type." 我还尝试使用以下命令进行[-Method Patch]: "Invoke-RestMethod -Uri $uri -Credential $snCred -Method Patch -ContentType 'text/csv' -InFile "C:\\temp\\test.csv" -也尝试过使用-ContentType 'multipart/form-data data'-我收到以下错误:“ Invoke-RestMethod:远程服务器返回错误:(415)不支持的媒体类型。”

Is there another way or uploading CSV file(s) in PowerShell using the Invoke-RestMethod or Invoke-WebRequest ? 是否有其他方法或使用Invoke-RestMethodInvoke-WebRequest在PowerShell中上载CSV文件?

Solution: I realized that I had a typo with my URI, after fixing it, it worked! 解决方案:我意识到我的URI上有一个错字,修复它后,它就可以了! Sorry for the inconvenience. 抱歉给你带来不便。

It's unclear what endpoint you were using, however, here's an example from the docs 尚不清楚您使用的是哪个端点,但是,这是文档中的一个示例

curl "https://instance.service-now.com/api/now/attachment/upload" \
--request POST \
--header "Accept:application/json"
--user 'admin':'admin'"\
--header "Content-Type:multipart/form-data"
-F 'table_name=incident' -F 'table_sys_id=d71f7935c0a8016700802b64c67c11c6' -F 'encryption_context=undefined'-F 'uploadFile=@ location of the file on file system'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM