Page 1 of 1

Add arguments

Posted: Fri Nov 25, 2016 3:12 pm
by ruffsense
I have created a script for nagios but now i want to change the $folder and $file $MaxAgeMinutes into $args[0] - $args[1] - $args[2].

So i can pass arguments in my nsclient.ini

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

Code: Select all

$folder = 'c:\temp\'
$file = 'CGO.csv'
$MaxAgeMinutes = 1440

# Process Limit
$Limit = (Get-Date).AddMinutes((-1 * $MaxAgeMinutes))

$path = "$folder\$file"
if (Test-Path $path)
{
	if ((Get-Item $Path).LastWriteTime -lt $limit)
	{
		echo "WARNING status - $file is older than 24 hours."
		exit 1 #returns warning status
	}
	else
	{
		echo "OK status – file is OK"
		exit 0 #Return OK status
	}
}
else
{
	echo "CRITICAL status – file $file doesn’t exist"
	exit 2 #returns critical status
}

Re: Add arguments

Posted: Mon Nov 28, 2016 2:32 pm
by mcapra
Using args in powershell:
http://stackoverflow.com/questions/1232 ... powershell

Proof of concept code:

Code: Select all

Write-Host "here's arg 0: $($args[0])"
Write-Host "here's arg 1: $($args[1])"
Write-Host "here's arg 2: $($args[2])"

Code: Select all

PS C:\> ./test.ps1 one two three
here's arg 0: one
here's arg 1: two
here's arg 2: three

Re: Add arguments

Posted: Mon Nov 28, 2016 3:45 pm
by ruffsense
thnx.

If any one is looking for a check_file powershell script with arguments that checks if a file excist or is older then 24ours. Here you go.

Code: Select all

function check_file()
{
$folder = "$($args[0])"
$file = "$($args[1])"
$MaxAgeMinutes = 1440

# Process Limit
$Limit = (Get-Date).AddMinutes((-1 * $MaxAgeMinutes))

$path = "$folder\$file"
if (Test-Path $path)
{
   if ((Get-Item $Path).LastWriteTime -lt $limit)
   {
      echo "WARNING status - $file is older than 24 hours."
      exit 1 #returns warning status
   }
   else
   {
      echo "OK status – file is OK"
      exit 0 #Return OK status
   }
}
else
{
   echo "CRITICAL status – file $file doesn’t exist"
   exit 2 #returns critical status
}
}

Re: Add arguments

Posted: Mon Nov 28, 2016 4:31 pm
by dwhitfield
Thanks so much for posting!

It looks like your issue is resolved. Is that correct? Is it ok to lock the thread?

Re: Add arguments

Posted: Mon Nov 28, 2016 5:20 pm
by ruffsense
you can lock it.:)