Functions/Logging/Write-DebugInfoToOutput.ps1
<#
.SYNOPSIS Writes preset messages or a custom message to Host-Output. .DESCRIPTION This function helps debugging PowerShell scripts that are executed by a scheduler. 1. Create a logfile from the Host-Output during script execution (using Start-Transcript). 2. Write (preset) messages to the host during execution using this function 3. You've valuable information to debug issues when a script has failed. With switches you can enable preset messages to write to Host-Output. With switch 'all' you can enable all preset messages at once. .EXAMPLE Write-DebugInfoToOutput -All .EXAMPLE Write-DebugInfoToOutput -HostInfo -LoadedModules .EXAMPLE $VariableName1 = "ABC" $VariableName2 = "XYZ" $MessageBody = @() $MessageBody += "VariableName1 = {0}" -f $VariableName1 $MessageBody += "VariableName2 = {0}" -f $VariableName2 Write-DebugInfoToOutput -MessageTitle "My debug title" -MessageBody $MessageBody OUTPUT: ----------------------------start---------------------------- My debug title ------------------------------------------------------------- VariableName1 = ABC VariableName2 = XYZ -----------------------------end----------------------------- .EXAMPLE $VariableName1 = "ABC" $VariableName2 = "XYZ" $MessageBody = " VariableName1 = $VariableName1 VariableName2 = $VariableName2 " Write-DebugInfoToOutput -MessageTitle "My debug title" -MessageBody $MessageBody #> function Write-DebugInfoToOutput { [CmdletBinding(DefaultParameterSetName='default')] Param ( # Write hostname and network interface details of current PS session to output/host [Parameter(Mandatory=$false, Position=0, ParameterSetName='default')] [switch] $HostInfo, # Writes Get-Module to output/host [Parameter(Mandatory=$false, Position=1, ParameterSetName='default')] [switch] $Modules, # Writes Get-Variable to output/host [Parameter(Mandatory=$false, Position=2, ParameterSetName='default')] [switch] $Variables, # Writes Host, Module and Variable info to output/host [Parameter(Mandatory=$false, Position=0, ParameterSetName='all')] [switch] $All, # Title for the custom message to write. [Parameter(Mandatory=$true, Position=0, ParameterSetName='custom')] [string] $MessageTitle, # Body for the custom message to write. [Parameter(Mandatory=$false, Position=1, ParameterSetName='custom')] [string[]] $MessageBody ) if ($All) { $HostInfo = $true $Modules = $true $Variables = $true } # Variables to format the message $MessageFormatLines = @{ 'head' = "`n----------------------------start----------------------------`n" 'divider' = "`n-------------------------------------------------------------`n" 'end' = "-----------------------------end-----------------------------`n" 'spacer' = "" } $Message = "" if ($HostInfo) { $MessageTitle = "Host details of current PS Session" $MessageFormatLines['spacer'] = Set-Spacer -MessageTitle $MessageTitle -Divider $MessageFormatLines['divider'] $Message += "{0}{1}{2}{3}" -f $MessageFormatLines['head'], $MessageFormatLines['spacer'], $MessageTitle, $MessageFormatLines['divider'] $Message += "Hostname: {0} `n" -f $env:computername $Message += "Network Interfaces: `n" $Message += $(Get-NetIPConfiguration | ` Where-Object {$_.NetAdapter.Status -ne "Disconnected"} | ` Select-Object -Property InterfaceAlias, InterfaceDescription, IPv4Address | ` Out-String) } if ($Modules) { $MessageTitle = "Loaded modules in this powershell session" $MessageFormatLines['spacer'] = Set-Spacer -MessageTitle $MessageTitle -Divider $MessageFormatLines['divider'] $Message += "{0}{1}{2}{3}" -f $MessageFormatLines['head'], $MessageFormatLines['spacer'], $MessageTitle, $MessageFormatLines['divider'] $Message += $(Get-Module | Select-Object -Property Name, Version | Out-String) } if ($Variables){ $MessageTitle = "Variable list in this powershell session" $MessageFormatLines['spacer'] = Set-Spacer -MessageTitle $MessageTitle -Divider $MessageFormatLines['divider'] $Message += "{0}{1}{2}{3}" -f $MessageFormatLines['head'], $MessageFormatLines['spacer'], $MessageTitle, $MessageFormatLines['divider'] $Message += $(Get-Variable | Format-Table | Out-String) } if ($PSCmdlet.ParameterSetName -eq 'custom'){ $MessageFormatLines['spacer'] = Set-Spacer -MessageTitle $MessageTitle -Divider $MessageFormatLines['divider'] $Message += "{0}{1}{2}{3}" -f $MessageFormatLines['head'], $MessageFormatLines['spacer'], $MessageTitle, $MessageFormatLines['divider'] if($MessageBody){ $Message += $($MessageBody | Out-String) } } if($MessageBody){ $Message += $MessageFormatLines['end'] } Write-Output $Message } Export-ModuleMember -Function Write-DebugInfoToOutput function Set-Spacer($MessageTitle, $Divider){ $Spacer = '' if($MessageTitle.Length -lt ($Divider.Length - 2)) { for ($i=1; $i -le ([math]::Truncate((($Divider.Length - 2) - $MessageTitle.Length) / 2)); $i++){ $Spacer += ' ' } } return $Spacer } |