Conditional loop for monitoring WSUS

Hello,

I use this script to monitor for WSUS
https://gallery.technet.microsoft.com/scriptcenter/Windows-Updates-and-684c355c

The problem is that I receive an email even if there are no updates available.

I would like to make sure if possible not to send an email if the variable $totalUpdates is equivalent to 0.

I have tried this at the end

[pre]if ($totalUpdates.count -ne 0) {
$sendMail = $smtp.Send($msg)
$sendMail = $true
}

else {$sendMail = $false}[/pre]

But it doesn’t work, can you please help me?

This is because your var isn’t 0

see the difference in this code

 

[pre]

$t =‘’
$t.count

$r= $null
$r.count

[/pre]

Thanks for the feedback.

I am a student, I discovered Powershell 8 months ago so I am a beginner.

You ask me to see the difference between the two names of the variable, but I can’t understand what $r = $null represents

When I try this code, it returns either 0 or 1.

Can you briefly explain or direct me to documentation, please?

[quote quote=156606]Thanks for the feedback.

I am a student, I discovered Powershell 8 months ago so I am a beginner.

You ask me to see the difference between the two names of the variable, but I can’t understand what $r = $null represents

When I try this code, it returns either 0 or 1.

Can you briefly explain or direct me to documentation, please?

[/quote]

Not the name :slight_smile:

i can do the same name for both.

what i wanted to show that even an empty var ‘’ has a count of 1 because there is an empty object in there,

if you create the var with $null its created but without an object in there then its 0.

the .count is normally used for an array of abject.

 

Can you show what there is in your $totalUpdates var ?

Thank you for the explanations, I understand better.

[pre]$totalUpdates = $($SearchResult.updates.count)[/pre]

But I found the solution on the another forum

# If you have data between td's then you need to send the email`

`if ($regex.IsMatch($regexsubject)){`

`    if ($totalUpdates -ne 0) {
   $smtp = New-Object Net.mail.SmtpClient -ArgumentList $smtpServer
   $smtp.Credentials = New-Object System.Net.NetworkCredential($smtpUsername, $smtpPassword);
   $msg = New-Object Net.mail.MailMessage
   $msg.From = $emailFrom
   $msg.To.Add($mailto)
   $msg.subject ="Update et/ou reboot en attente sur $computer"
   $msg.IsBodyHtml = $true
   $msg.Body = $HTMLmessage
   $smtp.Send($msg)
}
  }

[quote quote=156666][/quote]
Good to hear you have a solution :slight_smile:

one little comment lose the back ticks :slight_smile:

A bunny just died :frowning:

Yeppers, on the backticks on what you have. They have their place, jus not how you are using them.

PowerShell has many natural line continuation possibilities, backtick, in most case should be the very last option to use, but that does not mean they should not be used. Dispute what this write up says:

Bye Bye Backtick: Natural Line Continuations in PowerShell

Most of this article is on the money though.

Also, semi-colons at the end don’t do anything. Semi-colons in PS say, the code before and after it have nothing to do with each other. I see this a lot from folks coming to PS from other languages. They are not really a thing in PS proper, but like backtick, they do have a use case in some cases, but not in what you are doing.

Yet, it’s all about choice, habit and style.

Since you are learning, see these:

Windows PowerShell Survival Guide

PowerShellPracticeAndStyle

Powershell - Recommended coding style

What is the recommended coding style for PowerShell?
https://stackoverflow.com/questions/2025989/what-is-the-recommended-coding-style-for-powershell

Really interesting, thank you !
I will take the time to read all of this.

Have a good weekend.