PowerShell Webrequest POST for PowerShell 2.0



如何在Windows 7上修改PowerShell版本2.0的此代码片段代码?

$path = "myfolder/file.txt"
$body = "file=$(Get-Content $path | Out-String)"
Invoke-WebRequest -Uri "http//mywebsite" -Method POST -Body $body`

我试过这个,但无法在PowerShell版本2.0上运行。有什么建议吗?

Invoke-webRequest 存在于 PowerShell v3.0 中 [ref - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6]

有两种

解决方案可以解决问题,一种可以将powershell版本更新到3以上,否则使用以下 cmdlet 执行 WebRequest。

$WebRequest = [System.Net.WebRequest]::Create("http://url")
$WebRequest.Method = "GET"
$WebRequest.ContentType = "application/json"
$Response = $WebRequest.GetResponse()
$ResponseStream = $Response.GetResponseStream()
$ReadStream = New-Object System.IO.StreamReader $ResponseStream
$Data=$ReadStream.ReadToEnd()

最新更新