2

I have a small script to get all user mobile devices info from exchange 2013 server.

Get-Mailbox -ResultSize Unlimited | 
ForEach {Get-MobileDeviceStatistics -Mailbox:$_.Identity} |
Select-Object @{label="User" ; expression={$_.Identity}},DeviceOS, lastsuccesssync

I just want to get an exact user name instead of a path in AD. How can I do it in expression={?}

Here is another script to do it, it gives me the user name, but all devices belongs to user are not in separated lines, they all in one line...

$EASMailboxes = Get-CASMailbox -Filter {HasActiveSyncDevicePartnership -eq $True -and DisplayName -notlike "CAS_{*"} | Get-Mailbox
$EASMailboxes | Select-Object DisplayName, PrimarySMTPAddress, @{Name="Mobile Devices";Expression={(Get-MobileDeviceStatistics -Mailbox $_.Identity).DeviceOS}} | 
Out-GridView
Root Loop
  • 3,004
  • 9
  • 46
  • 72
  • What does the username currently look like? Can you update the question with that? – arco444 Apr 06 '15 at 18:00
  • it looks like this `domain.com/Users/OU/UserName/ExchangeActiveSyncDevices/iPhone`. So I am using this `expression={$_.Identity -split "/" | select -last -3}` to get the name , but it gives me all last 3 words...I just need the 3rd from last – Root Loop Apr 06 '15 at 18:06
  • In that one case it would not be an issue but are all usernames in that same ou? You could get it from the `Get-Mailbox` as you already know it. – Matt Apr 06 '15 at 18:07
  • @Matt, Yes, you are right , that will be another issue. I updated another script, this one gives me the user name but all devices belongs to the user will be in one line, I would have each device in seperated lines – Root Loop Apr 06 '15 at 18:10

3 Answers3

4

I don't have the environment to test this but is this not what you are looking for ?

Get-Mailbox -ResultSize Unlimited | ForEach {
    $user = $_.SamAccountName
    Get-MobileDeviceStatistics -Mailbox:$_.Identity |
    Select-Object @{label="User" ; expression={$user}},DeviceOS, lastsuccesssync
}

That should output the user for every device they own on its own line. You could then easily export this to Export-CSV or some such thing that way.

We save the $user so it is available later in the pipe. Could also have used Add-Member but the result would have been the same.

Matt
  • 45,022
  • 8
  • 78
  • 119
1

If you have the identity field, which looks like this

domain.com/Users/OU/UserName/ExchangeActiveSyncDevices/iPhone

Then to split on the / and get the third result, you simply request:

$_.Identity.Split("/")[3]

>UserName

PowerShell begins indexing with number zero, so to request the fourth entry in the list, we request index number 3.

Update

OP mentioned that the OU level might vary, meaning that he couldn't count on a fixed position to request the Index. In order to accomodte that scenario, try this method, which will look for the index of ExchangeActiveSyncDevices and then pick the index position before that.

$_.Identity.Split('/')[($_.Identity.Split('/').Indexof('ExchangeActiveSyncDevices')-1)]
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
-1
Get-Mailbox -resultsize unlimited|foreach {Get-MobileDeviceStatistics -Mailbox:$_.identity} |Select-Object @{l="user";e={$_.Identity.parent.parent.name}}, lastsuccesssync
on e2k13
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
rsw
  • 1