小编flo*_*oyd的帖子

Powershell v3 Invoke-WebRequest HTTPS错误

使用Powershell v3的Invoke-WebRequest和Invoke-RestMethod我已成功使用POST方法将json文件发布到https网站.

我正在使用的命令是

 $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt")
 Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Body $json -ContentType application/json -Method POST
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用GET方法时:

 Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET
Run Code Online (Sandbox Code Playgroud)

返回以下错误

 Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
 At line:8 char:11
 + $output = Invoke-RestMethod -Uri https://IPADDRESS/resource -Credential $cred
 +           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest)      [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用以下代码来忽略SSL证书,但我不确定它是否真的在做任何事情.

 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Run Code Online (Sandbox Code Playgroud)

有人可以提供一些指导,说明这里可能出现的问题以及解决方法吗?

谢谢

.net rest powershell https

113
推荐指数
7
解决办法
18万
查看次数

Powershell v3 Invoke-RestMethod

我试图利用新的invoke-restmethod cmdlet来POST一个JSON文件并成功完成.但是,我没有像使用CURL时那样收到网络服务器的回复.对于我想要完成的任务,我需要从reposne获取信息到POST并将其用于另一个POST命令.

有人可以解释我如何从服务器获得预期的响应?下面是使用Invoke-RestMethod的两个命令1st in CURL,2nd命令.curl命令将执行正确的POST并返回响应.Powershell命令将执行正确的POST但不会返回响应.

谢谢

编辑:我认为我试图从ps输出获取的主要内容是"响应头"即.curl命令下面的输出

 < HTTP/1.1 201 Created
 < Date: Thu, 26 Jul 2012 01:20:06 GMT
 < Server: Apache
 < X-EM7-Implemented-methods: GET,PUT,POST
 < X-Powered-By: ScienceLogic,LLC - EM7 API/Integration Server
 < Location: /ticket/321750
 < X-EM7-status-message: ticket /ticket/321750 added.
 < X-EM7-status-code: CREATED
 < Content-Length: 830
 < Content-Type: application/json
 < 
Run Code Online (Sandbox Code Playgroud)

卷曲命令

 curl -f -v -s -k --no-sessionid -H X-em7-beautify-response:1 -H content-  type:application/json https://URLHERE --data-binary  @jsonfile.json
Run Code Online (Sandbox Code Playgroud)

Powershell代码

 $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("certfile.crt")
 $json = Get-Content jsonfile.json
 $cred = Get-Credential -Message "Enter Credentials"

 Invoke-RestMethod …
Run Code Online (Sandbox Code Playgroud)

rest powershell curl

8
推荐指数
1
解决办法
1万
查看次数

Powershell v3 Invoke-RestMethod标题

我正在尝试对服务进行RestAPI调用,该服务在其文档中指定以下内容:

Integration Server可以以XML和JSON格式进行响应.在请求中使用以下接受标头之一:

  1. 接受:application/json,/.
  2. 接受:application/xml,/

如果accept标头不包含application/xml,application/json或/,则集成服务器将使用"406方法不可接受"状态代码进行响应.

我的powershell代码看起来像这样 Invoke-RestMethod -URI https://URL/ticket -Credential $cred -Method Get -Headers @{"Accept"="application/xml"}

但是我收到与标题有关的以下错误: Invoke-RestMethod : This header must be modified using the appropriate property or method. Parameter name: name

有人可以帮助我理解为什么powershell不会让我指定Accept标头?或者我在这里缺少另一种方法吗?

谢谢

rest powershell-3.0

7
推荐指数
2
解决办法
2万
查看次数

图谱API的MSI权限

我的问题是,我们是否有任何记录方法向Graph API授予管理服务标识权限,就像在门户网站中使用Azure App注册一样?我无法在Azure门户或文档中找到任何Powershell选项或管理MSI服务主体权限的能力.我在MSDN论坛上发现了一个类似的问题,但是想确保没有任何人知道的更新或变通方法?

MSDN论坛帖子:https://social.msdn.microsoft.com/Forums/azure/en-US/dae34534-f193-4444-b52e-ba9cfa4a1fda/does-azure-msi-support-accessing-graph-api? forum = WindowsAzureAD

azure-active-directory microsoft-graph azure-managed-identity

4
推荐指数
1
解决办法
1878
查看次数

Powershell HTTPS REST api GET/POST

我试图测试一个非常简单的PowerShell脚本来从内部票务站点获取/发布数据.我遇到的问题似乎与所需的SSL证书有关.有人可以帮我理解我需要添加哪些代码才能使其工作?

谢谢

返回错误:使用"0"参数调用"GetResponse"的异常:"底层连接已关闭:无法为SSL/TLS安全通道建立信任关系."

现行代码:

 $url = "https://username:password@IPADDRESS/ticket"
 $command = get-content jsonfile.json

 $bytes = [System.Text.Encoding]::ASCII.GetBytes($command)
 $web = [System.Net.WebRequest]::Create($url)
 $web.Method = "POST"
 $web.ContentLength = $bytes.Length
 $web.ContentType = "application/json"
 $stream = $web.GetRequestStream()
 $stream.Write($bytes,0,$bytes.Length)
 $stream.close()

 $reader = New-Object System.IO.Streamreader -ArgumentList
 $web.GetResponse().GetResponseStream()
 $reader.ReadToEnd()
 $reader.Close()
Run Code Online (Sandbox Code Playgroud)

.net rest powershell https

3
推荐指数
1
解决办法
4897
查看次数