Powershell中的服务器运行状况报告

Powershell中的服务器运行状况报告,powershell,Powershell,我已通过PowerShell准备服务器运行状况报告。我的脚本工作正常,但当我通过脚本接收电子邮件时,我在底部得到一些随机值,而且我无法在发送邮件命令中添加正文。当我这样做时,脚本生成异常。 这是我的PS代码: <# .SYNOPSIS Script for the detailed server health report .DESCRIPTION The script accepts parameter from CSV file by which we

我已通过PowerShell准备服务器运行状况报告。我的脚本工作正常,但当我通过脚本接收电子邮件时,我在底部得到一些随机值,而且我无法在发送邮件命令中添加正文。当我这样做时,脚本生成异常。 这是我的PS代码:

<#
    .SYNOPSIS
    Script for the detailed server health report

    .DESCRIPTION
    The script accepts parameter from CSV file by which we can get details and
    all information related to health status of the server.The script generates all the 
    information in a CSv file.

#>
    [CmdletBinding(SupportsShouldProcess=$True)]

    param(
        [Parameter(Position=0,mandatory=$true)]
        [string]$Servercsv
    )
    begin{
        Write-Host "Running"
    }
    process{

         # Path for the CSV file that contains all the Print Server information.
        $ServerDetails=import-csv $Servercsv
        $result=@()
        $Outputreport = $null

         foreach($server in $ServerDetails){

         try{    

             #sysinfo variable contains complete systeminfo like manufacturer name, physical memory,servername 

             $cpu=Get-WMIObject  -ComputerName $server.servers win32_processor| select __Server, @{name="CPUUtilization" ;expression ={“{0:N2}” -f (get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 |
                select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average}}

             $disks =Get-WmiObject -Class win32_Volume -ComputerName $server.servers -Filter "DriveLetter = 'C:'" |
                Select-object @{Name = "PercentFree"; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } }


             $os=gwmi -Class win32_operatingsystem -computername $server.servers |
                Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}

        $result += [PSCustomObject] @{  
            ServerName ="$($server.servers)" 
            CPUUtilization = $cpu.CPUUtilization.ToString()
            CDrive = $disks.PercentFree
            MemLoad = $OS.MemoryUsage

            }
            }

            catch{
         "error communicating with $($server.servers), skipping to next"
         }
         }

    $Outputreport = "<HTML><TITLE> Utilization of Server Resources</TITLE>
                     <BODY background-color:peachpuff>
                     <font color =""#99000"" face=""arial"">
                     <H3> Utilization of Server Resources</H3></font>
                     <Table border=2 cellpadding=1 cellspacing=5>
                     <TR bgcolor=gray align=center>
                       <TD><B>Server Name</B></TD>
                       <TD><B>CPUUtilization</B></TD>
                       <TD><B>Memory Utilization</B></TD>
                       <TD><B>CDrive</B></TD></TR>"


    Foreach($Entry in $Result){

    if(([decimal]$Entry.CPUUtilization -ge 15) -or ([decimal]$Entry.MemLoad -ge 50) -or (([decimal]$Entry.CDrive) -le 60) )
    {
    $Outputreport += "<TR>"

     $Outputreport += "<TD><B>$($Entry.Servername)</B></TD>"
     if([decimal]$Entry.CPUUtilization -ge 15)
        {

        $Outputreport += "<TD align=center style=color:red;>$($Entry.CPUUtilization+'%')</TD>"
        }
        else {
        $Outputreport += "<TD align=center style=color:White;>$($Entry.CPUUtilization+'%')</TD>"
        }


        if([decimal]$Entry.MemLoad -ge 50)
        {

        $Outputreport += "<TD align=center style=color:red;>$($Entry.MemLoad+'%')</TD>"
        }
        else{
         $Outputreport += "<TD align=center style=color:White;>$($Entry.MemLoad+'%')</TD>"
         }



       if(([decimal]$Entry.CDrive) -le 60)
       { 

        $Outputreport += "<TD align=center style=color:red;>$($Entry.Cdrive+'%')</TD>"
        }
        else{ $Outputreport += "<TD align=center style=color:White;>$($Entry.Cdrive+'%')</TD>"}


         #$Outputreport += "<TD>$($Entry.Servername)</TD>"


         #$Outputreport += "<TD>$($Entry.Servername)</TD>"

        $Outputreport+="</TR>"

           }



     }

     $Outputreport += "</Table><p>Hello</p></BODY></HTML>" 
      $out=$Outputreport | ConvertTo-Html -Head $Outputreport
     #Send an email about the Load

        # $smtp is the outgoing mail server 
        $smtp= "smtphost.redmond.corp.microsoft.com"

        try{
        send-mailmessage -SmtpServer $smtp -from v-rubhat@microsoft.com  -to v-rubhat@microsoft.com  -subject "Server Health Report"  -BodyAsHtml "$out" 
        }
        catch [exception]
        {
        write-host ("Cannot send mail, since argument is null or empty")
        }


    }

