Search code examples
powershellexchange-serverexchange-server-2013

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

Solution

  • 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.