适用于PowerShell 2.0的PowerShell Webrequest POST

时间:2019-02-10 14:21:56

标签: post powershell-v2.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版上运行。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

PowerShell v3.0中存在Invoke-webRequest [ref-https://docs.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()