Solved

Report all the informations about powershell script

Posted on 2014-11-20
8
101 Views
Last Modified: 2014-11-20
Hi!

I come back to you for an additionnal question.

I have this script:
Get-ADUser -filter * -Searchbase 'OU=Test,DC=contoso,DC=com' | ? {$_.sAMAccountname -cmatch'[A-Z]'}  | Select sAMaccountname | Export-csv -path C:\Users\me\username.csv -NoTypeInformation

Import-Csv C:\Users\me\username.csv | foreach {
$samoriginal = $($_.samaccountname).tolower()
$samlower = $($_.samaccountname).tolower()
$samlower = $samlower + "1"
Set-ADUser -identity $_.samaccountname -samaccountname $samlower -UserPrincipalName "$($samlower)@contoso.com"
Set-ADUser -identity $samlower -samaccountname $samoriginal -UserPrincipalName "$($samoriginal)@contoso.com" 

Open in new window


This script simply search in AD, every users with logon account in uppercase and convert it to lowercase.

Now, i had some errors in the execution of the script with some users.
I wanna extract a report file (csv or txt), with informations of every actions performed by the script.
In this report file i want report successful actions and report every errors.
I tried the TRY/CATCH/FINALLY. But there is no report about all errors and successful actions.

For example i want a log like that:
J.smith is now in lowercase.
R.Donald error : $errortype

Open in new window


Thanks in advance.
0
Comment
Question by:Pinpindesbois
[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
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40454672
Here is an example of using try catch for one of your set command.. Same can be repeated for all commands and the error message can be customized as per your requirement.
Try{
$sam = $_.samaccountname
Set-ADUser -identity $sam -samaccountname $samlower -UserPrincipalName "$($samlower)@contoso.com"

#Success Message

	New-Object PSObject -Property @{
	User = $sam
	Status = "Lower Success"
	}
}Catch{

#Failed Message

	New-Object PSObject -Property @{
	User = $sam
	Status = "Lower Failed $($_.exception)"
	}
}

Open in new window


To export the result to a csv file, you can pipe the result to Export-csv command. Let me know if you have any questions..
0
 

Author Comment

by:Pinpindesbois
ID: 40454732
For pipe the export, the command should be after the "Finally {" ?

Thank a lot for your help, will test it.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40454766
Export-csv should be blaced out of the foreach loop..

For example..
Import-Csv C:\Users\me\username.csv | foreach {
$samoriginal = $($_.samaccountname).tolower()
$samlower = $($_.samaccountname).tolower()
$samlower = $samlower + "1"

Try{
$sam = $_.samaccountname
Set-ADUser -identity $sam -samaccountname $samlower -UserPrincipalName "$($samlower)@contoso.com"

#Success Message

	New-Object PSObject -Property @{
	User = $sam
	Status = "Lower Success"
	}
}Catch{

#Failed Message

	New-Object PSObject -Property @{
	User = $sam
	Status = "Lower Failed $($_.exception)"
	}
}

#place rest of the code inside the foreach loop here
} | Export-csv c:\report.csv -nti

Open in new window

0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

Author Comment

by:Pinpindesbois
ID: 40454836
Oh well, ok my bad, i forgot this "}"...
Still i search on google about New-Object and PSObject... I don't understand how with this code, you'll get "sucess message" part. I can understand for the "Catch{" but not for the New-Object, what does it mean exactly?
0
 

Author Closing Comment

by:Pinpindesbois
ID: 40454838
Perfect, quick... And i always learn more about powershell.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40454847
New-Object PSObject -Property @{....} is used to create a custom object. Which I used as a result object which is send to pipeline in this script.

There are many methods in PowerShell to create a custom object. You may refer following articles for details..

http://technet.microsoft.com/en-us/magazine/hh750381.aspx
http://technet.microsoft.com/en-us/library/ff730946.aspx
0
 

Author Comment

by:Pinpindesbois
ID: 40454849
Thank you sir!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40454850
You are welcome!.. :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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