The SysAdmin PowerShell Module consists of a diverse set of functions, but there is one set of functions that is leveraged in every other function…Logging! The logging functions provide visual feedback to the user about the progress of a script. It can also provide valuable debugging information when a script goes sideways.

The logging functions consist of a core function ‘New-Log’. The function takes two parameters: (1) the log text, and (2) the type of log. The second parameter is not required. The remaining logging functions in the module call ‘New-Log’ with the second parameter. The functions are shown below:

Function New-Log {
    PARAM (
        [Parameter(Position=0, Mandatory=$true)]
        $Text,
        [Parameter(Position=1, Mandatory=$false)]
        $Mode = "DEFAULT"
    )
    BEGIN {}
    PROCESS {
        if ([String]::IsNullOrEmpty($Text)) {
            return;
        }
        
        $DateData = ("[" + [DateTime]::Now.ToString() + "]")
        
        if ($Mode -eq "VERBOSE") { 
            if ($Global:VerboseLogs -eq $true) { 
                Write-Host                  `
                    "$DateData $Text"       `
                    -ForegroundColor Yellow `
                    -BackgroundColor Black                    
            }
            return
        }
        if ($Mode -eq "ERROR") { 
            Write-Host                    `
                "$DateData ERROR - $Text" `
                -ForegroundColor Red      `
                -BackgroundColor Black
            return
        }
        if ($Mode -eq "WARN") { 
            Write-Host                   `
                "$DateData WARN - $Text" `
                -ForegroundColor White   `
                -BackgroundColor Black
            return
        }
        if ($Mode -eq "DEBUG") { 
            Write-Debug "$DateData $Text" 
            return
        }
        
        # Default
        Write-Host "$DateData $Text" -ForegroundColor Gray
    }
    END {}
}

Function New-LogDebug {
    PARAM (
        [Parameter(Position=0, Mandatory=$true)]
        $Text
    )
    BEGIN {}
    PROCESS {
        New-Log $Text "DEBUG"
    }
    END {}
}

Function New-LogError {
    PARAM (
        [Parameter(Position=0, Mandatory=$true)]
        $Text
    )
    BEGIN {}
    PROCESS {
        New-Log $Text "ERROR"
    }
    END {}
}

Function New-LogVerbose {
    PARAM (
        [Parameter(Position=0, Mandatory=$true)]
        $Text
    )
    BEGIN {}
    PROCESS {
        New-Log $Text "VERBOSE"
    }
    END {}
}

Function New-LogWarning {
    PARAM (
        [Parameter(Position=0, Mandatory=$true)]
        $Text
    )
    BEGIN {}
    PROCESS {
        New-Log $Text "WARN"
    }
    END {}
}

The next set of components for the SysAdmin PowerShell Module will be core network checks and functions. Check the PSModule Category for more information.