[英]Executing invoke-webrequest to get status of multple websites in Powershell
We have several websites and I don't want to check the status in powershell using invoke-webrequest -URI google.com
every time. 我们有几个网站,我不想每次都使用
invoke-webrequest -URI google.com
检查powershell中的状态。
I drafted something like this, which isn't working and I'd like help with: 我起草了类似这样的内容,但是它不起作用,我需要以下方面的帮助:
$URLS = get-content c:\websitesaddress.txt
Invoke-webrequest -URI $urls |statuscode | export-csv c:\status.csv
I would like to check the status of several webpages and export it as CSV in the format website name, statuscode
. 我想检查几个网页的状态,并以
website name, statuscode
的格式将其导出为CSV。
You're not far away from the answer, you just needed to get the statuscode property of the Invoke-WebRequest call, like so: 您离答案不远,您只需要获取Invoke-WebRequest调用的statuscode属性,如下所示:
Invoke-webrequest -URI $url | select statuscode
or 要么
$(Invoke-webrequest -URI $url).statuscode
And if that were to be piped into the Export-Csv
cmdlet you'd only get the statuscodes, and not the url, plus the file would need to be appended to on each loop, as it stands the file would be recreated each time, only displaying the statuscode for the very last url in the file. 而且,如果将其通过管道传输到
Export-Csv
cmdlet中,则您只会获取状态码,而不是url,并且该文件将需要在每个循环中追加,因为这样一来,每次都会重新创建该文件,仅显示文件中最后一个URL的状态代码。
I've shown two possible approaches below: 我在下面显示了两种可能的方法:
This approach isn't exactly the cleanest, it doesn't use the Export-Csv
cmdlet 这种方法并不是最干净的,它没有使用
Export-Csv
cmdlet
$urls = Get-Content C:\webSiteAddress.txt
# Add the header to the file
Set-Content "C:\status.csv" -Value "URL,Status"
foreach($url in $urls)
{
$status = $(Invoke-webrequest -URI $url).statuscode
# Create the data line
$line = "$url,$status"
# Append the data to the file
Add-Content "C:\status.csv" $line
}
Using Export-Csv
is a little more complicated because Export-Csv
expects and object with properties as its columns. 使用
Export-Csv
有点复杂,因为Export-Csv
期望对象以其属性为列。 On each loop, you tell the process to -Append
to the existing CSV so the data isn't lost in the process. 在每个循环中,您告诉流程-
-Append
到现有的CSV,这样数据就不会在流程中丢失。
# Remove the file if it exists already
if ([System.IO.File]::Exists("C:\status.csv")) {Remove-Item "C:\status.csv"}
# Get the web addresses
$urls = Get-Content "C:\webSiteAddress.txt"
# Loop over the URLs
foreach($url in $urls) {
# Create a new object containing the url and the statuscode property
# of an Invoke-WebRequest call. Choose the column names here by changing url,status
New-Object PSObject -Property @{
url=$url;
status=$(Invoke-webrequest -URI $url).statuscode
} |
export-csv "C:\status.csv" -Append -NoTypeInformation
}
The second approach is preferable because it properly quotes the values in the CSV, preventing any data integrity issues. 第二种方法是可取的,因为它正确地引用了CSV中的值,从而防止了任何数据完整性问题。
You should use an foreach for that because you have a list of websites. 您应该为此使用一个foreach,因为您有一个网站列表。 So you should try something like this.
所以你应该尝试这样的事情。
$urls = Get-Content C:\webSiteAddress.txt
foreach($url in $urls)
{
Invoke-webrequest -URI $url |statuscode | export-csv c:\status.csv
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.