好的,我改变了我的代码。现在数据直接加载到html中,而不是保存到文件中,然后作为html表转换为电子邮件。 这是我的新代码:
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 -ErrorAction SilentlyContinue|
Select SystemName,DeviceID,VolumeName,$PercentFree,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{name="PercentFree(%)";Expression={int}}
}
$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 }}
</style>
</head>
<body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/></colgroup>
"
Get-Content U:\Users\XXX\Desktop\servers.txt | ForEach-Object {
if(!(test-connection -computername $_ -quiet -count 1))
{
$html += "<tr><td>$_</td><td>Offline</td><td></td><td></td><td></td><td></td><td></td></tr>"
}
else
{
# $entries = Get-Content U:\Users\XXX\Desktop\servers.txt | % { Get-ComInfo -computers $_ } | ForEach-Object {
Get-ComInfo -computers $_ | ForEach-Object {
if ([float]::Parse($_.'FreeSpace(GB)') -le 5)
{
#$alertEntryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
$html += "<tr class='alert'><tr><td>$_.SystemName</td><td>$_.DeviceID</td><td>$_.VolumeName</td><td>$_.'FreeSpace(GB)'</td><td>$_.'Size(GB)'</td><td>$_.'PercentFree(%)'</td><td>$_.'LOW SPACE'</td></tr>"
}
else
{
#$entryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
$html += "<tr><td>$_.SystemName</td><td>$_.DeviceID</td><td>$_.VolumeName</td><td>$_.'FreeSpace(GB)'</td><td>$_.'Size(GB)'</td><td>$_.'PercentFree(%)'</td><td>$_.'LOW SPACE'</td></tr>"
}
}
}
}
$html +=
"
</table>
</body>
</html>
"
$SMTPServer = "smtp.XXX.XXX"
$EmailFrom = "XXX@XXX.XXX"
$EmailTo = "XXX@XXX.XXX"
$Subject = "$computers PROGRES ;))"
$body = $html
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("XXX", "XXX");
$msg.Body = $html
$msg = New-object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $body)
$msg.IsBodyHTML = $true
$SMTPClient.Send($msg)
我的电子邮件重复看起来很糟糕,我没有受到骚扰,我也不知道为什么?
@{SystemName=K005; DeviceID=U:; VolumeName=Nowy; FreeSpace(GB)=87,4; Size(GB)=465,8; PercentFree(%)=}.SystemName @{SystemName=K005; DeviceID=U:; VolumeName=Nowy; FreeSpace(GB)=87,4; Size(GB)=465,8; PercentFree(%)=}.DeviceID @{SystemName=K005; DeviceID=U:; VolumeName=New; FreeSpace(GB)=87,4; Size(GB)=465,8; PercentFree(%)=}.VolumeName @{SystemName=K005; DeviceID=U:; VolumeName=Nowy; FreeSpace(GB)=87,4; Size(GB)=465,8; PercentFree(%)=}.'FreeSpace(GB)' @{SystemName=K005; DeviceID=U:; VolumeName=New; FreeSpace(GB)=87,4; Size(GB)=465,8; PercentFree(%)=}.'Size(GB)' @{SystemName=K005; DeviceID=U:; VolumeName=Nowy; FreeSpace(GB)=87,4; Size(GB)=465,8; PercentFree(%)=}.'PercentFree(%)' @{SystemName=K005; DeviceID=U:; VolumeName=Nowy; FreeSpace(GB)=87,4; Size(GB)=465,8; PercentFree(%)=}.'LOW SPACE'
XXX Offline
XXX Offline
为什么没有格式化?我之前能够做我想做的事情(或者在报告中取消订阅XXX离线),但现在对我来说它并没有像以前那样格式化。
答案 0 :(得分:0)
我对代码进行了一些更改,但没有对其进行测试。这只是为了让您了解如何继续下一步以实现您的目标。请根据需要进行必要的更改。
Function Get-ComInfo
{
param(
$computers
)
#Here LOW Space thresold is lessthan 10% of the total size of the Volume
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $computers -ErrorAction SilentlyContinue|
Select SystemName,DeviceID,VolumeName,`
@{Name="FreeSpace(GB)";Expression={[Math]::Round($_.FreeSpace /1GB,2)}},`
@{Name="Size(GB)";Expression={[Math]::Round($_.Size/1gb,2)}},`
@{Name="PercentFree(%)";Expression={[Math]::Round(($_.FreeSpace*100/$_.Size)/1gb,2)}}
}
$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 }}
</style>
<style type="text/css">
.status {{
background-color: #FEFE33 }}
</style>
</head>
<body>
<table>
<colgroup><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></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>'
$StatusTest = '<tr><td>{0}</td></tr>'
Get-Content U:\Users\xxx\Desktop\servers.txt |
ForEach-Object
{
if(!(test-connection -computername $_ -quiet -count 1))
{
$StatusTest -f "$_ offline"
}
else
{
}
$entries = Get-Content U:\Users\xxx\Desktop\servers.txt | % { Get-ComInfo -computers $_ } |
ForEach-Object
{
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'
}
}
}