powershell arguments with nrpe

363 views Asked by At

I want to pass two arguements with a powershell-script.

This is the regular check

test = cmd /c echo scripts\test.ps1 ; exit($lastexitcode) | powershell.exe -command -

This is the idea that i want. To set warning and critical.

test = cmd /c echo scripts\test.ps1 -w 10 -c 50 ; exit($lastexitcode) | powershell.exe -command -

If warning is set to over 10 then it will return exit 1

If critical is set over 50 then it will return exit 2

Not sure howto do this in my script.

Here is how it looks now.

$condition = (Get-Service | Where-Object Status -eq "Running").Count

if ($argument warn) {
    Write-Output "Warning:" $condition
    exit 1 
}

ElseIf ($argument critical) {
        Write-Output "Critical:" $condition
        exit 2
}
2

There are 2 answers

9
Theo On BEST ANSWER

Although I'm not quite sure if this is what you mean, but you can start the script with a param() block so it accepts arguments like

param (
    [int]$WarningLevel = 0,
    [int]$CriticalLevel = 0
)

$condition = (Get-Service | Where-Object {$_.Status -eq "Running"}).Count

if ($CriticalLevel -gt 0 -and $condition -gt $CriticalLevel) {
    Write-Output "Critical: Way too many proc's: $condition"
    Exit 2
}
elseif ($WarningLevel -gt 0 -and  $condition -gt $WarningLevel) {
    Write-Output "Warning: proc's filling up: $condition"
    Exit 1 
}
Write-Output "All OK"
Exit 0
1
Shaqil Ismail On

You can use,

powershell -file "(Path to your PowerShell Script).ps1 -w10 -c50"

and inside your PowerShell script at the top, insert the following:

param ($w, $c)

and then use the variables in your script.

Learn more about named parameters, so you do not have to enter the arguments in order of them being used, and less chance of your script breaking when you want to make changes.