$url = $args[0]
$serverName = 'remotepc-01'
Invoke-Command -ComputerName $serverName -ScriptBlock {
Write-Host "URL is: $url"
function Get-WebStatus($url) {
try {
[Net.HttpWebRequest] $req = [Net.WebRequest]::Create($url)
$req.Method = "HEAD"
[Net.HttpWebResponse] $res = $req.GetResponse()
if ($res.StatusCode -eq "200") {
Write-Host "`nSite $url is up (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n"
} else {
Write-Host "`nSite $url is down (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n"
}
} catch {
Write-Host "`nSite $url is down (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n" -ForegroundColor Red
}
}
Get-WebStatus $url
}
当我使用以下命令调用脚本时:
./Get-Url http://localhost
它总是返回
网站已关闭(返回码:0)
似乎函数中丢失了参数https://google.com(空白)。如果我在函数之前添加Write-Host $url
,它将显示HTTP地址,但在函数内部为空白。
我只是想检查远程计算机上的http:// localhost:8080网页是打开还是关闭。
理想情况下,我实际上想传递两个参数:1)remotePC和2)URL地址;因此,如果我在PowerShell(v5)中运行:
./Get-Url remotePC http://localhost:8080
它会告诉我remotePC上http:// localhost:8080的页面是否出现。