HM-Monitoring.psm1

#region Check_SNMP_Value_If_Exists
<#
.SYNOPSIS
Checks if the value provided by the snmp deamon is a valid value
 
.DESCRIPTION
Checks if the value provided by the snmp deamon is a valid value
 
.PARAMETER SNMPValue
The Output of an SNMP request
 
.EXAMPLE
Check_SNMP_Value_If_Exists -SNMPValue $AllSNMPDATA.Key
 
.NOTES
 
#>

function Check_SNMP_Value_If_Exists {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)]
        $SNMPValue
    )

    if (
        $SNMPValue -notlike "*NoSuchInstance*" -and `
        $SNMPValue -notlike "*NoSuchObject*" -and `
        $SNMPValue -ne "" -and `
        $null -ne $SNMPValue)
    { 
        return $true
    }
    else { 
        return $false
    }
}
Export-ModuleMember -Cmdlet Check_SNMP_Value_If_Exists 
#endregion

#region Convert_Version_To_Accumulated
<#
    .Description
    Compares to versions insertet as an string array
 
    .example
    $FoundVersionArray = "21.2.16.590".Split(".")
    $MinVerionArray = "11.5.22.980".Split(".")
    Convert_Version_To_Accumulated -VersionArray $FoundVersionArray -lt Convert_Version_To_Accumulated -VersionArray $MinVerionArray
#>

function Convert_Version_To_Accumulated {
    [CmdletBinding()]
    param (
        # VersionArray
        [Parameter(Mandatory = $true)]
        $VersionArray
    )

    [int64]$Version = ([int64]$VersionArray[3] * 1 + [int64]$VersionArray[2] * 10000 + [int64]$VersionArray[1] * 100000000 + [int64]$VersionArray[0] * 1000000000000)

    return [int64]$Version
}
Export-ModuleMember -Cmdlet Convert_Version_To_Accumulated
#endregion

#region Convert_Date_To_German
<#
    .Description
    Converts to german time format
#>

function Convert_Date_To_German {
    [CmdletBinding()]
    param (
        [System.DateTime]
        $DateObj
    )    
    return (Get-Date $Dateobj -Format "dd.MM.yyyy HH:mm:ss")
}
Export-ModuleMember -Cmdlet Convert_Date_To_German
#endregion

#region Test_FileLock
<#
.Description
Check if a file is in use
#>

function Test_FileLock {
    param (
        [parameter(Mandatory=$true)][string]$Path
    )

    $oFile = New-Object System.IO.FileInfo $Path
    if ((Test-Path -Path $Path) -eq $false) {
        return $false
    }
    try {
        $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)

        if ($oStream) {
            $oStream.Close()
        }
        $false
    }
    catch {
        # file is locked by a process.
        return $true
    }
}
Export-ModuleMember -Cmdlet Test_FileLock 
#endregion

#region Write_Separator
<#
.Description
writes separator
#>

function Write_Separator {
    Write-Host ""
    Write-Host "----------------------------------------------------------"
}
Export-ModuleMember -Cmdlet Write_Separator 
#endregion

#region Check_SNMP_Module_Installed
<#
.SYNOPSIS
Checks if the SNMP Module is installed
 
.DESCRIPTION
Checks if the SNMP Module is installed
 
.EXAMPLE
Check_SNMP_Module_Installed
 
.NOTES
No Parameters
#>

function Check_SNMP_Module_Installed {
    if ([environment]::OSVersion.Version.Major -gt 8) {
        if ($null -eq (Get-Module -ListAvailable -Name Snmp)) {
            Write-Host "PowerShell Module SNMP is not installed!"
            Error_Exit
        }
    }
    else {
        $PSModulePath1 = "C:\Program Files\WindowsPowerShell\Modules"
        $PSModulePath2 = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules"
    
        if (Test-Path -path "$PSModulePath1\SNMP\*\SNMP.psm1") {
            try {
                Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Process
                Import-Module "$PSModulePath1\SNMP\1.0.0.1\SNMP.psm1"
                Add-Type -Path "$PSModulePath1\SNMP\1.0.0.1\SharpSnmpLib.dll"
            }
            catch { 
                Write-Host "(Error - SNMP-Module): $($PSItem.Exception.Message)"
                Error_Exit
            }
        }
        elseif (Test-Path -path "$PSModulePath2\SNMP\*\SNMP.psm1") {
            try {
                Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Process
                Import-Module "$PSModulePath2\SNMP\1.0.0.1\SNMP.psm1"
                Add-Type -Path "$PSModulePath2\SNMP\1.0.0.1\SharpSnmpLib.dll"
            }
            catch { 
                Write-Host "(Error - SNMP-Module): $($PSItem.Exception.Message)"
                Error_Exit
            }
        }
        else { 
            Write-Host "PowerShell Module SNMP is not installed!"
            Error_Exit
        }
    }
}
Export-ModuleMember -Cmdlet Check_SNMP_Module_Installed
#endregion

#region Error_Exit
<#
    .Description
    Increases the Count in $ErrorCount and does an Exit
#>

function Error_Exit {
    $Script:ErrorCount++
    Exit 1001
}
#endregion