What is wrong with my script.

Im creatin' a script for nagios. It needs to warn me when the file is older then 24hr and show critical if the file does not exsist.

$age="1440"
$file=c:\temp\
$filename='CGO.csv'
$time = (get-childitem $file\$filename).LastWriteTime
if(Test-Path $file\$filename) 
 {
 echo OK status  file is OK
exit 0 #Return OK status
 }
 ElseIf ($time -lt (get-date).AddMinutes(-$age))
 {
 echo "WARNING status - $filename is older than 24 hours. Last write was $time"
 exit 1 #returns warning status
 }
 else
 {
 echo CRITICAL status  file $filename doesnt exist
exit 2 #returns critical status
 }

It is not working. Can anyone help me out.

February 20th, 2015 12:57pm

Hi,

first of all, in line 4 you use the Get-ChildItem cmdlet. That's the new implementation of dir and works on a single file, but if you want a single specific item, try using Get-Item instead. You may get used to it and use Get-Childitem on a folder some day when you want the folder itself.

then you may want to replace all instances of $file\$filename with "$file\$filename". Just to make sure.

Finally, your if/else will not work properly, as if the file exists it will exit with 0 and not consider the elseif at all (so time will not be considered). You can move the elseif part into the if block.

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 1:10pm

Can you show me a good example. Im learning powershell.
February 20th, 2015 1:13pm

Hi,

sure I can show you a workable if/else structure:

if ($Condition1)
{
	if ($Condition2)
	{
		# Warning
	}
	else
	{
		# Ok
	}
}
else
{
	# Critical
}

furthermore, about calculating the time-limit, try this:

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

then you can later compare the timestamp on the file with the $Limit (Which makes the if-clause more readable).

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 1:22pm

Is this good.

$folder=c:\temp\
$file='CGO.csv'
$MaxAgeMinutes = 1440
$Limit = (Get-Date).AddMinutes( (-1 * $MaxAgeMinutes) )
$path = (Get-Item ("$folder\$file"))
if (Test-Path $path)
{
	if ($Condition2)
	{
	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 doesnt exist
exit 2 #returns critical status
}

I don't know what to add in condition2.

February 20th, 2015 1:49pm

Too much code and too much logic.  Just do it one condition at a time:

$file='c:\temp\CGO.csv'
if(-not (Test-Path $file)){
	echo "CRITICAL status  file $filename doesnt exist"
    exit 2
}
if(($fi=Get-ChildItem $file).LastWriteTime -lt [DateTime]::Now.AddDays(-1)){
	echo "WARNING status - $filename is older than 24 hours. Last write was $($fi.LastWriteTime)"
 	exit 1 #returns warning status
}else{
	exit 0
}
Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 2:16pm

Hi cvanaxel,

looking good. Here's a slight revision:

# Parameters
$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 doesnt exist"
	exit 2 #returns critical status
}

Points to note:

- I did not call get-item until after testing the path (It'll throw an error if there is no item)
- Make sure not to use typographical apostrophes.

Cheers,
Fred

February 20th, 2015 3:53pm

Is this also possible?

$folder = '$args[0]'
$file = '$args[1]'
$MaxAgeMinutes = $args[2]

# 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 doesnt exist"
	#exit 2 #returns critical status
}

$args[?] Will this help when i add this for nagios?

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 7:41pm

Hi Cvanaxel,

you can indeed use this to accept arguments from Nagios.

However you'd need to get rid of the single-quotes from the first two lines. Simply put, there are two ways to write text in PowerShell: single-quotes and double-quotes. PowerShell will interprete strings in double-quotes, however single-quote wrapped strings will be taken literally. Here a quick example you can paste into your console to see the difference:

$Answer = 42
"The double-quote answer is: $Answer"
'The single-quote answer is: $Answer'

Cheers,

February 21st, 2015 1:06am

I tried that but im stuck with the $folder part. It won't work. It probaly has problem / in it.
Free Windows Admin Tool Kit Click here and download it now
February 21st, 2015 2:33am

I get this error:

Cannot convert value "$" to type "System.Int32". Error: "Input string was not i
n a correct format."
At C:\Program Files\NSClient++\scripts\check_file.ps1:15 char:1
+ $Limit = (Get-Date).AddMinutes((-1 * $MaxAgeMinutes))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

February 22nd, 2015 1:33am

Try{   $MaxAgeMinutes =[int]$args[2]
}
Catch{
     Write-Error $_
     exit
}


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

Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 1:49am

$folder = '$args[0]'
$file = '$args[1]'
{$MaxAgeMinutes = [int]$args[2]
}
Catch{
 Write-Error $_
 exit
}
# Process Limit
$Limit = (Get-Date).AddMinutes(-$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 doesnt exist"
	#exit 2 #returns critical status
}

what is the code. something like this?
February 22nd, 2015 1:56am

You didn't copy it correctly.  Try again.
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 1:59am

This still works better:

$file='c:\temp\CGO.csv'
if(-not (Test-Path $file)){
	echo "CRITICAL status  file $filename doesnt exist"
    exit 2
}
if(($fi=Get-ChildItem $file).LastWriteTime -lt [DateTime]::Now.AddDays(-1)){
	echo "WARNING status - $filename is older than 24 hours. Last write was $($fi.LastWriteTime)"
 	exit 1 #returns warning status
}else{
	exit 0
}

February 22nd, 2015 2:00am

Nope still get an error

C:\Program Files\NSClient++\scripts\check_file.ps1 : Cannot convert value "$" t
o type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ scripts\check_file.ps1 $ARG1$ $ARG2$ $ARG3$; exit ($lastexitcode)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorExcep
   tion
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio
   n,check_file.ps1|

Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 2:05am

Why are you passing a dollar sign in the argument.  Perhaps you need to review how script arguments are passed.

You also don't explain how you are calling this script.  That is where your big problem is.

February 22nd, 2015 2:33am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics