PowerShell版本2 Invoke-WebRequest

时间:2015-05-13 15:51:15

标签: powershell powershell-v2.0 powershell-v3.0 invoke

我需要运行一个Powershell脚本,其中包含以下语法。相对于这一行的PowerShell 2是什么?

Invoke-WebRequest "http://$Server/api/setobjectproperty.htm?id=$Id&name=Inheritscheduledependency&value=0&username=$User&passhash=$Hash" | out-null

这是完整的脚本:

param([int]$Id = 5557,[int]$Duration = 1,[string]$Unit = "Hours")

$Server     = "webpage.bla.org"
$User       = "user"
$Hash       = "45093450908"
$DateFormat     = "yyyy-MM-dd-HH-mm-ss";
$StartDate  = (Get-Date).ToString($DateFormat);

switch($Unit){
"Minutes"   { $EndDate = (Get-Date).AddMinutes($Duration).ToString($DateFormat); }
"Hours"     { $EndDate = (Get-Date).AddHours($Duration).ToString($DateFormat); }
"Days"      { $EndDate = (Get-Date).AddDays($Duration).ToString($DateFormat); }
default     { $EndDate = (Get-Date).AddHours($Duration).ToString($DateFormat); }
}

Invoke-WebRequest "http://$Server/api/setobjectproperty.htm?id=$Id&name=Inheritscheduledependency&value=0&username=$User&passhash=$Hash" | out-null
Invoke-WebRequest "http://$Server/api/setobjectproperty.htm?id=$Id&name=maintenable&value=1&username=$User&passhash=$Hash" | out-null
Invoke-WebRequest "http://$Server/api/setobjectproperty.htm?id=$Id&name=maintstart&value=$StartDate&username=$User&passhash=$Hash" | out-null
Invoke-WebRequest "http://$Server/api/setobjectproperty.htm?id=$Id&name=maintend&value=$EndDate&username=$User&passhash=$Hash" | out-null

由于

1 个答案:

答案 0 :(得分:15)

在Powershell 2中,你可以使用.NET WebClient类,因为@KIM Taegyoon在一个关于PowerShell和webrequests的旧问题中指出了这一点。见他的回答here

简而言之:

(New-Object System.Net.WebClient).DownloadString("http://stackoverflow.com")