简体   繁体   English

Powershell将日期时间附加到文件开头

[英]powershell append date time to beginning of file

I'm working to get a powershell command that will dump the value into output.xml. 我正在努力获得将这些值转储到output.xml中的powershell命令。 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. 首先,从用户请求一个值,然后为该值解析应用程序日志,并将结果返回到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 

Use Out-File to write the first line(s) of the file as well: 也可以使用Out-File写入Out-File的第一行:

$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

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. 如果要在日期之后添加更多行,请不要忘记使用-Append。

对您的write方法进行简单的编辑:

Out-File "$(Get-Date -UFormat '%Y-%m-%d-%H-%M-%S-')$output.xml" -Append

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM