Your PowerGUI Community: Simplify your PowerShell related GUI and script editing needs.


Logging output in powershell scripts

This question is answered
I have been wondering about this...

What is best practice log output (sucess/failure) of a powershell a script ?
Verified Answer
  • note to self : don't assume everyone uses powershell 2!

    the ps1 way is to use trap...


    &{trap {write-host "failure";break}
     $AddGrp = Add-QADGroupMember -Identity 'MyDomain\ADGroup01' -Member 'MyDomain\User01'
     write-host "success"
    }

All Replies
  • You can use the exit keyword with a specific exit code:

    for example (pseudo code):

    if(error)
    {
       exit 0
    }
    else
    {
       exit 1
    }


  • This is a useful technique for logging different error types as well. In ConfigMgr for instance, different exit codes will show up in reports and allow you to see a breakdown of failures across many machines executing a distributed script.

    Beyond that, there is always the event log which is very easy to use within Powershell.
     
  • Indeed, the event log option is recommended since it is recorded in the machine level and can be read remotely by other admins. Another option is text files.
  • thx for that response guys, but i was wondering is there any way to log the error directly thrown by a command. For eg.

    $AddGrp = Add-QADGroupMember -Identity 'MyDomain\ADGroup01' -Member 'MyDomain\User01'

    in above command let say if AdGroup01 or User01 (or both) do not exist then Quest Management shell would generally thow error on screen & not save anything in $AddGrp is there anyway to log these kind of situations to a file.

    I have already tried "Tee-Object" but even that does not work
    eg. $AddGrp = Add-QADGroupMember -Identity 'MyDomain\ADGroup01' -Member 'MyDomain\User01' | Tee-Object -FilePath 'C:\MyCodeLog.txt'

    But above works only in case of success, failures are only dumped on screen
    Message was edited by: sk8er_boi

    Message was edited by: sk8er_boi
  • using try / catch gives you something....

    try{
       $AddGrp = Add-QADGroupMember -Identity 'MyDomain\ADGroup01' -Member 'MyDomain\User01'
       write-host "success"
    }
    catch{
       write-host "failure"
       $error[0] | fl -force
    }

    the format-list -force revelas the error exception class to be

       Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException

    so you could just catch that exception like this :

    catch [Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException]{
       write-host "failure"
       $error[0] | fl -force
    }

    This way you can deal with different error exceptions in different ways.

    regards

    Adam
    http://adadmin.blogspot.com
  • "Try" is not recognised as cmdlet or function !!!

    I'm using Powershell v1.0 , i suppose try / catch is not supported in that, any alternatives other than upgrading (cause that is really pain procedure in my company with TONS of procedures :(
  • note to self : don't assume everyone uses powershell 2!

    the ps1 way is to use trap...


    &{trap {write-host "failure";break}
     $AddGrp = Add-QADGroupMember -Identity 'MyDomain\ADGroup01' -Member 'MyDomain\User01'
     write-host "success"
    }

  • This resolves my original issue thx a lot ! :)

    I don’t want to sound like a pain guy but now i seem to have a new issue, whenever i enter a incorrect user or group (or both) for above command powershell takes LOADS of time to get to next line in the code (about 1.5 to 2 mins) , whereas if all the info i provide is correct it runs within few seconds.
    Is this default behavior of powershell (if yes then is there anything i can to to speed it up?) or has this got something to do with the way AD/ARS is setup @ my organization ?

    thx in advance :)
  • any 1 pls ?
  • Check existing.ps1
    I have tried to improve a perfomance and there is a some issue with searching.
    I have found the fast method (see attach) by

    Get-QADObject -LdapFilter "(sAMAccountName=$account)"
    .... where { $_.edsaDomainNetbiosName -eq $domain }

    The -RootSearch parametr decreases a perfomance.
  • Hello


    I have use the method detailed below, very successfully in all my scripts.

    The function Write-EventLog writes directly into the system Application log - plenty of examples like this on the WEB



    Your actual script should make use of a variable called "ErrorVariable" that will trap Error output. The "if" statement the tests $ErrMesg and if true (ie. not empty - meaning that there is content) will write the error message to the Application event log with and indicate errror by marking it with a red blob.

    If $ErrMesg tests Not True (ie. empty - meaning success) then write a success message and also write it to the Application event log as Information.


    I hope this helps you!

    SamuelC



    ----start of script--------
    function Write-EventLog
    {
    param([string]$msg = "Default Message", [string]$type="Information")
    $log = New-Object System.Diagnostics.EventLog
    $log.set_log("Application")
    $log.set_source("PSscript")
    $log.WriteEntry($msg,$type)
    }



    $AddGrp = Add-QADGroupMember -Identity 'MyDomain\ADGroup01' -Member 'MyDomain\User01' -ErrorVariable "ErrMesg"

    if($ErrMesg)
    {
    Write-EventLog $ErrMesg Error
    $ErrMesg.Clear()
    }
    else
    {
    $Mesg = "AddGrp success"
    Write-Event $Mesg
    }
    -------end script----------------