2013-07-17 93 views
0

我通過採取一些信息獲取-WmiObject可以:PowerShell的HTML表格

$logicalDisks = Get-WmiObject -ComputerName $cmpSys Win32_LogicalDisk 

然後創造了一些非常基本的HTML代碼,以顯示它的驅動器驅動器:

Foreach ($disk in $logicalDisks) { 
If ($disk.DriveType -eq 3) { 
$disksize = [math]::round(($disk.size/1048576)) 
$freespace = [math]::round(($disk.FreeSpace/1048576)) 
$percFreespace=[math]::round(((($disk.FreeSpace/1048576)/($disk.Size/1048676)) * 100),0) 
$body += @" 
    <font color="red">Drive Letter: </font>$($disk.DeviceID) 
    <br> 
    <font color="red">Volume Label: </font>$($disk.VolumeName) 
    <br> 
    <font color="red">FileSystem Type: </font>$($disk.FileSystem) 
    <br> 
    <font color="red">Disk Size (MB): </font>$($disksize)MB 
    <br> 
    <font color="red">Free Space (MB)/%: </font>$($freespace)MB/$($percFreeSpace)% 
    <br> 
    <br> 
"@ 
    } 
} 

然而,這顯示是相當通用的,我希望有一個可用的報告傳遞給其他部門。我怎樣才能把它格式化成表格?喜歡的東西:

DriveLetter VolumeLabel FileSystemType DiskSize Freespace % 
    C    OS    NTFS   100GB   32% 
    E   DATA   NTFS   1000GB   2% 

回答

2

「表」是ConvertTo-Html cmdlet的默認輸出格式:

gwmi Win32_LogicalDisk -Computer $cmpSys -Filter 'DriveType = 3' | 
    select @{n='DriveLetter';e={$_.DeviceID -replace ':'}}, 
     @{n='VolumeLabel';e={$_.VolumeName}}, 
     @{n='FileSystemType';e={$_.FileSystem}}, 
     @{n='DiskSize';e={"{0}GB" -f [int]($_.Size/1GB)}}, 
     @{n='Freespace %';e={"{0}%" -f [int]($_.FreeSpace/$_.Size*100)}} | 
    ConvertTo-Html -Head '<style>th,td {text-align:center;}</style>' 

如果你想運行此agains多臺計算機(-Computer可以利用主機名的數組),你可能要包括主機名,以及:

gwmi Win32_LogicalDisk -Computer $cmpSys -Filter 'DriveType = 3' | 
    select @{n='Hostname';e={$_.SystemName}}, 
     @{n='DriveLetter';e={$_.DeviceID -replace ':'}}, 
     @{n='VolumeLabel';e={$_.VolumeName}}, 
     @{n='FileSystemType';e={$_.FileSystem}}, 
     @{n='DiskSize';e={"{0}GB" -f [int]($_.Size/1GB)}}, 
     @{n='Freespace %';e={"{0}%" -f [int]($_.FreeSpace/$_.Size*100)}} | 
    ConvertTo-Html -Head '<style>th,td {text-align:center;}</style>' 
+0

但它是我問的格式,它產生一個表的事實是在那裏,但我需要它對齊等,如後。 – PnP

+0

查看更新的答案。如果你想要一些與其他格式不同的列,你需要使它們與衆不同。通過在相應列的「​​」標籤中添加一個'class'屬性以及該類的樣式。 –

1

這是我如何完成從創建PowerShell中的HTML的任務。

# First build the HTML structure for the ConvertTo-HTML 
$CD_a = $CD_a + "<!DOCTYPE html>" 
$CD_a = $CD_a + "<html>" 
$CD_a = $CD_a + "<head>" 
$CD_a = $CD_a + "<style>" 
$CD_a = $CD_a + "BODY{background-color:white;}" 
$CD_a = $CD_a + "TABLE{border=1;border-color: black;border-width: 1px;border-style:solid;border-collapse: separate;empty-cells:show}" 
$CD_a = $CD_a + "TH{border-width:1px;padding: 3px;border-style:solid;font-weight:bold;text-align:center;border-color:black;background-color:#99CC00}" 
$CD_a = $CD_a + "TD{color:black;colspan=1;border-width:1px;padding:1px;font-weight:normal;font-size:18;border-style:solid;border-color:black;background-color:#CCFFCC}" 
$CD_a = $CD_a + "</style>" 
$CD_a = $CD_a + "</head>" 
$CD_a = $CD_a + "</html>" 

# Building the WMI. Change the default name to add spaces. 
$GWMI_OS = GWmi Win32_operatingsystem 
$Start_HTML_OS = $GWMI_OS | select Caption,` 
@{name="Number of Users";Expression={$_.NumberOfUsers}},` 
@{name="Serial Number";Expression={$_.SerialNumber}},` 
@{name="Service Pack Major Version";Expression={$_.ServicePackMajorVersion}},` 
@{name="OS Architecture";Expression={$_.OSArchitecture}} 

# This is a more specific way and can be customized more with HTML or CSS 
$GWMI_OS_caption = $GWMI_OS.caption 
$GWMI_OS_NumberOfUsers = $GWMI_OS.NumberOfUsers 
$GWMI_OS_SerialNumber = $GWMI_OS.SerialNumber 
$GWMI_OS_ServicePackMajorVersion = $GWMI_OS.ServicePackMajorVersion 
$GWMI_OS_OSArchitecture = $GWMI_OS.OSArchitecture 

# Converting the WMI to HTML 
$Start_HTML_OS_Final = $Start_HTML_OS | ConvertTo-HTML -head $CD_a 

# Now I create a variable to store the HTML doc. 
$Create_HTML_doc = "<!DOCTYPE html> 
<head><title> Computer Data </title> 
<style> 
TABLE{border=1; border-color:black; border-width:1px; border-style:solid; empty-cells:show} 
TH{border-width:1px; padding:1px; border-style:solid; font-weight:normal; text-align:center;border-color:black;background-color:#99CC00;empty-cells:show} 
TD{color:black; colspan=1; border-width:1px; padding:1px; font-weight:bold; font-size:18;border-style:solid;border-color:black;background-color:#CCFFCC;empty-cells:show} 
</style> 
</head> 

<H2> Operating System, WMI </H2> $Start_HTML_OS_Final 

<H2> Operating System, WMI </H2> 
<table><tr> 
<th> Caption </th> 
<th> Number Of Users </th> 
<th> Serial Number </th> 
<th> Service Pack Major Version </th> 
<th> OS Architecture </th> 
</tr><tr> 
<td> $GWMI_OS_caption </td> 
<td> $GWMI_OS_NumberOfUsers </td> 
<td> $GWMI_OS_SerialNumber </td> 
<td> $GWMI_OS_ServicePackMajorVersion </td> 
<td> $GWMI_OS_OSArchitecture </td> 
</tr></table>" 

$Create_HTML_doc > C:\PowerShell\print\HTML\Get_WMI_OS.html 
ii C:\PowerShell\print\HTML\Get_WMI_OS.html 

PowerShell沒有HTML或CSS的問題。您必須考慮的唯一因素是轉義引號「」。這是通過使用$('「引號」')來完成的。