To the PowerShell

As I am learning some of the benefits (and quirks) to PowerShell, I am finding new ways to take advantage of the language. Besides the enormous user community out there available via Google, there are a number of people all trying to do the same thing. It appears that we are all very excited about the simplicity and complexity of Powershell.

I've been working to revise (yet again) my User export script to write to a single CSV file and to include additional fields. Using the Scripting Guys recent post, I was able to find the Powershell value for when an account was created. Oddly, the same value in VBScript?! :) Next I used my sample from my LastLogonTime field to span several OUs into a single CSV file. The normal export-csv does not appear to span, simply over-write.

The code relies on the Quest ARMS solution found here. You need to save this script to a file, then run it inside the Quest Powershell Shell.

Going through the code, I get started with a basic introduction and creating the CSV file..

#Get_User_info.ps1
# Export User Information for a single OU.
# Creates a CSV file in the root, based on today's date.
# Author: Eric Woodford
# Date: 10-10-2007
#
#Creates dated file and specifies header for CSV.
$strDate = get-date -uformat "%Y-%m-%d"
$CSVFilePath = 'c:\My_users_'+$strDate+'.csv'
$strOutputString = "SAMAccountName,EmployeeID,DistinguishedName,Primary SMTP,User Principal Name,First Name,Last Name,Date Created,Proxy Addresses"
$strOutputString | out-file -filepath $CSVFilePath -encoding ascii

Now, the code starts going through the OU and compiling a list of names. Once the user accounts are pulled, it starts exporting them to the CSV file. The next bit of code, can be modified to include more or less values. For example, you can remove the mailbox filter (-ObjectAttribute @{mail='*'}) to get all accounts, not just mail enabled. You can change the SearchRoot value to be a root OU, or specific OU. To change the OU, pull up the properties for your new destination folder, and paste in the OU's objectvalue.


$users = get-qaduser -dontusedefaultincludedproperties -ObjectAttributes @{mail='*'} -includedproperties sAMAccountName,employeeid,distinguishedname,mail,userprincipalname,givenname,sn,whencreated,proxyaddresses -searchroot 'Corp.local/MyUsers/_A - G' -searchscope 'subtree' -serializevalues -sizelimit 0 |sort-object -property 'sn'

This final part, exports the users to the CSV file. There is some troubleshooting done here to clean-up the entries a little. I wanted to do comparison's of these accounts, with users on another system, so I converted as many values as possible to lowercase. Plus, we have a several OUs with contact entries in them, which do not have a User Principal Name value associated with them. This script does not export those entries to the CSV.


foreach ($user in $users) {
$user.distinguishedname #echos current user to screen.
if ($user.userprincipalname -eq $null) {}
else {
if ($user.mail -eq $null) {
#Mail and ProxyAddress fields are blanked out.
$strOutputString = $user.SAMAccountName.toLower() + "," + $user.employeeID + ",""" + $user.distinguishedname + """, ," + $user.userprincipalname.tolower() + "," + $user.givenname + "," + $user.sn + "," + $user.whencreated + ", "
}
else {
$strOutputString = $user.SAMAccountName.toLower() + "," + $user.employeeID + ",""" + $user.distinguishedname + """," + $user.mail.toLower() + "," + $user.userprincipalname.toLower() + "," + $user.givenname + "," + $user.sn + "," + $user.whencreated + ",""" + $user.proxyaddresses.toLower() + """ "
}
$strOutputString | out-file -filepath $CSVFilePath -encoding ascii -append
}
}

I'll keep posting updates to this script as I add something new. Check it out and have fun.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options