Conditional loop for monitoring WSUS

Welcome Forums General PowerShell Q&A Conditional loop for monitoring WSUS

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
1 week, 2 days ago.

  • Author
    Posts
  • #156582

    Participant
    Topics: 3
    Replies: 12
    Points: 75
    Rank: Member

    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

    if ($totalUpdates.count -ne 0) {
    $sendMail = $smtp.Send($msg)
    $sendMail = $true
    }
    
    else {$sendMail = $false}

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

  • #156597

    Participant
    Topics: 3
    Replies: 42
    Points: 278
    Helping Hand
    Rank: Contributor

    This is because your var isn't 0

    see the difference in this code

     

    
    $t ="
    $t.count
    
    $r= $null
    $r.count
    
    
  • #156606

    Participant
    Topics: 3
    Replies: 12
    Points: 75
    Rank: Member

    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?

    • #156617

      Participant
      Topics: 3
      Replies: 42
      Points: 278
      Helping Hand
      Rank: Contributor

      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?

      Not the name 🙂

      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 ?

  • #156666

    Participant
    Topics: 3
    Replies: 12
    Points: 75
    Rank: Member

    Thank you for the explanations, I understand better.

    $totalUpdates = $($SearchResult.updates.count)

    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)
    }
      }
  • #156668

    Participant
    Topics: 3
    Replies: 42
    Points: 278
    Helping Hand
    Rank: Contributor

    Good to hear you have a solution 🙂

    one little comment lose the back ticks 🙂

    A bunny just died 🙁

  • #157034

    Participant
    Topics: 2
    Replies: 938
    Points: 1,634
    Helping Hand
    Rank: Community Hero

    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
    https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html

    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
    https://social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx

    PowerShellPracticeAndStyle
    https://github.com/PoshCode/PowerShellPracticeAndStyle

    PowerShell scripting best practices

    Powershell – Recommended coding style
    https://lazywinadmin.com/2011/06/powershell-recommended-coding-style.html

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

  • #157128

    Participant
    Topics: 3
    Replies: 12
    Points: 75
    Rank: Member

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

    Have a good weekend.

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort