问题
I want to take the http status of multiple URL at once to prepare a report. How to acheive it using powershell?
回答1:
I have seen questions on monitoring multiple websites through windows machine. My friend wanted to check status of 200 URLs which he used to do manually. I wrote a Power-Shell script to overcome this. Posting it for the benefit of all users.
Save the below code as "AnyName.ps1" file in "D:\MonitorWeb\"
#Place URL list file in the below path
$URLListFile = "D:\MonitorWeb\URLList.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
#For every URL in the list
Foreach($Uri in $URLList) {
    try{
        #For proxy systems
        [System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
        [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
        #Web request
        $req = [system.Net.WebRequest]::Create($uri)
        $res = $req.GetResponse()
    }catch {
        #Err handling
        $res = $_.Exception.Response
    }
    $req = $null
    #Getting HTTP status code
    $int = [int]$res.StatusCode
    #Writing on the screen
    Write-Host "$int - $uri"
    #Disposing response if available
    if($res){
        $res.Dispose()
    }
}
Place a file "URLList.txt" with list of URLs in the same directory "D:\MonitorWeb\"
e.g:
http://www.google.com
http://google.com
http://flexboxfroggy.com
http://www.lazyquestion.com/interview-questions-and-answer/es6
Now open normal command prompt and navigate to the "D:\MonitorWeb\" and type below command:
powershell -executionpolicy bypass -File .\AnyName.ps1
And you'll get output like below:
HTTP STATUS 200 = OK
Note:
This works in power-shell version 2 as well.
Works even if you are behind proxy (uses default proxy settings)
Hope this helps!
来源:https://stackoverflow.com/questions/52738532/how-to-check-status-of-list-of-websites-urls-using-power-shell-script