Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell logging from invoke-command

Posted on 2013-06-19
2
Medium Priority
?
1,307 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 41

Accepted Solution

by:
footech earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

597 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