none
Output to a log file successful operations RRS feed

  • Question

  • hello,
    I have a script that runs every night and add users to a recipient group
    What I would like to implement in the script is a log for every user that is added.
    The script is fairly simple:

    $group="RecipientsGroup" $Recipients = get-recipient -ResultSize Unlimited -OrganizationalUnit 'OU=City,OU=Organization,DC=SubDomain,DC=Domain,DC=com' | select name, alias #Add recipients to the group defined in $group foreach ($item in $Recipients) { Add-DistributionGroupMember -Identity $group -Member $item.alias -ea "silentlycontinue"

    }


    what I would like is, if there is no error, write a line in a log file with a timestamp and the user name added to which recipient group
    Been looking at Try/Catch but I am not sure how to implement that in my script
    D
    thanks for any suggestion

    Wednesday, October 1, 2014 11:28 AM

Answers

  • Try this:

    $Logfile = "c:\logboom.txt.txt"
    
    $group="RecipientsGroup"
    $Recipients = get-recipient -ResultSize Unlimited -OrganizationalUnit 'OU=City,OU=Organization,DC=SubDomain,DC=Domain,DC=com'
    #Add recipients to the group defined in $group
    foreach ($item in $Recipients)
    {
    	Try
    	{
    		Add-DistributionGroupMember -Identity $group -Member $item.alias -ea "silentlycontinue"
    		
    		#Get the time
    		$TimeStamp = (get-date).ToString()
    		
    		#Put the String together with the timestamp
    		$Data = $TimeStamp + " " + "$($Item.Name) was added to $Group"
    		
    		#Output it to the file
    		Out-File -FilePath $Logfile -InputObject $Data -Append
    	}
    	Catch
    	{
    		#Get the time
    		$TimeStamp = (get-date).ToString()
    		
    		#Put the String together with the timestamp
    		$Data = $TimeStamp + " " + "Failed to add $($Item.Name) to $Group"
    		
    		#Output it to the file
    		Out-File -FilePath $Logfile -InputObject $Data -Append
    	}
    }


    DJ Grijalva | MCITP: EMA 2007/2010 SPA 2010 | www.persistentcerebro.com

    Wednesday, October 1, 2014 8:12 PM

All replies

  • Try this:

    $Logfile = "c:\logboom.txt.txt"
    
    $group="RecipientsGroup"
    $Recipients = get-recipient -ResultSize Unlimited -OrganizationalUnit 'OU=City,OU=Organization,DC=SubDomain,DC=Domain,DC=com'
    #Add recipients to the group defined in $group
    foreach ($item in $Recipients)
    {
    	Try
    	{
    		Add-DistributionGroupMember -Identity $group -Member $item.alias -ea "silentlycontinue"
    		
    		#Get the time
    		$TimeStamp = (get-date).ToString()
    		
    		#Put the String together with the timestamp
    		$Data = $TimeStamp + " " + "$($Item.Name) was added to $Group"
    		
    		#Output it to the file
    		Out-File -FilePath $Logfile -InputObject $Data -Append
    	}
    	Catch
    	{
    		#Get the time
    		$TimeStamp = (get-date).ToString()
    		
    		#Put the String together with the timestamp
    		$Data = $TimeStamp + " " + "Failed to add $($Item.Name) to $Group"
    		
    		#Output it to the file
    		Out-File -FilePath $Logfile -InputObject $Data -Append
    	}
    }


    DJ Grijalva | MCITP: EMA 2007/2010 SPA 2010 | www.persistentcerebro.com

    Wednesday, October 1, 2014 8:12 PM
  • Hi,

    this is great, thank you.

    Only one little problem, now my log is showing "UserX" was added to the "GroupX" when in reality the user is in that group already.

    What I would like is:

    1. script runs

    2. if an error is generated (Add-DistributionGroupMember returns an error if the user is already in the group) nothing is written into the log

    3. If no error is generated that means a user is successfully added to a group, therefore write in the log "UserX" was added to the "GroupX"

    thank you very much again for your help, this is really great

    D

    Thursday, October 2, 2014 6:50 AM