Monday, May 14, 2012

Preparing scripts for Exporting details from active directory or Exchange

Observed many of my colleagues looking for ready  scripts  in internet for exporting details from AD / Exchange.
Infact its very easy using cmdlets / AD query etc. If you know little bit of excel vlookup / pivot table  you can do comparisons / appending etc easily on output files.

Eg: If you want to export any attributes of objects ( here users ), you can use any of below.
1. AD query ( in AD users & computers) ( eg: you can go for User with business phone present., right click on results & do add /remove columns to add required fields, select all & export to csv)
2. csvde -d "DC=domainname,DC=COM" -f result.csv -r “(&(objectClass=user)(objectCategory=person)” -l "list of attributes seperated in comma "
3. dsquery  / dsget user commands
4. Powershell ( cmdlets like Get-user OR Get-mailbox  etc )

powershell is always easy.i will brief that below,

first simply run the command to get all users details & check if required attributes are there.( becoz, Few of attributes listed by Get-User may not be listing by Get-mailbox or may be a different attribute name in AD & exchange )

Eg: First run below command to get a list of every users each attribute,

Get-User -Resultsize unlimited |fl

if need to execute against only a server / database / OU use corresponding filter
Get-User -OrganizationalUnit   "OU identity" -ResultSize Unlimited |fl

press CTRL+C & verify if required attributes listing,
eg, if you need Display name, email ID ,designation, phone -  type those attribute names properly in below command( Ensure to mention emailid or identity kind of unique ID always as a reference )

Get-user -Resultsize unlimited |ft Identity,DisplayName,WindowsEmailAddress,Title,MobilePhone,Phone

if you want to write the output of above command to a csv named result.csv

Get-user -Resultsize unlimited |ft Identity,DisplayName,WindowsEmailAddress,Title,MobilePhone,Phone >>result.csv

Do you want to work on specific input ?

do create an input file ( eg:input.csv), you can do it from excel / notepad etc.

ensure to have a singleword label in  first row & values ( input for which you need to perform activity ) below, 
preferred to have unique type attribute in first column, eg: email ID or identity.

sample as below
emailid                                         name                         xyz                        abc
Now if you want to export details of only these user IDs listed in above input.csv file, use below command

import-csv input.csv |ForEach { Get-mailbox $_.emailid |ft DisplayName,primarysmtpaddress,Identity,Alias,EmailAddresses >>result.csv }

Hope you understood & you can export required attributes going forward, every exchange cmdlets /powershell commands are easy like this.
if errors verify the syntax of command & correct.

No comments:

Post a Comment