Root Loop
Root Loop

Reputation: 3162

how to get exact user name from get-mobiledevicestatistics in powershell

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

Upvotes: 2

Views: 8077

Answers (3)

rsw
rsw

Reputation: 1

Get-Mailbox -resultsize unlimited|foreach {Get-MobileDeviceStatistics -Mailbox:$_.identity} |Select-Object @{l="user";e={$_.Identity.parent.parent.name}}, lastsuccesssync
on e2k13

Upvotes: -1

Matt
Matt

Reputation: 46730

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.

Upvotes: 4

FoxDeploy
FoxDeploy

Reputation: 13567

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)]

Upvotes: 1

Related Questions