#Demo-DebugLog.ps1 Function Write-DebugMessage { Param([string]$text) #set to Continue to turn on debug messages $debugpreference="continue" # build a text string of information that can be passed # to the Debug pipeline #here are some variables you might want to work with $computer=$env:computername $domain=$env:userdomain $user=$env:username #return a timestamp value with milliseconds #like 08/06/2008 11:08:37:4361 #Or you can use an expression like this #(Get-Date -Format g).toString() $timestamp=get-date -f 'MM/dd/yyyy HH:mm:ss:ffff' $line=$($myinvocation.scriptlinenumber) $script=$($myinvocation.invocationname) [string]$msg="[{0}:{1}\{2}] {3} Line:{4} {5}" -f $computer,$domain,$user,$timestamp,$line,$text Write-Debug $msg } function Foo-Me { Write-DebugMessage "in function $($myinvocation.invocationname)" Write-DebugMessage "getting services" Get-Service Write-DebugMessage "exiting function" } #setting this to $TRUE doesn't do any good if the debug pipeline is #turned off $debuglog=$TRUE if ($debuglog) { #define a temporary name for the transcript file $transcriptName="{0}_transcript.txt" -f $myinvocation.mycommand.name $transcript=(Join-Path "C:\" $transcriptName ) Start-Transcript $transcript | Out-Null } Write-DebugMessage "starting $($myinvocation.invocationname)" sleep 3 Write-DebugMessage "Getting Date" Get-Date Write-DebugMessage "Getting powershell process" Get-Process powershell Write-DebugMessage "Calling Foo-Me" Foo-Me Write-DebugMessage "Getting WMI Information" $d=Get-WmiObject win32_logicaldisk -filter "deviceid='c:'" Write-DebugMessage ("Drive C size is {0}GB" -f ([int]($d.Size/1GB))) $a=@(1,2,3,4,5) Write-DebugMessage $a Write-DebugMessage "finished" if ($debuglog) { $filename="{0}_debug.txt" -f $myinvocation.mycommand.name $debuglogfile=(Join-Path "c:\" $filename) Stop-Transcript | Out-Null Get-Content $transcript | Select-String -Pattern "DEBUG:" | Out-File $debuglogfile notepad $debuglogfile $rc=Read-Host "Do you want to delete $transcript [YN]?" if ($rc -match "y") { Remove-Item $transcript } }