Powershell - 免费磁盘空间警报 - 添加测试连接

时间:2016-02-24 13:02:02

标签: powershell

好的,我尝试添加Test-Connection,现在代码看起来像这样:

$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>
<title>HTML TABLE</title>
 <style type="text/css">
  .alert {{
    background-color: #FB1717 }}
    .statusCom {{
    background-color: #FB1717
    }}
  </style>
</head>
<body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>SystemName</th><th>DeviceID</th><th>VolumeName</th><th>Size(GB)</th><th>FreeSpace(GB)</th><th>Status</th></tr>
{0}
</table>
</body></html>
'@

$entryTemplate = '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>'
$alertEntryTemplate = '<tr class="alert"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>'
$statusTemplate = '<tr class="statusCom"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td></tr>'


Function Get-ComInfo {   
param(
$computers
)

#Here LOW Space thresold is lessthan 10% of the total size of the Volume
$PercentFree = @{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace /1GB)}} 
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $computers |
Select SystemName,DeviceID,VolumeName,$PercentFree,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{name="PercentFree(%)";Expression={int}}

}
$servers = Get-Content U:\Users\xxx\Desktop\servers.txt 

$client = $servers
    if(Test-Connection -ComputerName (Get-Content U:\Users\xxx\Desktop\servers.txt ) -BufferSize 16 -Count 1 -Quiet) {
$entries = $servers | % { Get-ComInfo -computers $_ } | % {

    if ([float]::Parse($_.'FreeSpace(GB)') -le 5) {
        $alertEntryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }

    else {
        $entryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }
}
    }
    else{
        $statusTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE', $_.'Status'
    }


$html -f ($entries -join ' ') | out-file U:\Users\xxx\Desktop\Drivess.html
$OutputFile = "U:\Users\xxx\Desktop\Drivess.html"
$SMTPServer = "smtp.xxx.com" 
$EmailFrom = "xxxx@xxx.com"
$EmailTo = "xxxx@xxx.com"
$Subject = "$computers PROGRES ;))"  
$body = (Get-Content $OutputFile) 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxx", "xxx");
$MailTextT =  Get-Content  -Path U:\Users\xxx\Desktop\Drivess.html -Raw
$msg = New-object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $body)
$msg.IsBodyHTML = $true  
$SMTPClient.Send($msg)

在html中我添加:

.statusCom {{
    background-color: #FB1717
    }}

$statusTemplate = '<tr class="statusCom"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td></tr>'

我有多台计算机,其中一台在我的servers.txt离线,我收到此错误:

Get-WmiObject : Serwer RPC it is offline. (Expectiob HRESULT: 0x800706BA)
At U:\Users\xxx\Desktop\test.ps1:36 char:1
+ Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $comp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

如果我有一台计算机而且它处于离线状态,那就得到这个:

<tr class="statusCom"><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

if(Test-Connection -ComputerName COMPUTERNAME_HERE -Count 1 -EA SilentlyContinue){&#34; online&#34; } else {&#34;离线&#34; }

您需要将某处带入您的脚本,这应该很容易完成。 您可以使用Get-ComInfo函数提供的Systemname作为Computername,并且您需要将{2}的两个模板扩展为在线/离线信息。