来自 invoke-command 的 Powershell 日志记录 [英] Powershell logging from invoke-command

查看:69
本文介绍了来自 invoke-command 的 Powershell 日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日志记录功能的脚本.该函数将日志写入 $msg 变量,然后将消息写入屏幕.我可以将此输出重定向到 .txt 文件以进行记录.

I have a script that includes a function for logging. The function writes log to $msg variable then writes the message to the screen. i can redirect this output to a .txt file for logging.

我需要使用 invoke-command 将脚本运行到多个服务器并将日志输出写入运行脚本的计算机上的txt文件.

I need to run the script to multiple servers using invoke-command and write the log output to a txt file on the computer running the script.

我想弄清楚如何在 PSSession 的 write-log 函数中输出 $msg 并返回它,以便我可以为所有服务器创建一个主日志文件.我应该创建并填充自定义对象吗?不确定如何执行此操作并从远程会话中获取结果.

I am trying to figure out how to output $msg in the write-log function from the PSSession and return it so I can create a master log file for all the servers. Should I create and populate a custom object? Not sure how to do that and get the results back from the remote session.

这能做到吗?

以下是我使用的代码示例以及日志文件输出的样子:

Here is an example of the code I am using and what the log file output should look like:

$servers = 'Server1','Server2'
$logfile = 'c:\scripts\logs\Reg-DLL-log.txt'

foreach($server in $servers){
    invoke-command -cn $server -sb{
        Param($server)

        Function write-log{
            [cmdletbinding()]
            Param(
                [Parameter(ValueFromPipeline=$true,Mandatory=$true)] [ValidateNotNullOrEmpty()]
                [string] $Message,
                [Parameter()] [ValidateSet("Error", "Warn", "Info")]
                [string] $Level = "Info",
                [Parameter()] [ValidateRange(1,30)]
                [Int16] $Indent = 0
            )
            $msg = "{0} {1}{2}:{3}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Level.ToUpper(), (" " * $Indent), $Message
                #This is output to screen
                switch ($Level) {
                    'Error' { Write-Host ('{0}{1}' -f (" " * $Indent), $Message) -ForegroundColor 'Red'}
                    'Warn' { Write-Host ('{0}{1}' -f (" " * $Indent), $Message) -ForegroundColor 'Yellow'}
                    'Info' { Write-Host ('{0}{1}' -f (" " * $Indent), $Message) -ForegroundColor 'white'}
        }}

        write-log -message 'Begin DLL registration for $server' -level Info
        $RegFile = "cimwin32.dll"
        regsvr32 $RegFile /s
        write-log -message 'registered $RegFile' -level Info
        write-log -message 'End DLL registration for $server' -level Info

    } -ArgumentList $server
}

Reg-DLL-log.txt 的日志输出应如下所示:

Log output to Reg-DLL-log.txt should look like this:

2013-06-19 11:25:12 INFO:Begin DLL registration for Server1
2013-06-19 11:25:12 INFO:registered cimwin32.dll
2013-06-19 11:25:12 INFO:End DLL registration for Server1
2013-06-19 11:25:12 INFO:Begin DLL registration for Server2
2013-06-19 11:25:12 INFO:registered cimwin32.dll
2013-06-19 11:25:12 INFO:End DLL registration for Server2

推荐答案

我最终将整个 foreach 变成了一个变量,脚本开始时为:$out = Foreach{.......}.然后我在最后传递变量

I ended up making the entire foreach a variable by begining the script with: $out = Foreach{.......}. I then piped the variable at the end

$out |外文件 $logfile

$out | Out-File $logfile

这是一个示例:

$out = foreach($server in $servers){
    invoke-command -cn $server -scriptblock {
        Param($server)
        "{0} :Begin DLL registration for {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"),$server
    } -ArgumentList $server
}

$out | Out-File $logfile

这篇关于来自 invoke-command 的 Powershell 日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