[CmdletBinding(SupportsShouldProcess=$True)]
param(
[参数(位置=0,强制=true)]
[字符串]$Servercsv
)
开始{
写入主机“正在运行”
}
过程{
#包含所有打印服务器信息的CSV文件的路径。
$ServerDetails=导入csv$Servercsv
$result=@()
$Outputreport=$null
foreach($ServerDetails中的服务器){
试试{
#sysinfo变量包含完整的systeminfo,如制造商名称、物理内存、服务器名称
$cpu=Get-WMIObject-ComputerName$server.servers win32|u processor | select|u server,@{name=“cpuUutilization”;表达式={{0:N2}“-f(Get-counter-counter”\processor(\u Total)\%处理器时间”-SampleInterval 1-MaxSamples 5|
select-ExpandProperty反采样| select-ExpandProperty cookedvalue | Measure Object-Average).Average}
$disks=Get WmiObject-Class win32_Volume-ComputerName$server.servers-Filter“DriveLetter='C:'|
选择object@{Name=“PercentFree”;Expression={{0:N2}”-f($\.FreeSpace/$\.Capacity)*100)}
$os=gwmi-类win32_操作系统-computername$server.servers|
选择Object@{Name=“MemoryUsage”;Expression={{0:N2})-f(($.TotalVisibleMemorySize-$.FreePhysicalMemory)*100)/$.TotalVisibleMorySize)}
$result+=[PSCustomObject]@{
ServerName=“$($server.servers)”
CPUUtilization=$cpu.CPUUtilization.ToString()
CDrive=$disks.PercentFree
MemLoad=$OS.MemoryUsage
}
}
抓住{
与$($server.servers)通信时出错,跳过下一步
}
}
$Outputreport=“服务器资源的利用率
服务器资源的利用
服务器名
CPU利用
内存利用率
CDrive“
Foreach($Result中的条目){
如果(([decimal]$Entry.CPUUtilization-ge 15)-或([decimal]$Entry.MemLoad-ge 50)-或([decimal]$Entry.CDrive-le 60))
{
$Outputreport+=“”
$Outputreport+=“$($Entry.Servername)”
如果([decimal]$Entry.CPUUtilization-通用电气15)
{
$Outputreport+=“$($Entry.CPUUtilization+'%')”
}
否则{
$Outputreport+=“$($Entry.CPUUtilization+'%')”
}
如果([十进制]$Entry.MemLoad-ge 50)
{
$Outputreport+=“$($Entry.MemLoad+'%')”
}
否则{
$Outputreport+=“$($Entry.MemLoad+'%')”
}
if(([decimal]$Entry.CDrive)-le 60)
{ 
$Outputreport+=“$($Entry.Cdrive+'%')”
}
else{$Outputreport+=“$($Entry.Cdrive+'%')”}
#$Outputreport+=“$($Entry.Servername)”
#$Outputreport+=“$($Entry.Servername)”
$Outputreport+=“”
}
}
$Outputreport+=“你好

” $out=$Outputreport |转换为Html-Head$Outputreport #发送有关加载的电子邮件 #$smtp是传出邮件服务器 $smtp=“smtphost.redmond.corp.microsoft.com” 试一试{ 从v发送邮件消息-SmtpServer$smtp-rubhat@microsoft.com-to v-rubhat@microsoft.com-主题“服务器运行状况报告”-BodyAsHtml“$out” } 捕获[例外] { 写入主机(“无法发送邮件,因为参数为null或空”) } }
下面是我附加的输出,其中*和1121是随机数:

服务器资源的利用 服务器名称CPU利用率内存利用率CDrive xxxxxxxx 3.25%29.26%22.11% xxxxxxxx 18.80%52.15%57.49% xxxxxxxx 27.76%27.51%71.91%

*
1121

我已经更改了$out变量,并在其中添加了property属性,这样做很有效。这是我编辑的$out命令

$out=$Outputreport | ConvertTo-Html -Property Servername,CPUUtilization,MemLoad,CDrive -Head $Outputreport

这个
$out=$Outputreport |转换为Html-Head$Outputreport
对我来说毫无意义,因为
$Outputreport
已经是一个包含Html的字符串。对于Send-MailMessage cmdlet,您缺少参数
-Body
,如
-Body$Outputreport
中所示。此外,还应在此处添加
-ErrorAction Stop
,以便在
捕获
中排除非终止错误。