Solved

Report all the informations about powershell script

Posted on 2014-11-20
8
97 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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