从 Windows 服务器通过电子邮件发送 CPU 和内存利用率时出错

从 Windows 服务器通过电子邮件发送 CPU 和内存利用率时出错

我可以生成笔记本电脑的 CPU 和内存利用率,并将它们通过电子邮件发送到我的电子邮件地址。当我尝试对 Windows Server 2008 执行相同操作时,我得到了

“new-object 无法加载 com 类型 outlook.application”。

Windows Server 未安装 Outlook。执行此脚本是否需要 Outlook?

## This is the location the script will save the output file

$OutputFile="C:\admin\ServerStatus.htm"     

## Replace these values with valid from and to email addresses

$smtpFrom = "emailaddress"

$smtpTo = "emailaddress"

$CPU = Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

$Mem = gwmi -Class win32_operatingsystem |

Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}

$Outputreport = "Test Server     

Average CPU = $($CPU.Average)%

Memory Used = $($MEM.MemoryUsage)%"

$Outputreport | out-file $OutputFile

$ol = New-Object -comObject Outlook.Application 
$mail = $ol.CreateItem(0) 
$Mail.Recipients.Add("emailaddress") 
$Mail.Subject = "PS1 Script TestMail" 
$Mail.Body += Get-Content $OutputFile

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$Mail.Send() 

答案1

32 位 Powershell 需要 32 位 Outlook 对象,而 64 位 Powershell 需要 64 位 Outlook。

是的,您确实需要安装 Outlook 才能运行该脚本。

编辑:实际上,您需要的是 Outlook 附带的 MAPI(邮件 API)。

如果你有可用的 SMTP 服务器,你可以Send-MailMessage按照说明使用这里

答案2

做一些更棒的事情,

$computername="DC-server", "Hp-pc"

$AVGProc = Get-WmiObject -computername $computername win32_processor |  
Measure-Object -property LoadPercentage -Average | Select Average 
$OS = gwmi -Class win32_operatingsystem -computername $computername | 
Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }} 
#$vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" | 
#Select-object @{Name = "C PercentFree"; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } } 

$result += [PSCustomObject] @{  
        ServerName = "$computername" 
        CPULoad = "$($AVGProc.Average)%" 
        MemLoad = "$($OS.MemoryUsage)%" 
        #CDrive = "$($vol.'C PercentFree')%" 
    } 

    $Outputreport = "<HTML><TITLE> Server Health Report </TITLE> 
                     <BODY background-color:peachpuff> 
                     <font color =""#99000"" face=""Microsoft Tai le""> 
                     <H2> Server Health Report </H2></font> 
                     <Table border=1 cellpadding=0 cellspacing=0> 
                     <TR bgcolor=gray align=center> 
                       <TD><B>Server Name</B></TD> 
                       <TD><B>Avrg.CPU Utilization</B></TD> 
                       <TD><B>Memory Utilization</B></TD> " 

    Foreach($Entry in $Result)  

        {  
          if((($Entry.CpuLoad) -or ($Entry.memload)) -ge "80")  
          {  
            $Outputreport += "<TR bgcolor=red>"  
          }  
          else 
           { 
            $Outputreport += "<TR>"  
          } 
          $Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>"  
        } 
     $Outputreport += "</Table></BODY></HTML>"  


$Outputreport | out-file C:\Test.htm 

$smtpServer = "yoursmtpserver.com" 
$smtpFrom = "[email protected]" 
$smtpTo = "[email protected]" 
$messageSubject = "Servers Health report" 
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto 
$message.Subject = "PS1 Script TestMail"
$message.IsBodyHTML = $true 
$message.Body = "<head><pre>$style</pre></head>" 
$message.Body += Get-Content C:\test.htm 
$smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
$smtp.Send($message)

https://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-to-Get-78687c5e

相关内容