gpt4 book ai didi

powershell - 在 PowerShell 中将所有写输出通过管道传输到同一个输出文件

转载 作者:行者123 更新时间:2023-12-04 02:11:40 32 4
gpt4 key购买 nike

正如标题所暗示的那样,您如何使所有写入输出 - 无论它们出现在何处 - 自动附加到您定义的日志文件中?这样脚本会更容易阅读,并且会减少一点工作!

下面的小例子,如果可能的话,我不想看到任何“| Out-File”,但让它们仍然输出到那个文件!

$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命令来记录 shell 看到的所有内容(除了 exe 输出)。脚本完成后调用 Stop-Transcript .

关于powershell - 在 PowerShell 中将所有写输出通过管道传输到同一个输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24799972/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com