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

泄露秘密 提交于 2019-12-11 01:45:26

问题


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

回答1:


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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/29475711/how-to-get-exact-user-name-from-get-mobiledevicestatistics-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!