Hi,
I have a powershell script which runs just fine from the host command line. But I am using a monitoring system (Icinga) that can execute te script remotely with NRPE. Now when I run the script remotely it gives me this error:
Cannot convert value "$" to type "System.Int32". Error: "Input string was not in a correct format." At C:\Program Files\NSClient++\scripts\check_csv_freespace.ps1:34 char:1+ $warnvalue = [math]::truncate(($capacity / 100) * $warnlevel)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [], RuntimeException+ FullyQualifiedErrorId : InvalidCastFromStringToInteger Cannot convert value "$" to type "System.Int32". Error: "Input string was not in a correct format." At C:\Program Files\NSClient++\scripts\check_csv_freespace.ps1:35 char:1+ $critvalue = [math]::truncate(($capacity / 100) * $critlevel)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [], RuntimeException+ FullyQualifiedErrorId : InvalidCastFromStringToInteger Attempted to divide by zero.And this is the script:
<# .SYNOPSIS check your CSV for used/free diskspace .DESCRIPTION check your mounted HyperV-CSV for free/used diskspace and exit with nagios exit codes additional the script writes out performance data .NOTES File Name : check_csv_freespace.ps1 Author : DGE Prerequisite : PowerShell V2 or newer .LINK http://nowhere.com .EXAMPLE .\check_csv_freespace.ps1 -n csv1-scsi -w 80 -c 90 Sample Output: OK - csv1-scsi total: 2047GB, used: 734GB (35%), free: 1313GB (64%) | 'csv1-scsi used_space'=734Gb;1637;1842;0;2047 #> Param( [parameter(Mandatory=$true)] [alias("n")] $csvname, [parameter(Mandatory=$true)] [alias("w")] $warnlevel, [parameter(Mandatory=$true)] [alias("c")] $critlevel) $exitcode = 3 # "unknown" $freespace = Get-WmiObject win32_volume | where-object {$_.Label -eq $csvname} | ForEach-Object {[math]::truncate($_.freespace / 1GB)} $capacity = Get-WmiObject win32_volume | where-object {$_.Label -eq $csvname} | ForEach-Object {[math]::truncate($_.capacity / 1GB)} $usedspace = $capacity - $freespace $warnvalue = [math]::truncate(($capacity / 100) * $warnlevel) $critvalue = [math]::truncate(($capacity / 100) * $critlevel) $usedpercent = [math]::truncate(($usedspace / $capacity) * 100) $freepercent = [math]::truncate(($freespace / $capacity) * 100) if ($usedpercent -gt $critlevel) { $exitcode = 2 Write-Host "CRITICAL - $csvname total: ${capacity}GB, used: ${usedspace}GB (${usedpercent}%), free: ${freespace}GB (${freepercent}%) | '$csvname used_space'=${usedspace}Gb;$warnvalue;$critvalue;0;$capacity" } elseif ($usedpercent -gt $warnlevel) { $exitcode = 1 Write-Host "WARNING - $csvname total: ${capacity}GB, used: ${usedspace}GB (${usedpercent}%), free: ${freespace}GB (${freepercent}%) | '$csvname used_space'=${usedspace}Gb;$warnvalue;$critvalue;0;$capacity" } else { $exitcode = 0 Write-Host "OK - $csvname total: ${capacity}GB, used: ${usedspace}GB (${usedpercent}%), free: ${freespace}GB (${freepercent}%) | '$csvname used_space'=${usedspace}Gb;$warnvalue;$critvalue;0;$capacity" }
So the error is apparently on line 34 and 35..
Does anyone has any idea what is wrong with this script? Because I have no idea at all.
Thanks for the help in advance!