Need to delete mobile device access to 365 Mailboxes

Roger Leech
Roger Leech used Ask the Experts™
on
I'm trying to write a simple PS script to remove EAS partnerships to a mailbox.  This only needs to be done 1 by 1, but Ideally I could import a CSV for it.  The CSV import I think I can do, but I'm having trouble with the 1 at a time script.  I'm really basic when it comes to PS so I'd appreciate any help - for each and variables are super confusing.  

This is what I have:  
$User = Read-Host -Prompt "Who's Mobile Devices are you going to nuke?  Enter username"
$Devices = Get-MobileDevice -Mailbox $user
ForEach ($Device in $Devices) 
    {
        Remove-MobileDevice $Device
     }
Clear-Variable User,Name,Devices

Open in new window

But I keep getting this:
Cannot process argument transformation on parameter 'Identity'. Cannot convert the "Persons Name\ExchangeActiveSyncDevices\iPad§Q5F3123456KHLAGK41P7VO" value of type 
"Deserialized.Microsoft.Exchange.Data.Directory.SystemConfiguration.MobileDevice" to type "Microsoft.Exchange.Configuration.Tasks.MobileDeviceIdParameter".
    + CategoryInfo          : InvalidData: (:) [Remove-MobileDevice], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Remove-MobileDevice
    + PSComputerName        : outlook.office365.com

Open in new window


Then I'll do
Set-CASMailbox username -ActiveSyncDeviceIDs $null

Open in new window

as a finisher.  

What do you think of this process?  

Since I know you're curious, we just migrated to G-Suite and I want to prevent people from accidentally accessing their 365 mailbox on mobile.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Enterprise Business Process Architect
Commented:
Change this line
Remove-MobileDevice $Device

Open in new window

to
Remove-MobileDevice $Device.DeviceID

Open in new window

and I think your error will go away.

$Device is a collection, not merely an identifier.

Author

Commented:
Ahh - so simple.  Thanks that worked.  Here's the new script because I've never understood why the CASMailbox info relating to ActiveSync devices is different than the mobile-device command, but I think adding this command makes it more thorough.  I'll accept your comment Darell and award points.  Thanks!
$User = Read-Host -Prompt "Who's Mobile Devices are you going to nuke?  Enter username"
$Devices = Get-MobileDevice -Mailbox $user
ForEach ($Device in $Devices) 
    {
        Remove-MobileDevice $Device.Device.ID
     }
Set-CASMailbox $User -ActiveSyncAllowedDeviceIDs $Null
Clear-Variable User,Devices

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial