Powershell Web状态脚本

时间:2014-05-15 09:19:29

标签: powershell

我有一个脚本,它读取URL的文件,然后执行webrequest来检查站点门户列表的状态和延迟。我们在内部网络服务器的html表中提供它。我们的网址对我们的商业用户没有多大意义我想在表格中创建一个新列,其中显示网站的名称(http://10.x.x.x:8080/portal将被命名为'曼彻斯特')

最好在另一个名字文件中读取,因为两个文件的每一行都匹配

当我在工作中有一些空闲时间但知识有限时,我一直在做这个虚荣项目。

    '## 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'

1 个答案:

答案 0 :(得分:0)

我建议将源文件更改为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
}