AB2112
AB2112

Reputation: 71

PowerShell script to look for number and if greater than send email

I am looking for a script that runs a command, reads the output and then if a number is greater than...send an email.

This is the code I have so far -

$Output = 'D:\test.data\QueuedJobss.txt'
d:
set-location -Path 'D:\program files\veritas\netbackup\bin\admincmd'
.\bpdbjobs -summary -L > $Output

$Queued = (Select-String -Pattern "Queued:\s+(\d+)" -Path $Output).Matches.Groups[1].Value

if ($Queued -gt 100 ) {
  $MailArgs = @{
        'To'          = '[email protected]'
        'From'        = '[email protected]'
        'Subject'     = 'Over 100 Queued Jobs!'
        'Attachments' = $Output
        'Body'        = 'Check Environment'

        'SmtpServer' = 'smtp.us.test.com'
       }
     Send-MailMessage @MailArgs
  }

It is currently returning an error -

}
Cannot index into a null array.
At line:6 char:81
+ $Queued = (Select-String -Pattern "Queued:\s+(\d+)" -Path $Output).Match.Groups[ <<<< 1].Value
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

The output that it's reading looks like -

Summary of jobs on ustestnbma01
Queued:                                1
Waiting-to-Retry:                        0
Active:                           5
Successful:                   25876
Partially Successful:           136
Failed:                         327
Incomplete:                       0
Suspended:                        0
Total:                        26345

The row I'm focused on is Queued, that number is followed by a lot of spaces.

Upvotes: 2

Views: 3711

Answers (2)

Adam Parsons
Adam Parsons

Reputation: 536

Is your code waiting for the job to finish before running - are you sure there is an output for the select-string cmdlet at the time that cmdlet runs? Perhaps try to replace with this instead:

If (Test-Path $Output)
{
    $Queued = (Select-String -Pattern "Queued:\s+(\d+)" -Path $Output).Matches.Groups[1].Value

    If ($Queued -gt 100 )
    {
        $MailArgs = @{
            'To'          = '[email protected]'
            'From'        = '[email protected]'
            'Subject'     = 'Over 100 Queued Jobs!'
            'Attachments' = $Output
            'Body'        = 'Check Environment'

            'SmtpServer' = 'smtp.us.test.com'
        }

        Send-MailMessage @MailArgs
    }
}
Else
{
    $MailArgs = @{
        'To'          = '[email protected]'
        'From'        = '[email protected]'
        'Subject'     = 'No output found!'
       #'Attachments' = $Output
        'Body'        = 'Check Environment'

        'SmtpServer' = 'smtp.us.test.com'
    }

    Send-MailMessage @MailArgs
}

I would suspect the data simply is not there when the cmdlet runs, as the regex, the resulting members and the methods invoked all work fine if you're testing against a file with that data in it.

If the data is simply not there, replace this:

.\bpdbjobs -summary -L > $Output

With this:

Start-Process -FilePath .\bpdbjobs -ArgumentList "-summary","-L","> $Output" -Wait

Alternatively, if the resulting members and methods are not working, you could try this:

$Queued = (Select-String -Pattern "Queued:\s+(\d+)" -Path $Output).Line.Split(" ")[-1]

Upvotes: 2

Victor Silva
Victor Silva

Reputation: 780

You need to fix the line of code, using matches instead of match:

(Select-String -Pattern "Queued:\s+(\d+)" -Path $Output).Matches.Groups[1].Value

Upvotes: 0

Related Questions