Tuesday, November 20, 2007

PowerShell AD/v-card utility

Recently, I wanted to grab someone's contact details out of Active Directory, save them as a v-card and send the v-card to a friend. It's not so trivial to do that manually so I wondered how it might be done in PowerShell - and, I'll admit, how I could get someone else to build the utility for me - so I talked to our resident PowerShell guru Dmitry Sotnikov.

Dmitry quickly whipped up a script for me, ran it through his powerful quality control process ("Here, see if this works!) and I was off to the races. Dmitry blogged about the v-card tool and recently told me that there's been significant downloads of it. It was even picked up on both the MSDN PowerShell and Windows PowerShell blogs.

Check it out...

Our local identity management guru Jackson Shaw tasked me with giving him an easy way to export contact information from corporate address book so you can then send it to someone for their reference. The standard format for Outlook to import contact information is vCard, but the problem is that Outlook can export to vCard only personal contacts, but not GAL entries. Needless to say, PowerShell is the answer.

This is the command-line which solves the task:

Get-QADUser "Dmitry Sotnikov" Out-vCard

This will locate a user in your AD whose name is "Dmitry Sotnikov" (which probably means you work for Quest) and create a file "Dmitry Sotnikov.vcf" at the c:\ drive root.

If you want to export all members of a DL - this will work too:

Get-QADGroupMember DL.ProjectA Out-vCard

This will create a vCard for each DL member.

And because it only reads data from your Active Directory you don’t need any administrative privileges. This will work for any domain user.

To make this work you need to:

Install PowerShell and AD cmdlets.
Copy/paste the following function into PowerShell command-line shell before running the commands or add it to your profile (My Documents/WindowsPowerShell/profile.ps1):
function Out-vCard {
$input ForEach-Object {

$filename = "c:\" + $_.Name + ".vcf"
Remove-Item $filename -ErrorAction SilentlyContinue
add-content -path $filename "BEGIN:VCARD"
add-content -path $filename "VERSION:2.1"
add-content -path $filename ("N:" + $_.LastName + ";" + $_.FirstName)
add-content -path $filename ("FN:" + $_.Name)
add-content -path $filename ("ORG:" + $_.Company)
add-content -path $filename ("TITLE:" + $_.Title)
add-content -path $filename ("TEL;WORK;VOICE:" + $_.PhoneNumber)
add-content -path $filename ("TEL;HOME;VOICE:" + $_.HomePhone)
add-content -path $filename ("TEL;CELL;VOICE:" + $_.MobilePhone)
add-content -path $filename ("TEL;WORK;FAX:" + $_.Fax)
add-content -path $filename ("ADR;WORK;PREF:" + ";;" + $_.StreetAddress + ";" + $_.PostalCode + " " + $_.City + ";" + $_.co + ";;" + $_.Country)
add-content -path $filename ("URL;WORK:" + $_.WebPage)
add-content -path $filename ("EMAIL;PREF;INTERNET:" + $_.Email)
add-content -path $filename "END:VCARD"
}
}

Note that the script is something I put together in 15 minutes to help Jackson, so it still needs a few improvements when I have time:

Need to add an optional parameter for the output folder.
Need to actually look at vCard spec to make sure all attributes translate right.
Need to look whether I need to check whether attributes are present. Does vCard format permit empty values or should their keys be in that case omitted?
Anyways, this seems to solve the task for now, I hope I have a few hours later to make it perfect. Feel free to do so yourself if you are interested.


Technorati Tags:
, , ,

No comments: