Powershell web状态脚本
我有一个脚本,它读取一个URL文件,然后执行一个webrequest来检查网站门户列表的状态和延迟。我们在内部Web服务器上的html表中提供了这一点。我们的URL对我们的商业用户来说不是很有意义。我想在表中创建一个新的列,显示站点的名称(将命名为“Manchester”) 最好读入另一个文件名,因为两个文件的每一行都将匹配 我一直在做一个虚荣的项目,当我有一点空闲时间在工作,但知识有限Powershell web状态脚本,powershell,Powershell,我有一个脚本,它读取一个URL文件,然后执行一个webrequest来检查网站门户列表的状态和延迟。我们在内部Web服务器上的html表中提供了这一点。我们的URL对我们的商业用户来说不是很有意义。我想在表中创建一个新的列,显示站点的名称(将命名为“Manchester”) 最好读入另一个文件名,因为两个文件的每一行都将匹配 我一直在做一个虚荣的项目,当我有一点空闲时间在工作,但知识有限 '## The URI list to test $URLListFile = "H:\Sc
'## The URI list to test
$URLListFile = "H:\Scripts\web.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = @()
$a = Get-date
Foreach($Uri in $URLList) {
$time = try
{
$request = $null
## Request the URI, and measure how long the response took.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch
{
<# If the request generated an exception (i.e.: 500 server
error or 404 not found), we can pull the status code from the
Exception.Response property #>
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] @{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
$SResult += [PSCustomObject] @{
Store = $Store;
}
}
#Prepare email body in HTML format
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Stores Web Portal Status</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Stores Web Portal Status $($a)</H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR bgcolor=green>"
}
if($Entry.timetaken -ge "2600.000")
{
$Outputreport += "<TR bgcolor=yellow>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file H:\Scripts\Test.htm'
”##要测试的URI列表
$URLListFile=“H:\Scripts\web.txt”
$URLList=获取内容$URLListFile-ErrorAction SilentlyContinue
$Result=@()
$a=获取日期
Foreach($URLList中的Uri){
$time=try
{
$request=$null
##请求URI,并测量响应所用的时间。
$result1=Measure命令{$request=Invoke-WebRequest-Uri$Uri}
$result1.0毫秒
}
接住
{
$request=$\异常.Response
$time=-1
}
$result+=[PSCustomObject]@{
时间=获取日期;
Uri=$Uri;
StatusCode=[int]$request.StatusCode;
StatusDescription=$request.StatusDescription;
ResponseLength=$request.RawContentLength;
耗时=$time;
}
$SResult+=[PSCustomObject]@{
Store=$Store;
}
}
#准备HTML格式的电子邮件正文
如果($result-ne$null)
{
$Outputreport=“Stores Web Portal Status Stores Web Portal Status$($a)URLStatusCodeStatusDescriptionResponseLengthTimeTake我建议将源文件更改为CSV格式,该格式同时存储URI和名称。从概念上讲,它类似于:
$csvText = @"
Uri,SiteName
http://10.0.0.1/foo,Foo
http://10.0.0.2/bar,Bar
"@
$siteRecords = ConvertFrom-Csv $csvText
$siteRecords
实际上,您可以使用Import Csv从文件中获取数据,然后在迭代时可以访问这两个属性
$siteRecords = Import-Csv -Path $pathToYourCsvFile
foreach ($record in $siteRecords)
{
$record.Uri
$record.SiteName
}
问题是什么?你是在问如何用html创建一个新列吗?你需要帮助脚本的哪一部分?抱歉,这可能写得不太好。我可以用html创建新列,我需要帮助用包含名称列表的文件的每一行填充该列。理想情况下,我只需要替换$entry.ur我用新文件的行列了一列谢谢!我已经使用导入作为csv来排序了。谢谢James