【发布时间】:2020-08-18 00:30:47
【问题描述】:
我想记录我的 PowerShell 脚本。 现在我自己发现了 Verbose 参数。
脚本当前如下所示(示例脚本):
try {
New-Item "D:\Test.txt" -ItemType "File" -ErrorAction Stop -Verbose
}
catch {
Write-Host $Error[0] -ForegroundColor Red
}
然后输出如下所示:
VERBOSE: Execute the "Create file" operation for the target "Target: D:\Test.txt".
Directory: D:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03.05.2020 18:09 0 Test.txt
我希望输出出现在控制台中并写入日志文件。 输出应如下所示:
控制台:
VERBOSE: Execute the "Create file" operation for the target "Target: D:\Test.txt".
日志文件:
01.01.2020 12:00 Execute the "Create file" operation for the target "Target: D:\Test.txt".
你不应该看到这样的问题。
Directory: D:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03.05.2020 18:09 0 Test.txt
我该怎么做? 是否也有可能不必在每个命令之后都写一个“详细”?
谢谢!
【问题讨论】:
-
对于日志记录,我建议使用
PSFramework库。它有一个Write-PSFMessage和Get-PSFMessage函数。从长远来看,这可能是更好的选择。 -
你能用一个例子来证明这一点吗?
-
@Alex - 你看过有问题的模块吗?它似乎有一组合理的例子...... [grin]
-
@Alex,这很简单。使用 write 函数来创建您的日志记录信息,最后使用
Get-PSFMessage将您想要的所有内容输出到文件或任何适合您需要的内容。
标签: powershell logging logfile verbose