创建 html 格式的服务器报告

创建 html 格式的服务器报告

我创建了以下简单的代码,可以生成报告。它可以很好地拉入 excel,但理想情况下我希望以 html 格式输出。我可以通过管道传输输出来转换它吗?我如何包含列标题?


[array]$Serverlist = Get-ADComputer -Filter * -SearchBase "OU=blah, DC=blah, DC=blah" | where {($_.DistinguishedName -notlike "*OU=Test*")}
$num = 1

foreach ($servername in $Serverlist) {

   $version = try {invoke-command -computername $Servername.name {WMIC OS Get Name} -ErrorAction Stop | Select-string -Pattern 'Windows Server'} catch { $ServerVersion = "Denied, Check WinRM" }

   if ($Serverversion -notlike "Denied*") {
   
      $ServerVersion = $version.line.Substring(0,($version.line.IndexOf('|C:\')))
      $cpu = invoke-command -computername $servername.name {Get-CimInstance -ClassName 'Win32_Processor' | Select-Object -Property 'DeviceID', 'NumberOfCores'}
      $ram = invoke-command -computername $servername.name {(Get-WmiObject -class "cim_physicalmemory" | Measure-Object -Property Capacity -Sum).Sum}; $ram = [math]::Round($ram/1gb) 
      $disk = invoke-command -computername $servername.name {(Get-WmiObject Win32_LogicalDisk | Measure-Object -Sum size).sum}; $disk = [math]::Round($disk/1gb)
      $sockets = $cpu.DeviceID.count
      
      } else {
       
      $cpu = ""; $ram = ""; $disk =""

      }
   
   $server = $servername.name + "|" + $serverversion + "|" + $sockets + "|" + $cpu.numberofcores+ "|" + $ram + "|" + $disk 
   write-host "Examining server" $num "of" $Serverlist.Count 
   Add-Content -Path $PSScriptRoot\report.txt -value $server
   
   $Serverversion = ""; $sqlversion = ""; $num++
            
}

答案1

请参阅此 SO Q&A 讨论,它与您在此处的要求类似:

发送 HTML 格式的批量邮件

尽管它专门用于电子邮件,但对于您的用例来说,方法是相同的,并且资源指针将直接覆盖您的用例。

答案2

现在有一份可读的报告。交替使用行的颜色,使其更易于阅读。


[array]$Serverlist = Get-ADComputer -Filter * -SearchBase "OU=blah, DC=blah, DC=blah" | where {($_.DistinguishedName -notlike "*OU=Test*")}

Remove-Item -Path $PSScriptRoot\report.html
$header = '<TABLE width=100% border=1 style="background-color:CCCCCC"><TR><TD width=10%><B>Server Name</B></TD><TD width=50%><B>OS Version</B></TD><TD width=10%><B>Sockets</B></TD><TD width=10%><B>Cores</B></TD><TD width=10%><B>Ram</B></TD><TD width=10%><B>Total Disk</B></TD></TR></TABLE>'
Add-Content -Path $PSScriptRoot\report.html -value $header
$num = 1

foreach ($servername in $Serverlist) {

   $version = try {invoke-command -computername $Servername.name {WMIC OS Get Name} -ErrorAction Stop | Select-string -Pattern 'Windows Server'} catch { $ServerVersion = "Denied, Check WinRM" }

   if ($Serverversion -notlike "Denied*") {
   
      $ServerVersion = $version.line.Substring(0,($version.line.IndexOf('|C:\')))
      $cpu = invoke-command -computername $servername.name {Get-CimInstance -ClassName 'Win32_Processor' | Select-Object -Property 'DeviceID', 'Name', 'NumberOfCores'}
      $ram = invoke-command -computername $servername.name {(Get-WmiObject -class "cim_physicalmemory" | Measure-Object -Property Capacity -Sum).Sum}; $ram = [math]::Round($ram/1gb) 
      $disk = invoke-command -computername $servername.name {(Get-WmiObject Win32_LogicalDisk | Measure-Object -Sum size).sum}; $disk = [math]::Round($disk/1gb)
      $sockets = $cpu.DeviceID.count
      
      } else {
       
      $cpu = ""; $ram = ""; $disk =""

      }  

   if ($num % 2) {$colour = 'style=background-color:ffffff'} else {$colour = 'style=background-color:ffffcc'}
   $server = '<TABLE width=100% border=1 ' + $colour + '><TR><TD width=10%>' + $servername.name + '</TD><TD width=50%>' + $serverversion + '</TD><TD width=10%>' + $sockets + '</TD><TD width=10%>' + $cpu.numberofcores+ '</TD><TD width=10%>' + $ram + '</TD><TD width=10%>' + $disk + '</TD></TR>'
   write-host "Examining server" $num "of" $Serverlist.Count 
   Add-Content -Path $PSScriptRoot\report.html -value $server 
      
   $Serverversion = ""; $sqlversion = ""; $num++
            
}

相关内容