Wednesday, July 26, 2017

Do something and Log information to event log in Powershell

A quick way to generate a log file that can be used to feed write-eventlog
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
$Archivolog = "C:\temp\output-$(get-date -f yyyy-MM-dd_hh-mm-ss).txt"
$Archivolog2 = "C:\temp\compress-$(get-date -f yyyy-MM-dd_hh-mm-ss).txt"
Start-Transcript -path $Archivolog -append
Write-Host Step 1 Start at $(get-date)
gci -r C:\temp | where {$_.attributes -notmatch "compressed"} | foreach {compact /C $_.fullname }  | out-file $Archivolog2 -Append
gc $Archivolog2
Write-Host Step 1 Finished at $(get-date)
$text = [IO.File]::ReadAllText($Archivolog)
New-EventLog –LogName Application –Source "Nakama test"
Write-EventLog –LogName Application –Source "Nakama test" –EntryType Information –EventID 9213  –Message $text


Post a Comment