【问题标题】:PowerShell Version 2 Invoke-WebRequestPowerShell 版本 2 调用-WebRequest
【发布时间】:2015-05-13 15:51:14
【问题描述】:

我需要运行一个包含以下语法的 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 powershell-2.0 powershell-3.0 invoke


【解决方案1】:

在 Powershell 2 中,您可以使用 .NET WebClient 类,正如 @KIM Taegyoon 在关于 PowerShell 和 webrequests 的较早问题中指出的那样。看他的回答here

简而言之:

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

【讨论】:

    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多