Powershell获取磁盘空间,转换为HTML并比较值,如果小于15%,则更改整行的颜色

时间:2016-05-17 12:49:44

标签: html powershell wmi

我创建了一个从服务器列表中读取的PowerShell脚本,使用Get-WmiObject获取驱动器容量,释放空间,然后计算可用空间百分比并将其全部转换为一个漂亮的小HTML。

问题是我需要弄清楚如何让它看看自由空间的百分比,如果它低于15%颜色,那么html表的整行都是红色的。

这甚至可能吗? 任何人都可以帮忙吗?

下面是我的powershell

$servers = GC XX:\myservers.txt
$date = get-date -Format yyyyMMdd
$time = get-date -Format hhmm
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: separate;width:800px}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:lightblue}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:White}"
$a = $a + "</style>"
Foreach ($s in $servers)  
{  
  Get-WmiObject -Class win32_volume -cn $s | 
   Select-Object @{LABEL='Computer';EXPRESSION={$s}}, 
     driveletter, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}},
       @{LABEL='Capacity';EXPRESSION={"{0:N2}" -f ($_.capacity/1GB)}}, 
        @{LABEL='Percentage';EXPRESSION={"{0:N2}" -f ($_.freespace/$_.capacity*100)+"%"}} | 
     ConvertTo-Html -head $a | Out-File -append "D:\Users\PLACE\Desktop\diskspace\Freespace$date-$time.htm"
} 

2 个答案:

答案 0 :(得分:1)

我认为您有两种选择:您可以使用ConvertTo-Html cmdlet创建HTML并在之后格式化行,也可以使用某些字符串格式为自己创建html:

$html = 
@'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>BODY{{background-color:white;}}TABLE{{border-width: 2px;border-style: solid;border-color: black;border-collapse: separate;width:800px}}TH{{border-width: 1px;padding: 0px;border-style: solid;border
-color: black;background-color:lightblue}}TD{{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:White}}</style>
</head><body>
<table>
<colgroup><col/><col/><col/><col/><col/></colgroup>
<tr><th>Computer</th><th>driveletter</th><th>GBfreespace</th><th>Capacity</th><th>Percentage</th></tr>
{0}
</table>
</body></html>
'@

$trTemplate ='<tr><td>{0}</td><td>{1}</td><td>{2:N2}</td><td>{3:N2}</td><td {4}>{5:N2}%</td></tr>'
$redStyle = 'style="background-color:Red"'
$tr = @()
$servers = GC XX:\myservers.txt

Foreach ($s in $servers)  
{  
  Get-WmiObject -Class win32_volume -cn $s | Where Capacity | ForEach-Object {

    $freespaceInPercent = ($_.freespace/$_.capacity*100)

    $red = ''
    if ($freespaceInPercent -lt [double]15)
    {
        $red = $redStyle
    }

    $tr += ($trTemplate -f $s, $_.driveletter, ($_.freespace/1GB), ($_.capacity/1GB), $red, $freespaceInPercent)
  }
} 


$html -f ($tr -join '') | Out-File -append "D:\Users\&lt;Your username here>\Desktop\diskspace\Freespace$date-$time.htm"

答案 1 :(得分:0)

这是我最终的结果,它的工作原理与我需要的一样

$server_file = "D:\location\Windowsservers.txt"
$html_file_dir = "\\netwolocation\DiskSpace"


$background_color = "#FFFFFF" # can be in rgb format (rgb(140,166,193)) or hex format (#FFFFFF)
$server_name_font = "Tahoma"
$server_name_font_size = "20px"
$server_name_bg_color = "#FFFFFF" # can be in rgb format (rgb(77,108,145)) or hex format (#FFFFFF)
$heading_font = "Tahoma"
$heading_font_size = "14px"
$heading_name_bg_color = "#0099FF" # can be in rgb format (rgb(95,130,169)) or hex format (#FFFFFF)
$data_font = "Tahoma"
$data_font_size = "11px"

$Critical_space = "#FF0000" # (Red) Critical low space
$very_low_space = "#FFFF00" # (Yellow) very low space
$low_space = "#FF9900" # (Orange) low space 
$medium_space = "#99CC66" # (Green) medium 

$ErrorActionPreference = "SilentlyContinue"
$date = Get-Date -UFormat "%Y%m%d-%H%m"
$html_file = New-Item -ItemType File -Path "$html_file_dir\WindowsServerDiskSpaceCheck_$date-$time.html" -Force

$html_file

