disk-space.ps1 - powershell script by ActiveXperts Software
disk-space.ps1 checks whether a disk has sufficient disk space.
Use disk-space.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select disk-space.ps1. Configure the required parameter, or press 'Load a working sample'.
In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.
disk-space.ps1 script code
################################################################################# # ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V. # For more information about ActiveXperts Network Monitor, visit the ActiveXperts # Network Monitor web site at http://www.activexperts.com ################################################################################# # Script # Disk-Space.ps1 # Description: # This function checks if a disk has enough space left. # Declare Parameters: # 1) strHost (string) - Hostname or IP address of the computer you want to monitor # 2) strDriveLetter (string) - The driveletter of the disk you want to check. # 3) nRequiredSpaceGB (int) - The freespace required on the Disk # 4) strAltCredentials (string, optional) - Specify an empty string to use Network Monitor service credentials. # To use alternate credentials, enter a server that is defined in Server Credentials table. # (To define Server Credentials, choose Tools->Options->Server Credentials) # Usage: # .\Disk-Space.ps1 '<Hostname | IP>' '<DriveLetter>' <Required_Space_GB> '[alt-credentials]' # Sample: # .\Disk-Space.ps1 'localhost' 'C:' 10 ################################################################################# # -- Declare Parameters param( [string]$strHost = '', [string]$strDriveLetter = '', [int]$nRequiredSpaceGB = -1, [string]$strAltCredentials = '' ) # -- Use _activexperts.ps1 with common functions . 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1' ################################################################################# # // --- Main script --- ################################################################################# # -- Clear screen and clear error cls $Error.Clear() # -- Validate parameters, return on parameter mismatch if( $strHost -eq '' -or $strDriveLetter -eq '' -or $nRequiredSpaceGB -lt 0 ) { $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\Disk-Space.ps1 "<Hostname | IP>" "<DriveLetter>" <Required_Space_MB> "[alt-credentials]"' echo $res exit } # -- Declare local variables by assigning an initial value to it $strExplanation = '' $objAltCredentials = $null $nFreeGB = 0 # If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings if( $strAltCredentials -ne '' ) { # Get the Alternate Credentials object. Function 'getCredentials' is implemented in '_activexperts.ps1' if( ( AxGetCredentials $strHost $strAltCredentials ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS ) { echo $strExplanation exit } } # Get WMI object $strWmi = 'Win32_PerfFormattedData_PerfDisk_LogicalDisk' if( $objAltCredentials -eq $null ) { $objWMIService = Get-WmiObject -ComputerName $strHost -Class $strWmi -ErrorVariable Error -ErrorAction SilentlyContinue } else { $objWMIService = Get-WmiObject -ComputerName $strHost -Class $strWmi -ErrorVariable Error -ErrorAction SilentlyContinue } if( $Error -ne '' ) { $res = 'UNCERTAIN: ' + $Error echo $res exit } if( $objWMIService -eq $null ) { $res = 'ERROR: Unable to access [' + $strHost + ']. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down' echo $res exit } foreach( $objDisk in $objWMIService ) { if( $objDisk.Name -eq $strDriveLetter ) { $nFreeGB = [int]($objDisk.FreeMegaBytes / 1024) if( $nRequiredSpaceGB -le $nFreeGB) { $res = 'SUCCESS: ' } else { $res = 'ERROR: ' } $res += 'Disk [' + $strDriveLetter + '] has [' + $nFreeGB + ' GB] free space, [' + $nRequiredSpaceGB + ' GB] is required on [' + $strHost + '] DATA:' + $nFreeGB echo $res exit } } # -- Print script result $res = 'UNCERTAIN: Disk [' + $strDriveLetter + '] was not found on[' + $strHost + ']' echo $res exit ################################################################################# # // --- Catch script exceptions --- ################################################################################# trap [Exception] { $res = 'UNCERTAIN: ' + $_.Exception.Message echo $res exit }