如何使用 PowerShell 生成多個 HTML 報告?
若要使用 PowerShell 在 HTML 檔案中生成或追加多個輸出,我們需要在ConvertTo-HTML命令中使用 - Fragment引數。
例如,假設我們需要生成計算機使用率報告,其中包括消耗前 5 名的程序、已停止的服務,以及 disk 使用率報告。可以透過將輸出傳送到ConvertTo-HTML管道命令來生成單個報告。
示例
$Heading = "<h1><center>System Utilization Report</h1></center>" $procs = Get-Process | Sort-Object -Property CPU -Descending| Select - First 5 | ` ConvertTo-Html -Property ProcessName, ID, CPU -Fragment - PreContent "<h3>High Utilization Processes</h3>" $services = Get-Service | where{$_.StartType -eq "Disabled"} | ` ConvertTo-Html -Property Name, Status, StartType -Fragment - PreContent "<h3>Disabled Services</h3>" $disks = Get-WmiObject win32_logicaldisk | ` Select DeviceID, @{N='FreeSpace(GB)';E={[math]::Round(($_.FreeSpace/1GB),2)}} , ` @{N='TotalSpace(GB)';E={[math]::Round(($_.Size/1GB),2)}} | ` ConvertTo-Html -Fragment -PreContent "<h3>Disk Information</h3>" ConvertTo-Html -Body "$Heading $procs $services $disks" | OutFile C:\Temp\SystemUtilizationReport.html ii C:\Temp\SystemUtilizationReport.html
輸出
若要將 CSS 樣式新增到報告中,
$Heading = "<h1><center>System Utilization Report</h1></center>" $procs = Get-Process | Sort-Object -Property CPU -Descending| Select - First 5 | ` ConvertTo-Html -Property ProcessName, ID, CPU -Fragment - PreContent "<h3>High Utilization Processes</h3>" $services = Get-Service | where{$_.StartType -eq "Disabled"} | ` ConvertTo-Html -Property Name, Status, StartType -Fragment - PreContent "<h3>Disabled Services</h3>" $disks = Get-WmiObject win32_logicaldisk | ` Select DeviceID, @{N='FreeSpace(GB)';E={[math]::Round(($_.FreeSpace/1GB),2)}} , ` @{N='TotalSpace(GB)';E={[math]::Round(($_.Size/1GB),2)}} | ` ConvertTo-Html -Fragment -PreContent "<h3>Disk Information</h3>" ConvertTo-Html -Body "$Heading $procs $services $disks" -Head $cssstyle | OutFile C:\Temp\SystemUtilizationReport.html ii C:\Temp\SystemUtilizationReport.html
輸出
廣告