Mansueli
Mansueli

Reputation: 6994

Iterate through mailboxes on Get-MobileDeviceStatistics

I am working on a script to remove all EAS devices from our exchange server. (To force use of REST-based only clients)

# Login
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -

AllowRedirection
Import-PSSession $Session

#Removing EAS devices
#
$Mailboxes = Get-Mailbox -ResultSize Unlimited
Foreach ($box in $Mailboxes){ $EASDevices = Get-MobileDeviceStatistics -Mailbox $box | Where-Object {$_.ClientType -like "EAS"}; 
EASDevices | foreach {$Guid = $_.guid.ToString(); Remove-MobileDevice -id $Guid}} 

#@TODO add -Confirm:$False when it is working

I get the following error:

Cannot process argument transformation on parameter 'Mailbox'. Cannot convert value "Support Account" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "Cannot convert hashtable to an object of the following type: Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not supported in restricted language mode or a Data section."

My question is how to get all mailboxes, then iterate through Get-MobildeDeviceStatistics ?

I couldn't get much further even searching online such as :

https://social.technet.microsoft.com/Forums/exchange/en-US/1fea011d-484d-4b0a-badf-6f5fcc3ae097/powershell-mobile-devices-full-list-information?forum=exchange2010

https://social.msdn.microsoft.com/Forums/office/en-US/1765335e-fd1c-4886-9fac-b2f15d5a493a/hashtabletoobject-conversion-is-not-supported?forum=exchangesvrdevelopment

Upvotes: 1

Views: 818

Answers (2)

Erwin
Erwin

Reputation: 51

Why not use:

Get-MobileDevice -ResultSize unlimited | where {$_.clienttype -eq "EAS"} | Remove-MobileDevice

This effectively removes all EAS type partnerships from your system.

Upvotes: 1

Ranadip Dutta
Ranadip Dutta

Reputation: 9173

You need to use the PSSnapin Exchange for this. I hope this fulfills your requirement.

Add-PSSnapin exchange -erroraction SilentlyContinue;
$Mailboxes = Get-Mailbox -ResultSize Unlimited;
foreach ($box in $Mailboxes)
{ 
$EASDevices = Get-MobileDeviceStatistics -Mailbox $box | Where-Object {$_.ClientType -like "EAS"}; 
$EASDevices | %{$Guid = $_.guid.ToString(); Remove-MobileDevice -id $Guid}
}

Upvotes: 0

Related Questions