我可以生成我的笔记本电脑的CPU和内存利用率,并通过电子邮件发送给我的电子邮件 当我尝试为Windows Server 2008做同样的事情时,我得到了
“新对象无法加载comtypesoutlook.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()
32位Powershell需要32位Outlook对象,64位PowerShell需要64位Outlook。
所以是的,你需要安装Outlook来运行脚本。
编辑:其实,你需要MAPI(邮件API)。 随Outlook附带。
如果您有可用的SMTP服务器,则可以使用Send-MailMessage ,如此处所述
做更棒的事情,
$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