2016-03-08 25 views
0

好的,我更改了我的代码。现在数据直接加载到html中,而不是保存到文件中,然后转换为HTML表格。 这是我的新代码:Powershell - 不会将结果传输到报告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 = "[email protected]" 
$EmailTo = "[email protected]" 
$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) 

我的电子邮件reprot看起来很糟糕,I`s不frmated,我不知道为什么?

@{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

我没有经历整个代码或却没有做试运行,初步看起来你已经定义的函数else块内,从来没有所谓的..你可以检查的一部分。 – SavindraSingh

+0

但是电子邮件raport没有问题。不仅会将XXX写入离线。 –

+0

它只显示“XXX Offline”,因为它是'if(!(test-connection -computername $ _ -quiet -count 1)){...}'的一部分,而当你的代码进入Else {}时,块,它只找到函数的定义而不是任何代码来执行它。您可以尝试在脚本的Else {}块之外的某处定义函数,并通过传递该参数在Else块内调用它。 – SavindraSingh

回答

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' 
     } 
    } 
} 
+0

我更改我的代码。你现在可以看到它看起来如何? –

+0

正在生成的HTML输出似乎存在问题。试着看看(Write-Host)你通过$ html得到的输出作为最终结果。 – SavindraSingh

+0

写主机它也没有配置。在电子邮件。 –