这是一个用于检查我的URL状态的脚本,无论它是否正常工作。我需要编写一个脚本,使其接受文本文件中存在的所有URL,这些URL放在本地驱动器中并显示其信息。
[string] $url = 'http://mywebsite.net'
function CheckForStatus($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" -ForegroundColor green
}
else {
write-host "`nSite $url is not available (Return code:
$($res.StatusCode) - $([int] $res.StatusCode))`n" -ForegroundColor red
}
} catch {
write-host "`nSite $url is having some DNS issues`n" -ForegroundColor
red
}
}
CheckForStatus $url
答案 0 :(得分:3)
使用Get-Content
cmdlet并将其管道以处理每一行。
Get-Content "some_file" | % { CheckForStatus($_) }