Solved

Report all the informations about powershell script

Posted on 2014-11-20
8
94 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now