Reputation: 3
I'm working to get a powershell command that will dump the value into output.xml. First a value is requested from the user, then the application log is parsed for that value and the results are returned to output.xml. I would like to also tag the beginning with the date time stamp, but haven't been able to figure it out. This is what I have come up with to accomplish the parsing, and I have attempted to use a few other methods:
read-host (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
I guess I don't understand exactly where to put this part of the code to make it work. My whole script looks like this.
$value = read-host "Enter your search value"
Get-WinEvent -Logname application -EA silentlycontinue | ?{$_.LevelDisplayName -eq "Error" -or $_.LevelDisplayName -eq "Warning"} | Out-File '$output.xml' -Append
Upvotes: 0
Views: 5664
Reputation: 19664
A simple edit to your write method:
Out-File "$(Get-Date -UFormat '%Y-%m-%d-%H-%M-%S-')$output.xml" -Append
Upvotes: 0
Reputation: 174495
Use Out-File
to write the first line(s) of the file as well:
$value = Read-Host "Enter your search value"
"# $(Get-Date -Format 'dd-MM-yyyy-hh-mm-ss')" |Out-File output.log
"# User $($env:USERNAME) supplied the following search term '$value'" |Out-File output.log -Append
Get-WinEvent -Logname application -EA silentlycontinue | ?{$_.LevelDisplayName -eq "Error" -or $_.LevelDisplayName -eq "Warning"} | Out-File output.log -Append
Upvotes: 3
Reputation: 434
If all you're looking for is the first line to be the data use this instead.
(Get-Date).tostring("dd-MM-yyyy-hh-mm-ss") | Out-File .\output.xml
Don't forget to use -Append if you're adding more lines after the date.
Upvotes: 0