如标题所示,如何使所有写入输出(无论出现在何处)自动附加到定义的日志文件中?这样,脚本将更易于阅读,并且省去了一点工作!
下面的小示例,id尽可能不显示“ |外文件”,但仍将它们输出到该文件!
$Author = 'Max'
$Time = Get-Date -Format "HH:mm:ss.fff"
$Title = "Illegal Software Removal"
$LogName = "Illegal_Remove_$($env:COMPUTERNAME).log"
$Log = "C:\Windows\Logs\Software" + "\" + $LogName
$RemoteLog = "\\Server\Adobe Illegal Software Removal"
Set-PSBreakpoint -Variable Time -Mode Read -Action { $global:Time = Get-Date -format "HH:mm:ss.fff" } | Out-Null
If((Test-Path $Log) -eq $False){ New-Item $Log -ItemType "File" -Force | Out-Null }
Else { $Null }
"[$Time][Startup] $Title : Created by $Author" | Out-File $Log -Append
"[$Time][Startup] Configuring initial variables required before run..." | Out-File $Log -Append
编辑:这需要在PS v2.0上工作,我完全不希望输出仅在日志中显示在屏幕上。所以我具有相同的功能,但脚本看起来像这样...
"[$Time][Startup] $Title : Created by $Author"
"[$Time][Startup] Configuring initial variables required before run..."
您有两种选择,一种是在调用脚本时进行重定向,例如:
PowerShell.exe -Command "& {c:\myscript.ps1}" > c:\myscript.log
或者,您可以使用Start-Transcript
命令记录外壳程序看到的所有内容(exe输出除外)。脚本完成后,调用Stop-Transcript
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句