[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script for User Name, Email and Office to CSV

Posted on 2009-04-22
7
Medium Priority
?
769 Views
Last Modified: 2012-05-06
I have the following script.  It works just fine for the name and email, but I cannot get the office field to work.  What am I missing here?
# Basic information to run the script
$File = "\\server\files\test\test.csv"
$SearchRoot = "LDAP://OU=Staff,DC=mydomain,DC=com"
 
# Filter to find the things we want
$Filter = "(objectClass=User)"
 
# Connect to the search root
$SearchRootDE = New-Object System.DirectoryServices.DirectoryEntry($SearchRoot)
 
# Set up the search
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($SearchRootDE, $Filter)
$intProp = $Searcher.PropertiesToLoad.Add("name")
$intProp = $Searcher.PropertiesToLoad.Add("mail")
$intProp = $Searcher.PropertiesToLoad.Add("office")
 
# Get the results
$Results = $Searcher.FindAll()
 
# Sort through the results and save them in an array
$Users = @()
ForEach ($Result in $Results) {
  $Users += $Result.Properties | Select-Object `
        @{n="name";e={$_.name}}, `
        @{n="mail";e={$_.mail}}
        @{n="office";e={$_.office}}
}
 
# Write the array to a file
$Contacts | Export-CSV -Path $File

Open in new window

0
Comment
Question by:wdurrett
  • 4
  • 3
7 Comments
 
LVL 10

Author Comment

by:wdurrett
ID: 24208106
here is my updated file.  Still no dice.  The "Office" column turns up blank.
# Basic information to run the script
$File = "\\server\files\test\test.csv"
$SearchRoot = "LDAP://OU=Staff,DC=mydomain,DC=com"
 
# Filter to find the things we want
$Filter = "(objectClass=User)"
 
# Connect to the search root
$SearchRootDE = New-Object System.DirectoryServices.DirectoryEntry($SearchRoot)
 
# Set up the search
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($SearchRootDE, $Filter)
$intProp = $Searcher.PropertiesToLoad.Add("name")
$intProp = $Searcher.PropertiesToLoad.Add("mail")
$intProp = $Searcher.PropertiesToLoad.Add("office")
 
 
# Get the results
$Results = $Searcher.FindAll()
 
# Sort through the results and save them in an array
$Users = @()
ForEach ($Result in $Results) {
  $Users += $Result.Properties | Select-Object `
        @{n="name";e={$_.name}}, `
        @{n="mail";e={$_.mail}}, `
        @{n="office";e={$_.office}}
}
 
# Write the array to a file
$Users |  Export-CSV -Path $File

Open in new window

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24217125

In AD, office is a field called "physicalDeliveryOfficeName". If you add that to your loaded properties, and modify the Select-Object statement you should find it springs to life.

You might be interested in the AD CmdLets Quest made if you end up doing a lot of this:

http://www.quest.com/activeroles-server/arms.aspx

That would allow the script above to be simplified to:

Get-QADUser | Select-Object Name, Mail, Office

Which can then be exported to a CSV if desired.

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24217137

Oh and just in case... The PowerShell TA is here:

http://www.experts-exchange.com/Programming/Languages/Scripting/MSH-Monad/

Might get you a quicker response in future, despite it's extremely out of date area name :)

Chris
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.

 
LVL 10

Author Comment

by:wdurrett
ID: 24219618
Thank you for your response.

This is what i changed and it still comes up blank.
# Basic information to run the script
$File = "\\server\files\test\test.csv"
$SearchRoot = "LDAP://OU=Staff,DC=mydomain,DC=com"
 
 
# Filter to find the things we want
$Filter = "(objectClass=User)"
 
# Connect to the search root
$SearchRootDE = New-Object System.DirectoryServices.DirectoryEntry($SearchRoot)
 
# Set up the search
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($SearchRootDE, $Filter)
$intProp = $Searcher.PropertiesToLoad.Add("name")
$intProp = $Searcher.PropertiesToLoad.Add("mail")
$intProp = $Searcher.PropertiesToLoad.Add("physicalDeliveryOfficeName")
 
 
# Get the results
$Results = $Searcher.FindAll()
 
# Sort through the results and save them in an array
$Users = @()
ForEach ($Result in $Results) {
  $Users += $Result.Properties | Select-Object `
        @{n="name";e={$_.name}}, `
        @{n="mail";e={$_.mail}}, `
        @{n="physicalDeliveryOfficeName";e={$_.physicalDeliveryOfficeName}}
}
 
# Write the array to a file
$Users |  Export-CSV -Path $File

Open in new window

0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 24220245

Try this version.

Chris
# Basic information to run the script
$File = "\\server\files\test\test.csv"
$SearchRoot = "LDAP://OU=Staff,DC=mydomain,DC=com"
 
 
# Filter to find the things we want
$Filter = "(objectClass=User)"
 
# Connect to the search root
$SearchRootDE = New-Object System.DirectoryServices.DirectoryEntry($SearchRoot)
 
# Set up the search
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($SearchRootDE, $Filter)
$intProp = $Searcher.PropertiesToLoad.Add("name")
$intProp = $Searcher.PropertiesToLoad.Add("mail")
$intProp = $Searcher.PropertiesToLoad.Add("physicalDeliveryOfficeName")
 
 
# Get the results
$Results = $Searcher.FindAll()
 
# Sort through the results and save them in an array
$Users = @()
ForEach ($Result in $Results) {
  $Users += $Result.Properties | Select-Object `
        @{n="name";e={$_.name}}, `
        @{n="mail";e={$_.mail}}, `
        @{n="physicalDeliveryOfficeName";e={$_.physicaldeliveryofficename}}
}
 
# Write the array to a file
$Users |  Export-CSV -Path $File

Open in new window

0
 
LVL 10

Author Comment

by:wdurrett
ID: 24220335
Bingo!  Thank you so much!

What did you change?

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24222681

The properties in the result set can only be accessed if you use lower case for the property names. That is "physicaldeliveryofficename" instead of "physicalDeliveryOfficeName". Doesn't matter how they appear in AD, it just seems to dislike mixing case there.

Can't say I know why :) I've never been bothered by it enough to find out, just made me use lower case there ever since I found it :)

Chris
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

829 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