dhcp.ps1 - powershell script by ActiveXperts Software
dhcp.ps1 checks for maximum number of IP addresses in use.
Use dhcp.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select dhcp.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.
dhcp.ps1 script code
################################################################################# # ActiveXperts Network Monitor PowerShell script. # For more information about ActiveXperts Network Monitor, visit the ActiveXperts # Network Monitor web site at http://www.activexperts.com ################################################################################# # Script # Dhcp.ps1 # Description: # Check for maximum number of IP addresses in use. # Requires SNMP running on the DHCP server and monitoring system. # This function uses ActiveXperts Network Component. # ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased # For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component # Declare Parameters: # 1) strDhcpServer (string) - Host name or IP address of the DHCP server # 2) strSubnet (string) - Subnet. # 3) numMaxAllowed (int) - maxmimum number of IP addresses allows # Usage: # .\Dhcp.ps1 '<Hostname | IP>' 'x.x.x.x' <Maximum Value> # Sample: # .\Dhcp.ps1 'localhost' '192.168.31.0' 200 ################################################################################# # -- Declare Parameters param( [string]$strHost = '', [string]$strSubnet = '', [int]$nMaxAllowed = -1 ) # -- Use _activexperts.ps1 with common functions . 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1' ################################################################################# # // --- Private Functions section --- ################################################################################# function checkDhcpMaxInuse( $strHost, $strOID, $nMaxAllowed, [ref]$strData, [ref]$strExplanation ) { $strCommunity = 'public' # Create instance of SNMP object $objSnmpManager = new-object -comobject AxNetwork.SnmpManager $objConstants = new-object -comobject AxNetwork.NwConstants # Initialize SNMP object $objSnmpManager.LogFile = 'c:\temp\_log.log' $objSnmpManager.ProtocolVersion = $objConstants.nwSNMP_VERSION_V2C $objSnmpManager.Initialize() if( $objSnmpManager.LastError -ne 0 ) { $strExplanation.value = 'ERROR: Unable to initialize ActiveXperts Network Component SNMP object' return $AXERROR } # Open connection to (remote) SNMP agent $objSnmpManager.Open( $strHost, $strCommunity ) if( $objSnmpManager.LastError -ne 0 ) { $strExplanation.value = 'ERROR: Unable to connect to agent [' + $strHost + '], community [' + $strCommunity + '], error #' + $objSnmpManager.LastError + ': ' + $objSnmpManager.GetErrorDescription( $objSnmpManager.LastError ) return $AXERROR } $objSnmpObject = $objSnmpManager.Get( $strOID ) if( $objSnmpManager.LastError -ne 0 ) { $strExplanation.value = 'UNCERTAIN: Unable to retrieve [' + $strOID + ', error #' + $objSnmpManager.LastError + ': ' + $objSnmpManager.GetErrorDescription( $objSnmpManager.LastError ) return $AXUNCERTAIN } $strData.value = $objSnmpObject.value if( $objSnmpObject.value -le $nMaxAllowed ) { $strExplanation.value = 'SUCCESS: ' } else { $strExplanation.value = 'ERROR: ' } $strExplanation.value = $strExplanation.value + 'IPs in use: [' + $objSnmpObject.value + ']' $objSnmpManager.Close() $objSnmpManager.Shutdown() # Note: type ID's are described in ActiveXperts Network Component manual, and included in the ActiveXperts Network Component samples. return $AXSUCCESS } ################################################################################# # // --- Main script --- ################################################################################# # -- Clear screen and clear error cls $Error.Clear() # -- Validate parameters, return on parameter mismatch if( $strHost -eq '' -or $strSubnet -eq '' -or $nMaxAllowed -lt 0 ) { $res = 'UNCERTAIN: Parameter error - Usage: .\Dhcp.ps1 "<Hostname | IP>" "x.x.x.x" <nMaxAllowed>' echo $res exit } # -- Declare local variables by assigning initial value $strData = '' $strExplanation = '' $objAltCredentials = $null $strOID = '1.3.6.1.4.1.311.1.3.2.1.1.2.' + $strSubnet # -- Check DHCP, assign to variable to prevent printing the function return value $nRetVal = checkDhcpMaxInuse $strHost $strOID $nMaxAllowed ([ref]$strData) ([ref]$strExplanation) # -- Print script result echo $strExplanation exit ################################################################################# # // --- Catch script exceptions --- ################################################################################# trap [Exception] { $res = 'UNCERTAIN: ' + $_.Exception.Message echo $res exit }