O R G A N I C / F E R T I L I Z E R: misc: dsquery vs powershell...

Nov 8, 2007

misc: dsquery vs powershell...

as a part of trying to familiarize myself with powershell, i figured converting some of my favorite dsquery commands to it would be as good of a measure as any. the problem was, i had a hell of a time figuring it out! thankfully, hal was nice enough to help out... in order to get displayname and streetaddress from dsquery, you'd use a command like this:
dsquery user -samid myUser | dsget user -display
oh wait a second... there is no switch for streetaddress. all you'll get is something like this:
  My User (Test)
let's try that again...
dsquery * -filter "(&(objectcategory=person)(samaccountname=myUser))" -attr displayname streetaddress
in my case, i have multiple lines in my streetaddress attribute, which throws off the entire format. this is something i wanted to avoid so i seeked powershell as the answer (instead of writing a vbscript to handle it.)
  displayname                 streetaddress
  My User (Test)              2000 My Test Avenue
Suite 200
here's how you'd pull the information from powershell (providing you use the quest add-ons)...
get-qaduser testdomain\myUser | format-table displayname,streetaddress

DisplayName                                  StreetAddress
-----------                                  -------------
My User (Test)                               2000 My Test Avenue...
already much better. however, the street address is truncated. so let's try again.
get-qaduser testdomain\myUser | format-table -wrap displayname,streetaddress
now we get the full street address, but as you can see, the cr/lf is carried over. to get rid of it, we'll insert a replace statement.
DisplayName                                  StreetAddress
-----------                                  -------------
My User (Test)                               2000 My Test Avenue
                                             Suite 200
get-qaduser testdomain\myUser | format-table -wrap displayname,@{label = "StreetAddress";expression = {$_.streetaddress -replace "`n",", "}}
resulting in this much nicer formatted version...
DisplayName                                  StreetAddress
-----------                                  -------------
My User (Test)                               2000 My Test Avenue, Suite 200