【问题标题】:PowerShell Logging (verbose)PowerShell 日志记录(详细)
【发布时间】: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-PSFMessageGet-PSFMessage 函数。从长远来看,这可能是更好的选择。
  • 你能用一个例子来证明这一点吗?
  • @Alex - 你看过有问题的模块吗?它似乎有一组合理的例子...... [grin]
  • @Alex,这很简单。使用 write 函数来创建您的日志记录信息,最后使用Get-PSFMessage 将您想要的所有内容输出到文件或任何适合您需要的内容。

标签: powershell logging logfile verbose


【解决方案1】:

您可以在此处找到有关 PSFramework 日志记录的官方文档:

https://psframework.org/documentation/documents/psframework/logging.html

基本上,您在脚本开头所做的是使用Set-PSFLoggingProvider 定义日志应该去哪里,然后使用Write-PSFMessage 来写东西。 使用日志文件是最常见的方案,但开箱即用地支持许多其他目标(Eventlog、Splunk、Azure Log Analytics 等)。

我们以logfile 为例:

$paramSetPSFLoggingProvider = @{
    Name         = 'logfile'
    InstanceName = 'MyTask'
    FilePath     = 'C:\Logs\TaskName-%Date%.csv'
    Enabled      = $true
}
Set-PSFLoggingProvider @paramSetPSFLoggingProvider

启用日志记录后,生成的任何消息都将写入文件“C:\Logs\TaskName-.csv.

write messages,您现在可以:

# Default verbose message
Write-PSFMessage "Hello"

# Visible to user by default
Write-PSFMessage -Level Host -Message "Alex"

# Add some extra info
try { 1/0 }
catch { Write-PSFMessage -Level Warning -Message "Tried to do the impossible" -ErrorRecord $_ -Tag 'failed','impossible' }

【讨论】:

    【解决方案2】:

    这就是Tee-Object 的用途。

    '将命令输出保存在文件或变量中,并将其发送到 管道。'

    例子

    <#
    Example 1: Output processes to a file and to the console
    This example gets a list of the processes running on the computer and sends the result to a file. Because a second path is not specified, the processes are also displayed in the console.
    #>
    
    Get-Process | 
    Tee-Object -FilePath "C:\Test1\testfile2.txt"
    
    
    Example 2: Output processes to a variable and `Select-Object`
    
    This example gets a list of the processes running on the computer, saves them to the $proc variable, and pipes them to Select-Object.
    #>
    
    Get-Process notepad | 
    Tee-Object -Variable proc | 
    Select-Object processname,handles
    
    
    <#
    Example 3: Output system files to two log files
    
    This example saves a list of system files in a two log files, a cumulative file and a current file.
    #>
    
    Get-ChildItem -Path D: -File -System -Recurse |
    Tee-Object -FilePath "c:\test\AllSystemFiles.txt" -Append |
    Out-File c:\test\NewSystemFiles.txt
    

    虽然 Alex_P 指向你的 PSFramework 更优雅。

    Logging Done Right with PowerShell and the PSFramework Module

    【讨论】:

      猜你喜欢
      • 2017-03-13
      • 1970-01-01
      • 2022-11-15
      • 2012-10-08
      • 2013-03-12
      • 1970-01-01
      • 2020-08-02
      • 2018-10-28
      • 1970-01-01
      相关资源
      最近更新 更多