Created
February 18, 2021 14:57
-
-
Save rbleattler/cb861e2cd0d5dd941e4c2e19abc7e822 to your computer and use it in GitHub Desktop.
PowerShell Write Verbose with Timestamp Parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-Verbose { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory, ValueFromPipeline, Position = 0)] | |
[string] | |
$Message, | |
[Parameter()] | |
[switch] | |
$TimeStamp | |
) | |
begin { | |
Write-Debug "Enter [$($PSCmdlet.MyInvocation.MyCommand.Name)]..." | |
$PSBoundParameters.Keys.ForEach{ | |
if ($PSBoundParameters.PSItem -is [string]) { | |
Write-Debug "$_ : $($PSBoundParameters.Item($_))" | |
} else { | |
Write-Debug "$_ : $($PSBoundParameters.Item($_).GetType())" | |
} | |
} | |
} | |
process { | |
if ($TimeStamp) { | |
$Message = '[{0} : {1}]' -f $(Get-Date -Format G), $Message | |
} | |
[System.Management.Automation.VerboseRecord]::new($Message) | |
} | |
end { | |
Write-Debug "Exit [$($PSCmdlet.MyInvocation.MyCommand.Name)]..." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment