0

我正在通过 Get-WmiObject 获取一些信息:

$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%
4

2 回答 2

3

“表格”是ConvertTo-Htmlcmdlet 的默认输出格式:

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

如果您想在多台计算机上运行此操作(-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>'
于 2013-07-17T22:11:36.490 回答
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 方面没有问题。您唯一需要考虑的是转义引号“”。这是通过使用 $('"引号"') 来完成的。

于 2015-12-15T18:06:30.150 回答