Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## $logFile passed as a parameter such as "%systemdrive%\%year%\%monthname%\blah-%hour%.%minute%-%day%.%month%.%year%.log"
- ## $appendToLogfile is a [switch] parameter to determine if log file is appended to or overwritten (default)
- if( -Not [string]::IsNullOrEmpty( $logFile ) )
- {
- ## allow place holders for date elements
- [datetime]$now = [datetime]::Now
- [string]$actualLogFile = [System.Environment]::ExpandEnvironmentVariables( $logFile ) -replace '%year%' , $now.Year -replace '%monthname%' , ((Get-Culture).DateTimeFormat.GetMonthName( $now.Month )) -replace '%month%' , ( '{0:D2}' -f $now.Month ) `
- -replace '%day%' , ( '{0:D2}' -f $now.Day ) -replace '%dayname%' , ((Get-Culture).DateTimeFormat.GetDayName( $now.Month )) -replace '%hour%' , ( '{0:D2}' -f $now.Hour ) -replace '%minute%' , ( '{0:D2}' -f $now.Minute ) `
- -replace '%second%' , ( '{0:D2}' -f $now.Second )
- [string]$logFolder = Split-Path -Path $actualLogFile -Parent
- if( -Not ( Test-Path -Path $logFolder ) )
- {
- if( -Not ( New-Item -Path $logFolder -ItemType Directory -Force ) )
- {
- Write-Warning -Message "Failed to create log file folder `"$logFolder`""
- }
- }
- Write-Verbose -Message "Log file is `"$actualLogFile`""
- Start-Transcript -Path $actualLogFile -Append:$appendToLogfile
- }
Add Comment
Please, Sign In to add comment