HM-Monitoring.psm1

#region Check_SNMP_Value_If_Exists
<#
.Description
Checks if the value provided is existing and not null
#>

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 Import_SNMPModul
<#
.Description
Installs SNMP Modul from PowerShell Gallery
#>

function Import_SNMPModul 
{
    [CmdletBinding()]
    param (

    )
    if (!(Get-Module -ListAvailable -Name Snmp)) 
    {
        Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Process
        if ($PSVersionTable.PSVersion.Major -ge 5) 
        {
            Write-Host "PS-Modul SNMP not installed"
            [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
            Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted -ErrorAction SilentlyContinue
            try 
            {
                Write-Host "Trying installation of SNMP Module from PowershellGallery"
                Install-PackageProvider -Name NuGet -Force -MinimumVersion 2.8.5.201 -Confirm:$false -WarningAction SilentlyContinue
                Install-Module -Name PowerShellGet -Force -AllowClobber -WarningAction SilentlyContinue 
                Install-Module -Name SNMP -Force -Confirm:$false -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
            }
            catch 
            { Write-Host "(Error - SNMP-Module): $($PSItem.Exception.Message)"; $ErrorCount++; Exit 1001 }
            Set-PSRepository -Name 'PSGallery' -InstallationPolicy Untrusted -ErrorAction SilentlyContinue
        }
        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)"; $ErrorCount++; Exit 1001 }
            }
            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)"; $ErrorCount++; Exit 1001 }
            }
            else 
            { Write-Host "PS-Modul SNMP not installed"; $ErrorCount++; Exit 1001 }
        }
    }
    else { Import-Module -Name SNMP -WarningAction SilentlyContinue -ErrorAction SilentlyContinue }
}
Export-ModuleMember -Cmdlet Import_SNMPModul 
#endregion

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

function Test_FileLock 
{
    [CmdletBinding()]
    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 
{
    [CmdletBinding()]
    param (
    )
    Write-Host ""
    Write-Host "----------------------------------------------------------"
}
Export-ModuleMember -Cmdlet Write_Separator 
#endregion