簡體   English   中英

Powershell獲得磁盤空間,轉換為HTML並比較一個值,如果該值小於15%,則更改整個行的顏色

[英]Powershell get disk space, convert to HTML and compare a value and if it is less than 15% change the color of the entire row

我創建了一個從服務器列表中讀取的PowerShell腳本,使用Get-WmiObject獲取驅動器容量,可用空間,然后計算可用空間百分比並將其全部轉換為漂亮的HTML。

問題是我需要弄清楚如何讓它查看可用空間的百分比,如果它低於15%,則html表的整個行都變成紅色。

這有可能嗎? 有人能幫忙嗎?

以下是我的功能

$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"
} 

我認為您有兩個選擇:您可以使用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"

這就是我最終得到的結果,它的工作原理與我需要的完全一樣

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM