Help for Powershell Newbie!

Welcome Forums General PowerShell Q&A Help for Powershell Newbie!

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

 
Participant
6 days, 20 hours ago.

  • Author
    Posts
  • #182301

    Participant
    Topics: 1
    Replies: 1
    Points: 15
    Rank: Member

    Hi all,

    Apologies if this sounds very dull but as a PowerShell newbie I am not sure how to search for answers on the web so thought i'd put it to the community here.

    Basically, i have a script that i would like to run daily to check for backup results and output the results to the event log and to the PowerShell window.

    I have it running presently and it gets the correct results and outputs them via 'write-host' correctly but when writing the data to the event log it will only read the first result and bypasses the others.

    My code looks like the below...

    New-EventLog -LogName Application -Source "VeeamResults" -ErrorAction SilentlyContinue > $null
    
    Add-PSSnapin VeeamPSSnapin
    
    $VbrJobs = Get-VBRJob | Sort-Object typetostring, name
    
    Foreach($Job in $VbrJobs)
    {
    $JobName = $Job.Name
    $Result = $Job.GetLastResult()
    
    write-host $Job.Name finished with $Job.GetLastResult()
    
    if($Job.GetLastResult() = "Success"){
    Write-EventLog -logname Application -Source "VeeamResults" -EntryType Information -EventId 7777 -Message "All Veeam Jobs Succeeded"
    }elseif($Job.GetLastResult() = "Warning") {
    Write-EventLog -logname Application -Source "VeeamResults" -EntryType Warning -EventId 6666 -Message "Veeam Warnings Detected"
    }elseif($Job.GetLastResult() = "Failed") {
    Write-EventLog -logname Application -Source "VeeamResults" -EntryType Error -EventId 5555 -Message "Veeam Failures Detected"
    }
    }

    The write-host output looks like this...

    Backup to NAS finished with Success
    DC02 Replication to Host 3 finished with Success
    Replication to Host 3 finished with Warning

    Apologies if its a bit of a mess, this is my first attempt with scripting.

    If anyone can help me achieve the write-eventlog commands to look at each of the jobs and output accordingly i would be very grateful.

    If anymore more information is needed from please please don't hesitate to give me a nudge

    Thanks in advance

    Huw

     

  • #182400

    Participant
    Topics: 10
    Replies: 84
    Points: 273
    Helping Hand
    Rank: Contributor

    A couple things to consider:

    1. Using Write-Host does just that; the output is directed to the current host, nowhere else, even if you save the Write-Host output to a variable and call it, it will return nothing. To use the output anywhere, use Write-Output.
    2. In the if/elseif statements, PowerShell doesn't understand = as an 'equal to' comparison operator. You need to use -eq. Check out https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-6
    ...
    if ($Job.GetLastResult() -eq 'Success') {
    ...
  • #182403

    Participant
    Topics: 10
    Replies: 84
    Points: 273
    Helping Hand
    Rank: Contributor

    You could also use a switch statement instead of if/elseif to make things little cleaner:

    switch ($Job.GetLastResult()) {
        Success {
            $entryType = 'Information'
            $eventId   = 7777
            $message   = 'All Veeam Jobs Succeeded'
        }
        Warning {
            $entryType = 'Warning'
            $eventId   = 6666
            $message   = 'Veeam Warnings Detected'
        }
        Error {
            $entryType = 'Error'
            $eventId   = 55555
            $message   = 'Veeam Failures Detected'
        }
    }
    
    try {
        Write-EventLog -Name Application -Source "Veeam Results" -EntryType $entryType -EventId $eventId -Message $message
    }
    catch {
        # do some error handling
    }
  • #182655

    Participant
    Topics: 1
    Replies: 1
    Points: 15
    Rank: Member

    Hi Aaron,

    Thank you for taking the time to explain the areas i had not got right, it's very much appreciated.

    Using the correct comparison operator has worked a treat, it's amazing how such a fundamental feature of PowerShell scripting had passed me by

    Thank you again.

    Huw

You must be logged in to reply to this topic.