Solved

Report all the informations about powershell script

Posted on 2014-11-20
8
96 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Determining unused mailboxes in Office 365 8 50
Programmatically signing Word macros 4 61
how to pick specific file from ftp 13 39
will this script work on w2k3 server? 2 11
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…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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 …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

22 Experts available now in Live!

Get 1:1 Help Now