[英]PowerShell Version 2 Invoke-WebRequest
我需要運行一個包含以下語法的 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
謝謝
在 Powershell 2 中,您可以使用 .NET WebClient 類,正如@KIM Taegyoon 在有關 PowerShell 和 webrequests 的較舊問題中指出的那樣。 在這里看到他的回答
簡而言之:
(New-Object System.Net.WebClient).DownloadString("http://stackoverflow.com")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.