?
Solved

Report all the informations about powershell script

Posted on 2014-11-20
8
Medium Priority
?
102 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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 this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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