Tuesday, December 21, 2010

Powershell log time-stamping

The function 'logtime'  below will work as a logging function in scripts


$FileTime=[DateTime]::Now.ToFileTime()

function logtime{
$date = [DateTime]::Now.Day
$hour = [DateTime]::Now.TimeOfDay.Hours
$minutes = [DateTime]::Now.TimeOfDay.Minutes
$seconds = [DateTime]::Now.TimeOfDay.Seconds
$ms = [DateTime]::Now.TimeOfDay.Milliseconds

[object]$logtime = New-Object PSObject -Property @{
date=$date
hour=$hour
minutes=$minutes
seconds=$seconds
ms=$ms
}
write $logtime | ft -HideTableHeaders -AutoSize -Property date,hour,minutes,seconds,ms | out-file -append -noclobber $PWD\$FileTime

}


[output]
PS C:\Users\rferrisx\Documents> gc 129374235* | more

  21    8      45      43 821
  21    8      51      12 604
  21    8      51      12 616
  21    8      51      12 656
  21    8      51      12 662




No comments: