Hi,
I need to found a monitoring solution in order to run a script that reports new updates available on the WSUS Server.
Scripts are executed locally.
It works on 49 serveurs, but there’s one that gives me a hard time.
I get this error message.
Exception calling “Send” with “1” argument(s): “Failure to send the mail.”
At C:\Temp\script_notif.ps1:172 char:7
- $smtp.Send($msg)
-
- CategoryInfo : NotSpecified: ( , MethodInvocationException
- FullyQualifiedErrorId : SmtpException
Do you have any idea ?
Thank you for your help.
<span style="color: #000000"><span style="color: #ff8000">#########################################################
# #
# Monitoring Windows Updates and Pending Restarts #
# #
#########################################################
#########################################################
# List of computers to be monitored
#########################################################
</span><span style="color: #0000bb">$Servers </span><span style="color: #007700">= </span><span style="color: #dd0000">"10.254.x.x"
</span><span style="color: #ff8000">#########################################################
# List of users who will receive the report
#########################################################
</span><span style="color: #0000bb">$mailto </span><span style="color: #007700">= </span><span style="color: #dd0000">"x"
</span><span style="color: #ff8000">#########################################################
# SMTP properties
#########################################################
</span><span style="color: #0000bb">$emailFrom </span><span style="color: #007700">= </span><span style="color: #dd0000">"x"
</span><span style="color: #0000bb">$smtpServer </span><span style="color: #007700">= </span><span style="color: #dd0000">"x" </span><span style="color: #ff8000">#SMTP Server.
#$smtpUsername = "myUsername"
#$smtpPassword = "myPassword"
</span><span style="color: #0000bb">$results </span><span style="color: #007700">= foreach (</span><span style="color: #0000bb">$Computer in $Servers</span><span style="color: #007700">)
{
</span><span style="color: #0000bb">try
</span><span style="color: #007700">{
</span><span style="color: #0000bb">$service </span><span style="color: #007700">= </span><span style="color: #0000bb">Get</span><span style="color: #007700">-</span><span style="color: #0000bb">WmiObject Win32_Service </span><span style="color: #007700">-</span><span style="color: #0000bb">Filter </span><span style="color: #dd0000">'Name="wuauserv"' </span><span style="color: #007700">-</span><span style="color: #0000bb">ComputerName $Computer </span><span style="color: #007700">-</span><span style="color: #0000bb">Ea 0
$WUStartMode </span><span style="color: #007700">= </span><span style="color: #0000bb">$service</span><span style="color: #007700">.</span><span style="color: #0000bb">StartMode
$WUState </span><span style="color: #007700">= </span><span style="color: #0000bb">$service</span><span style="color: #007700">.</span><span style="color: #0000bb">State
$WUStatus </span><span style="color: #007700">= </span><span style="color: #0000bb">$service</span><span style="color: #007700">.</span><span style="color: #0000bb">Status
try</span><span style="color: #007700">{
if (</span><span style="color: #0000bb">Test</span><span style="color: #007700">-</span><span style="color: #0000bb">Connection </span><span style="color: #007700">-</span><span style="color: #0000bb">ComputerName $Computer </span><span style="color: #007700">-</span><span style="color: #0000bb">Count 1 </span><span style="color: #007700">-</span><span style="color: #0000bb">Quiet</span><span style="color: #007700">)
{
</span><span style="color: #ff8000">#check if the server is the same where this script is running
</span><span style="color: #007700">if(</span><span style="color: #0000bb">$Computer </span><span style="color: #007700">-</span><span style="color: #0000bb">eq </span><span style="color: #dd0000">"$env:computername.$env:userdnsdomain"</span><span style="color: #007700">)
{
</span><span style="color: #0000bb">$UpdateSession </span><span style="color: #007700">= New-</span><span style="color: #0000bb">Object </span><span style="color: #007700">-</span><span style="color: #0000bb">ComObject Microsoft</span><span style="color: #007700">.</span><span style="color: #0000bb">Update</span><span style="color: #007700">.</span><span style="color: #0000bb">Session
</span><span style="color: #007700">}
else { </span><span style="color: #0000bb">$UpdateSession </span><span style="color: #007700">= [</span><span style="color: #0000bb">activator</span><span style="color: #007700">]::</span><span style="color: #0000bb">CreateInstance</span><span style="color: #007700">([</span><span style="color: #0000bb">type</span><span style="color: #007700">]::</span><span style="color: #0000bb">GetTypeFromProgID</span><span style="color: #007700">(</span><span style="color: #dd0000">"Microsoft.Update.Session"</span><span style="color: #007700">,</span><span style="color: #0000bb">$Computer</span><span style="color: #007700">)) }
</span><span style="color: #0000bb">$UpdateSearcher </span><span style="color: #007700">= </span><span style="color: #0000bb">$UpdateSession</span><span style="color: #007700">.</span><span style="color: #0000bb">CreateUpdateSearcher</span><span style="color: #007700">()
</span><span style="color: #0000bb">$SearchResult </span><span style="color: #007700">= </span><span style="color: #0000bb">$UpdateSearcher</span><span style="color: #007700">.</span><span style="color: #0000bb">Search</span><span style="color: #007700">(</span><span style="color: #dd0000">"IsAssigned=1 and IsHidden=0 and IsInstalled=0"</span><span style="color: #007700">)
</span><span style="color: #0000bb">$Critical </span><span style="color: #007700">= </span><span style="color: #0000bb">$SearchResult</span><span style="color: #007700">.</span><span style="color: #0000bb">updates </span><span style="color: #007700">| </span><span style="color: #0000bb">where </span><span style="color: #007700">{ </span><span style="color: #0000bb">$_</span><span style="color: #007700">.</span><span style="color: #0000bb">MsrcSeverity </span><span style="color: #007700">-</span><span style="color: #0000bb">eq </span><span style="color: #dd0000">"Critical" </span><span style="color: #007700">}
</span><span style="color: #0000bb">$important </span><span style="color: #007700">= </span><span style="color: #0000bb">$SearchResult</span><span style="color: #007700">.</span><span style="color: #0000bb">updates </span><span style="color: #007700">| </span><span style="color: #0000bb">where </span><span style="color: #007700">{ </span><span style="color: #0000bb">$_</span><span style="color: #007700">.</span><span style="color: #0000bb">MsrcSeverity </span><span style="color: #007700">-</span><span style="color: #0000bb">eq </span><span style="color: #dd0000">"Important" </span><span style="color: #007700">}
</span><span style="color: #0000bb">$other </span><span style="color: #007700">= </span><span style="color: #0000bb">$SearchResult</span><span style="color: #007700">.</span><span style="color: #0000bb">updates </span><span style="color: #007700">| </span><span style="color: #0000bb">where </span><span style="color: #007700">{ </span><span style="color: #0000bb">$_</span><span style="color: #007700">.</span><span style="color: #0000bb">MsrcSeverity </span><span style="color: #007700">-</span><span style="color: #0000bb">eq $null </span><span style="color: #007700">}
</span><span style="color: #ff8000"># Get windows updates counters
</span><span style="color: #0000bb">$totalUpdates </span><span style="color: #007700">= $(</span><span style="color: #0000bb">$SearchResult</span><span style="color: #007700">.</span><span style="color: #0000bb">updates</span><span style="color: #007700">.</span><span style="color: #0000bb">count</span><span style="color: #007700">)
</span><span style="color: #0000bb">$totalCriticalUp </span><span style="color: #007700">= $(</span><span style="color: #0000bb">$Critical</span><span style="color: #007700">.</span><span style="color: #0000bb">count</span><span style="color: #007700">)
</span><span style="color: #0000bb">$totalImportantUp </span><span style="color: #007700">= $(</span><span style="color: #0000bb">$Important</span><span style="color: #007700">.</span><span style="color: #0000bb">count</span><span style="color: #007700">)
if(</span><span style="color: #0000bb">$totalUpdates </span><span style="color: #007700">-</span><span style="color: #0000bb">gt 0</span><span style="color: #007700">)
{
</span><span style="color: #0000bb">$updatesToInstall </span><span style="color: #007700">= </span><span style="color: #0000bb">$true
</span><span style="color: #007700">}
else { </span><span style="color: #0000bb">$updatesToInstall </span><span style="color: #007700">= </span><span style="color: #0000bb">$false </span><span style="color: #007700">}
}
else
{
</span><span style="color: #ff8000"># if cannot connected to the server the updates are listed as not defined
</span><span style="color: #0000bb">$totalUpdates </span><span style="color: #007700">= </span><span style="color: #dd0000">"nd"
</span><span style="color: #0000bb">$totalCriticalUp </span><span style="color: #007700">= </span><span style="color: #dd0000">"nd"
</span><span style="color: #0000bb">$totalImportantUp </span><span style="color: #007700">= </span><span style="color: #dd0000">"nd"
</span><span style="color: #007700">}
}
</span><span style="color: #0000bb">catch
</span><span style="color: #007700">{
</span><span style="color: #ff8000"># if an error occurs the updates are listed as not defined
</span><span style="color: #0000bb">Write</span><span style="color: #007700">-</span><span style="color: #0000bb">Warning </span><span style="color: #dd0000">"$Computer`: $_"
</span><span style="color: #0000bb">$totalUpdates </span><span style="color: #007700">= </span><span style="color: #dd0000">"nd"
</span><span style="color: #0000bb">$totalCriticalUp </span><span style="color: #007700">= </span><span style="color: #dd0000">"nd"
</span><span style="color: #0000bb">$totalImportantUp </span><span style="color: #007700">= </span><span style="color: #dd0000">"nd"
</span><span style="color: #0000bb">$updatesToInstall </span><span style="color: #007700">= </span><span style="color: #0000bb">$false
</span><span style="color: #007700">}
</span><span style="color: #ff8000"># Querying WMI for build version
</span><span style="color: #0000bb">$WMI_OS </span><span style="color: #007700">= </span><span style="color: #0000bb">Get</span><span style="color: #007700">-</span><span style="color: #0000bb">WmiObject </span><span style="color: #007700">-Class </span><span style="color: #0000bb">Win32_OperatingSystem </span><span style="color: #007700">-</span><span style="color: #0000bb">Property BuildNumber</span><span style="color: #007700">, </span><span style="color: #0000bb">CSName </span><span style="color: #007700">-</span><span style="color: #0000bb">ComputerName $Computer </span><span style="color: #007700">-</span><span style="color: #0000bb">Authentication PacketPrivacy </span><span style="color: #007700">-</span><span style="color: #0000bb">Impersonation Impersonate
</span><span style="color: #ff8000"># Making registry connection to the local/remote computer
</span><span style="color: #0000bb">$RegCon </span><span style="color: #007700">= [</span><span style="color: #0000bb">Microsoft</span><span style="color: #007700">.</span><span style="color: #0000bb">Win32</span><span style="color: #007700">.</span><span style="color: #0000bb">RegistryKey</span><span style="color: #007700">]::</span><span style="color: #0000bb">OpenRemoteBaseKey</span><span style="color: #007700">([</span><span style="color: #0000bb">Microsoft</span><span style="color: #007700">.</span><span style="color: #0000bb">Win32</span><span style="color: #007700">.</span><span style="color: #0000bb">RegistryHive</span><span style="color: #007700">]</span><span style="color: #dd0000">"LocalMachine"</span><span style="color: #007700">,</span><span style="color: #0000bb">$Computer</span><span style="color: #007700">)
</span><span style="color: #ff8000"># If Vista/2008 & Above query the CBS Reg Key
</span><span style="color: #007700">If (</span><span style="color: #0000bb">$WMI_OS</span><span style="color: #007700">.</span><span style="color: #0000bb">BuildNumber </span><span style="color: #007700">-</span><span style="color: #0000bb">ge 6001</span><span style="color: #007700">)
{
</span><span style="color: #0000bb">$RegSubKeysCBS </span><span style="color: #007700">= </span><span style="color: #0000bb">$RegCon</span><span style="color: #007700">.</span><span style="color: #0000bb">OpenSubKey</span><span style="color: #007700">(</span><span style="color: #dd0000">"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\"</span><span style="color: #007700">).</span><span style="color: #0000bb">GetSubKeyNames</span><span style="color: #007700">()
</span><span style="color: #0000bb">$CBSRebootPend </span><span style="color: #007700">= </span><span style="color: #0000bb">$RegSubKeysCBS </span><span style="color: #007700">-</span><span style="color: #0000bb">contains </span><span style="color: #dd0000">"RebootPending"
</span><span style="color: #007700">}
else{
</span><span style="color: #0000bb">$CBSRebootPend </span><span style="color: #007700">= </span><span style="color: #0000bb">$false
</span><span style="color: #007700">}
</span><span style="color: #ff8000"># Query WUAU from the registry
</span><span style="color: #0000bb">$RegWUAU </span><span style="color: #007700">= </span><span style="color: #0000bb">$RegCon</span><span style="color: #007700">.</span><span style="color: #0000bb">OpenSubKey</span><span style="color: #007700">(</span><span style="color: #dd0000">"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\"</span><span style="color: #007700">)
</span><span style="color: #0000bb">$RegSubKeysWUAU </span><span style="color: #007700">= </span><span style="color: #0000bb">$RegWUAU</span><span style="color: #007700">.</span><span style="color: #0000bb">GetSubKeyNames</span><span style="color: #007700">()
</span><span style="color: #0000bb">$WUAURebootReq </span><span style="color: #007700">= </span><span style="color: #0000bb">$RegSubKeysWUAU </span><span style="color: #007700">-</span><span style="color: #0000bb">contains </span><span style="color: #dd0000">"RebootRequired"
</span><span style="color: #007700">If(</span><span style="color: #0000bb">$CBSRebootPend –OR $WUAURebootReq</span><span style="color: #007700">)
{
</span><span style="color: #0000bb">$machineNeedsRestart </span><span style="color: #007700">= </span><span style="color: #0000bb">$true
</span><span style="color: #007700">}
else
{
</span><span style="color: #0000bb">$machineNeedsRestart </span><span style="color: #007700">= </span><span style="color: #0000bb">$false
</span><span style="color: #007700">}
</span><span style="color: #ff8000"># Closing registry connection
</span><span style="color: #0000bb">$RegCon</span><span style="color: #007700">.</span><span style="color: #0000bb">Close</span><span style="color: #007700">()
if(</span><span style="color: #0000bb">$machineNeedsRestart </span><span style="color: #007700">-or </span><span style="color: #0000bb">$updatesToInstall </span><span style="color: #007700">-or (</span><span style="color: #0000bb">$WUStartMode </span><span style="color: #007700">-</span><span style="color: #0000bb">eq </span><span style="color: #dd0000">"Manual"</span><span style="color: #007700">) -or (</span><span style="color: #0000bb">$totalUpdates </span><span style="color: #007700">-</span><span style="color: #0000bb">eq </span><span style="color: #dd0000">"nd"</span><span style="color: #007700">))
{
New-</span><span style="color: #0000bb">Object PSObject </span><span style="color: #007700">-</span><span style="color: #0000bb">Property </span><span style="color: #007700">@{
</span><span style="color: #0000bb">Computer </span><span style="color: #007700">= </span><span style="color: #0000bb">$WMI_OS</span><span style="color: #007700">.</span><span style="color: #0000bb">CSName
WindowsUpdateStatus </span><span style="color: #007700">= </span><span style="color: #0000bb">$WUStartMode </span><span style="color: #007700">+ </span><span style="color: #dd0000">"/" </span><span style="color: #007700">+ </span><span style="color: #0000bb">$WUState </span><span style="color: #007700">+ </span><span style="color: #dd0000">"/" </span><span style="color: #007700">+ </span><span style="color: #0000bb">$WUStatus
UpdatesToInstall </span><span style="color: #007700">= </span><span style="color: #0000bb">$updatesToInstall
TotalOfUpdates </span><span style="color: #007700">= </span><span style="color: #0000bb">$totalUpdates
TotalOfCriticalUpdates </span><span style="color: #007700">= </span><span style="color: #0000bb">$totalCriticalUp
TotalOfImportantUpdates </span><span style="color: #007700">= </span><span style="color: #0000bb">$totalImportantUp
RebootPending </span><span style="color: #007700">= </span><span style="color: #0000bb">$machineNeedsRestart
</span><span style="color: #007700">}
}
}
</span><span style="color: #0000bb">Catch
</span><span style="color: #007700">{
</span><span style="color: #0000bb">Write</span><span style="color: #007700">-</span><span style="color: #0000bb">Warning </span><span style="color: #dd0000">"$Computer`: $_"
</span><span style="color: #007700">}
}
</span><span style="color: #ff8000">#########################################################
# Formating result
#########################################################
</span><span style="color: #0000bb">$tableFragment </span><span style="color: #007700">= </span><span style="color: #0000bb">$results </span><span style="color: #007700">| </span><span style="color: #0000bb">ConvertTo</span><span style="color: #007700">-</span><span style="color: #0000bb">HTML </span><span style="color: #007700">-</span><span style="color: #0000bb">fragment
</span><span style="color: #ff8000"># HTML Format for Output
</span><span style="color: #0000bb">$HTMLmessage </span><span style="color: #007700">= @</span><span style="color: #dd0000">"
<font color=""black"" face=""Arial"" size=""3"">
<h1 style='font-family:arial;'><b>Rapport des majs en attentes et/ou d'un redemarrage en attente</b>
</h1>
<p style='font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'>Ce rapport a été
généré car il y a des updates en attentes d'installation et/ou d'un reboot en attente sur ce
serveur. Merci de suivre la procédure : http://doc4tme.all4it.local/doku.php/clients/a4i_it/system/
majs_4cliwinrdpgwa .</p>
<br><br>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;}
table{width:80%;}
thead{}
thead th{font-size:120%;text-align:left;}
th{border-bottom:2px solid rgb(79,129,189);border-top:2px solid rgb(79,129,189);padding-bottom:10px;
padding-top:10px;}
tr{padding:10px 10px 10px 10px;border:none;}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
$tableFragment
</body>
"</span><span style="color: #007700">@
</span><span style="color: #ff8000">#########################################################
# Validation and sending email
#########################################################
# Regular expression to get what's inside of <td>'s
</span><span style="color: #0000bb">$regexsubject </span><span style="color: #007700">= </span><span style="color: #0000bb">$HTMLmessage
$regex </span><span style="color: #007700">= [</span><span style="color: #0000bb">regex</span><span style="color: #007700">] </span><span style="color: #dd0000">'(?im)<td>'
</span><span style="color: #ff8000"># If you have data between <td>'s then you need to send the email
</span><span style="color: #007700">if (</span><span style="color: #0000bb">$regex</span><span style="color: #007700">.</span><span style="color: #0000bb">IsMatch</span><span style="color: #007700">(</span><span style="color: #0000bb">$regexsubject</span><span style="color: #007700">)) {
</span><span style="color: #0000bb">$smtp </span><span style="color: #007700">= New-</span><span style="color: #0000bb">Object Net</span><span style="color: #007700">.</span><span style="color: #0000bb">Mail</span><span style="color: #007700">.</span><span style="color: #0000bb">SmtpClient </span><span style="color: #007700">-</span><span style="color: #0000bb">ArgumentList $smtpServer
</span><span style="color: #ff8000">#$smtp.credentials = New-Object System.Net.NetworkCredential($smtpUsername, $smtpPassword);
</span><span style="color: #0000bb">$msg </span><span style="color: #007700">= New-</span><span style="color: #0000bb">Object Net</span><span style="color: #007700">.</span><span style="color: #0000bb">Mail</span><span style="color: #007700">.</span><span style="color: #0000bb">MailMessage
$msg</span><span style="color: #007700">.</span><span style="color: #0000bb">From </span><span style="color: #007700">= </span><span style="color: #0000bb">$emailFrom
$msg</span><span style="color: #007700">.</span><span style="color: #0000bb">To</span><span style="color: #007700">.</span><span style="color: #0000bb">Add</span><span style="color: #007700">(</span><span style="color: #0000bb">$mailto</span><span style="color: #007700">)
</span><span style="color: #0000bb">$msg</span><span style="color: #007700">.</span><span style="color: #0000bb">Subject </span><span style="color: #007700">= </span><span style="color: #dd0000">"Update et/ou reboot en attente sur $computer"
</span><span style="color: #0000bb">$msg</span><span style="color: #007700">.</span><span style="color: #0000bb">IsBodyHTML </span><span style="color: #007700">= </span><span style="color: #0000bb">$true
$msg</span><span style="color: #007700">.</span><span style="color: #0000bb">Body </span><span style="color: #007700">= </span><span style="color: #0000bb">$HTMLmessage
$smtp</span><span style="color: #007700">.</span><span style="color: #0000bb">Send</span><span style="color: #007700">(</span><span style="color: #0000bb">$msg</span><span style="color: #007700">)
}</span>
</span>