Skip to content

Instantly share code, notes, and snippets.

@torgro torgro/log.ps1
Created Dec 4, 2018

What would you like to do?
function Test-Command
write-verbose -Message "Start"
function Write-Verbose
$date = [datetime]::Now
$parentFunction = (Get-Variable -Name MyInvocation -Scope 1).Value.MyCommand.Name
$logFile = $env:SystemDrive |
Join-Path -ChildPath temp |
Join-Path -ChildPath log.txt
$objectLogFile = $env:SystemDrive |
Join-Path -ChildPath temp |
Join-Path -ChildPath log.json
$logThis = "$date - $message"
$logObj = [pscustomobject][ordered]@{
Date = $date
Command = $parentFunction
Message = $Message
Add-Content -Path $logFile -Value $logThis -Encoding UTF8
$logObj | ConvertTo-Json -Compress | Add-Content -Path $objectLogFile -Encoding UTF8
Microsoft.PowerShell.Utility\Write-Verbose -Message "$parentFunction - $Message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.