Solved

Powershell logging from invoke-command

Posted on 2013-06-19
2
1,086 Views
Last Modified: 2013-06-21
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.

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.

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.

Can this even be done?

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
}

Open in new window


 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

Open in new window

0
Comment
Question by:greetrufus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39260919
Write-Host only outputs to the console (or host), so you'd have a hard time getting anything to a log file using that.  You need commands that actually return objects.  You might try something like the following.
$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
        $RegFile = "cimwin32.dll"
        regsvr32 $RegFile /s
        "{0} :Registered {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"),$RegFile
        "{0} :End DLL registration for {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"),$server

    } -ArgumentList $server
}
$out | Out-File $logfile

Open in new window

0
 

Author Comment

by:greetrufus
ID: 39265855
Works brilliantly.  Thanks!!!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question