I need scripting assistance

when I type the following I get everything in distinguishedname

[PS] >Get-ActiveSyncDevice  | select friendlyname, distinguishedname | ft
FriendlyName                                                                              DistinguishedName
------------                                                                              -----------------
BlackBerry Z30                                                                            CN=BlackBerry§BB2BF9C62C,CN=ExchangeActiveSyncDevices,CN=Christopher.Ellis,OU=USERS,OU..

I would like to get only a section of the distinguishedname (not the whole line) I have more than 300 devices

What I would like my output to be (script would select CN=User only - stripping out other info

FriendlyName    DistinguishedName
------------     ------------
BlackBerry Z30    Christopher.Ellis

Thank you for your help

 

  • There are so many different ways to do this it's not even funny.  Here is one example. Please ignore the line wrap on the $dn declaration.

    $dn = "CN=BlackBerry§BB2BF9C62C,CN=ExchangeActiveSyncDevices,CN=Christopher.Ellis,OU=USERS,OU=GOOSEBERRY,DC=MYCOMPANY,DC=CO,DC=UK"
    $userName = ((($dn -split ',')[2]).Split('='))[1]
    $userName

  • Bob - The script you propose has the users name in the variable. The idea of the script is to get the users name from the distinguished name. I stated in my initial request that  i have over three hundred active sync devices.  I am not a scriptor so i would want the most simple way to script this.  all i want to extract is the friendlyname   AND User Name from distinguished name field .  Can you offer an alternative ? thanks

  • If you're asking me to write a script for you, then the answer is no. Perhaps someone else here might want to take that on. Mine was simply a code sample to show how to extract the relevant portion of a sample DN. How you populate the DN is entirely up to you (Get-ActiveSyncDevice, Get-ADUser, CSV file, etc.), just as what you do with the results is up to you.

  • Bob, I am not asking you to write a script but point me in the right direction with a sample that would work... I have dealt with you on different occasions, I have found that you are not very helpful. This is a forum where people share their knowledge and help out people who need help. I have found all people on this forum very helpful (you being the exception) if you don't want to help others and share your knowledge, why do you bother posting replies which don't help.

  • If you take the code that Bob gave you and use that to create a calculated property with select-object, I think that is what you need. technet.microsoft.com/.../ff730948.aspx

    And you're not being very fair to Bob, he did give you the code that'll do the work. He posts to at least three different powershell forums, and I can say with surety that he spends a great deal of time helping others. 

  • If you have looked at the notes on the cmdlet you are using here: https://technet.microsoft.com/en-us/library/dd335068(v=exchg.160).aspx, there are other approaches. For example the Mailbox parameter vs just using the stand alone Select you are using.

    And not that in E2K13, and beyond, the Get-ActiveSyncDevice cmdley is going away to be replaced by Get-MobileDevice

    But if you approached this a bit differently, you could just do something like this

    [PS] C:\>Get-ActiveSyncDevice | select devicetype, FriendlyName, Distinguishedname, UserDisplayName | out-gridview -PassThru

    CN=BlackBerry§BB2BF9C62C,CN=ExchangeActiveSyncDevices,CN=Christopher.Ellis,OU=USERS,OU=GOOSEBERRY,DC=MYCOMPANY

    The UserDisplayName would normally correlate to the DistinguishedName

    DeviceType                    FriendlyName                  DistinguishedName             UserDisplayName

    Blackberry                      BlackBerry Z30               CN=BlackBerry§BB2BF9C6...        .../Christopher.Ellis

    You see in the grid the DisplayName shows the already separate CN. The parsing of that last bit is a simple string split, like above, something like ...

    $UserDN = Get-MobileActiveSyncDevice | Select DeviceType, Distinguishedname, UserDisplayName 

    $TargetRecords = foreach ($DeviceRecord in $UserDN){Write-Host ($DeviceRecord).DeviceType ($DeviceRecord).Frien
    dlyName ($DeviceRecord.UserDisplayName).Split("/")[2]}

    When I run this on my lab environment I get stuff like

    iPhone Kang's iPhone 5 Luke Skywalker
    iPhoneSimulator iPhone Simulator Mr Spock
    iPad Acool iPad Cpt Kirk
    iPad Good Work iPad Han Solo
    iPhone Christopher's iPhone Princess Leia
    iPad Lore's iPad Chubaka
    iPhone Stuart's iPhone C3PO
    iPhone Jorge's iPhone 5S R2D2
    iPad Jorge's iPad Air lando calrissian
    iPhone Stuart Jackson's iPhone Yoda

    FYI, the new Get-MobileDevice does not offer a split option.

    Again, all the above is quick and dirty, kind of ugly stuff. So, test, test, test, change, change, change as needed.

    A bit more time would make this more elegant. But I leave that to your homework assignment.  8^)

     

  • cmje said:
    I have dealt with you on different occasions, I have found that you are not very helpful.

    Wow.  Not really sure what that means.  I looked back over our joint history and it seems that we only intersected on two posts.

    powershell.com/.../42822.aspx
    Your final reply was:  It worked exactly as you stated  - the only thing I changed was the servers.txt path.  Thank you.

    powershell.com/.../15304.aspx
    On this one Jaap took over as the subject matter expert so I went into read-only mode.

    Sorry you find me offensive.  I have added you to my "Do not disturb" list so as not to bother you.

    Best wishes on your PowerShell journey.