
log your scripts to checkout your script behavior ,we can do that by simple function includes TimeStamp and your Note
you wanna add at any stage on your script .
where’s the log file located ?
1 2 3 |
$logfile = "C:\FileName&Path_$(get-date -format `"yyyyMMdd_hhmmsstt`").log" Example : $logfile = "C:\WorkDir\Logs_$(get-date -format `"yyyyMMdd_hhmmsstt`").log" |
lets make it as function –
1 2 3 4 5 6 |
function log($string) { write-host $string $TimeStamp = (Get-date -Format dd-MM-yyyy) + "|" + (get-date -format HHMMsstt) $TimeStamp + " " + $string | out-file -Filepath $logfile -append -Force } |
the TimeStamp format contains with Date | hour :
now lets combine it our script :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
clear get-date | Out-File $logfile log "checking for new version " $NewBackupFolder = if (!(test-path $CurrentVersionFolder\Backup_$CurrentVersionBuild )) { New-Item -ItemType Directory -Path $CurrentVersionFolder\Backup_$CurrentVersionBuild -Force } else { Write-Host "folder Backup_$CurrentVersionBuild Already Exist." -BackgroundColor Black -ForegroundColor White } log "Created BackUp Folder $NewBackupFolder" if (test-path $CurrentVersionFolder\Backup_$CurrentVersionBuild ) { try { $copy = Copy-Item -Path C:\WebApplicationSites\DevOps.payoneer.com\* -Recurse -Destination $CurrentVersionFolder\Backup_$CurrentVersionBuild -Force -Verbose log "All Files were copied/backuped from folder $CurrentVersionFolder\DevOps.payoneer.com to folder $NewBackupFolder" } catch {log "There Was a Problem With backup, please check log!" } } else { log "No Backup Created to Production folder ,Please Check log!and start over." -BackgroundColor Black -ForegroundColor Red Break } |
each time we’ll use the keyword function ‘log’ it will write our comment to the $logfile and will also write it to the Console.
(Visited 8,494 times, 1 visits today)