Function ConvertBytes {
param($size)
If ($size -lt 1MB) {
    $drive_size = $size / 1KB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' KB'
}elseif ($size -lt 1GB){
    $drive_size = $size / 1MB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' MB'
}ElseIf ($size -lt 1TB){ 
    $drive_size = $size / 1GB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' GB'
}Else{
    $drive_size = $size / 1TB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' TB'
}
}

$html_header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Server Drive Space</title>
<style type="text/css">
.serverName { text-align:center; font-family:"' + $server_name_font + '"; font-size:' + $server_name_font_size + `
    '; font-weight:bold; background-color: ' + $server_name_bg_color + '; border: 1px solid black; width: 150px; }
.headings { text-align:center; font-family:"' + $heading_font + '"; font-size:' + $heading_font_size + `
    '; font-weight:bold; background-color: ' + $heading_name_bg_color + '; border: 1px solid black; width: 150px; }
.data { font-family:"' + $data_font + '"; font-size:' + $data_font_size + '; border: 1px solid black; width: 150px; }
#dataTable { border: 1px solid black; border-collapse:collapse; }
body { background-color: ' + $background_color + '; }
#legend { border: 1px solid black; position:absolute; right:500px; top:10px; }
</style>
<script language="JavaScript" type="text/javascript">
<!--

function zxcWWHS(){
 if (document.all){
  zxcCur=''hand'';
  zxcWH=document.documentElement.clientHeight;
  zxcWW=document.documentElement.clientWidth;
  zxcWS=document.documentElement.scrollTop;
  if (zxcWH==0){
   zxcWS=document.body.scrollTop;
  zxcWH=document.body.clientHeight;
   zxcWW=document.body.clientWidth;
  }
 }
 else if (document.getElementById){
  zxcCur=''pointer'';
  zxcWH=window.innerHeight-15;
  zxcWW=window.innerWidth-15;
  zxcWS=window.pageYOffset;
 }
 zxcWC=Math.round(zxcWW/2);
 return [zxcWW,zxcWH,zxcWS];
}


window.onscroll=function(){
 var img=document.getElementById(''legend'');
 if (!document.all){ img.style.position=''fixed''; window.onscroll=null;     return; }
 if (!img.pos){ img.pos=img.offsetTop; }
 img.style.top=(zxcWWHS()[2]+img.pos)+''px'';
}
//-->
</script>
</head>
<body>'

   $html_footer = '</body>
</html>'

Add-Content $html_file $html_header

Get-Content $server_file |`
    ForEach-Object { 
        $hostname = Get-WmiObject -Impersonation Impersonate -ComputerName $_ -Query "SELECT Name From Win32_ComputerSystem"
    $name = $hostname.Name.ToUpper()
    Add-Content $html_file ('<Table id="dataTable"><tr><td colspan="3" class="serverName">' + $name + '</td></tr>
    <tr><td class="headings">Drive Letter</td><td class="headings">Total Size</td><td class="headings">Free Space</td><td class="headings">Percent Free</td></tr>')

    $drives = Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" -ComputerName $_ -Impersonation Impersonate


    ForEach ($drive in $drives) {
        $space_color = ""
        $free_space = $drive.FreeSpace
        $percent = ($drive.FreeSpace/$drive.size*100)
        $percent = [Math]::Round($percent, 2)
        If ($percent -le 1) {
            $space_color = $Critical_space
        }elseif ($percent -le 5) {
            $space_color = $very_low_space
        }elseif ($percent -le 10) {
            $space_color = $low_space
        }elseif ($percent -le 15) {
            $space_color = $medium_space
        }


        Add-Content $html_file ('<tr><td class="data">' + $drive.deviceid + '</td><td class="data">' + (ConvertBytes $drive.size) + `
            '</td><td class="data" bgcolor="' + $space_color + '">' + (ConvertBytes $drive.FreeSpace) + '</td><td class="data" bgcolor="' + $space_color + '">' + $percent + '</td></tr>')
    }

    Add-Content $html_file ('</table></br><div id="legend">
        <Table><tr><td style="font-size:12px">Less then or equal to 1% Free Space</td><td bgcolor="' + $Critical_space + '" width="10px"></td></tr>
            <tr><td style="font-size:12px">1% to 5% Free Space</td><td bgcolor="' + $very_low_space + '" width="10px"></td></tr>
            <tr><td style="font-size:12px">5% to 10% Free Space</td><td bgcolor="' + $low_space + '" width="10px"></td></tr>
            <tr><td style="font-size:12px">10% to 15% Free Space</td><td bgcolor="' + $medium_space + '" width="10px"></td></tr>
        </table></div>')
    }


Add-Content $html_file $html_footer