I've been working on a script to remove mobile devices from Exchange Online. Everything else works fine except this one bit:
foreach ($item in $userdevices) {
Remove-MobileDevice -Identity $item -whatif
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) | Out-Null
}
This line [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) | Out-Null
is required for the script not to hang, but it outputs the error due to incompatibility with Mac.
The error I receive is:
MethodInvocationException: /Users/XXX/XXX/MDM.ps1:80:17
Line |
80 | … [System.Runtime.Interopservices.Marshal]::ReleaseComObjec …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "ReleaseComObject" with "1" argument(s): "COM Interop is not supported on this platform."
I have tried adding in try
-catch
mechanisms, -ErrorAction SilentlyContinue
, -WarningAction SilentlyContinue
, and a few other variations with no luck. I get this error:
ParserError: /Users/XXX/XXX/MDM.ps1:80:83
Line |
80 | … nteropservices.Marshal]::ReleaseComObject($item) -ErrorAction:Silentl …
| ~~~~~~~~~~~~
| Unexpected token '-ErrorAction' in expression or statement.