Looking to suppress "COM Interop is not supported on this platform" error in Exchange Online Script

huangapple go评论69阅读模式
英文:

Looking to suppress "COM Interop is not supported on this platform" error in Exchange Online Script

问题

以下是您要翻译的部分:

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.

这一行 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) | Out-Null 是为了防止脚本挂起,但由于与 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."
MethodInvocationException: /Users/XXX/XXX/MDM.ps1:80:17
行 |
  80 |  …             [System.Runtime.Interopservices.Marshal]::ReleaseComObjec …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | 调用 "ReleaseComObject" 时出现异常,带有 "1" 个参数: "此平台不支持 COM 互操作。"

I have tried adding in try-catch mechanisms, -ErrorAction SilentlyContinue, -WarningAction SilentlyContinue, and a few other variations with no luck. I get this error:

我尝试添加了 try-catch 机制,-ErrorAction SilentlyContinue-WarningAction SilentlyContinue,以及一些其他变化,但都没有成功。我得到这个错误:

ParserError: /Users/XXX/XXX/MDM.ps1:80:83
Line |
  80 |  … nteropservices.Marshal]::ReleaseComObject($item) -ErrorAction:Silentl …
     |                                                     ~~~~~~~~~~~~
     | Unexpected token '-ErrorAction' in expression or statement.
ParserError: /Users/XXX/XXX/MDM.ps1:80:83
行 |
  80 |  … nteropservices.Marshal]::ReleaseComObject($item) -ErrorAction:Silentl …
     |                                                     ~~~~~~~~~~~~
     | 表达式或语句中的未预期标记 '-ErrorAction'。

<details>
<summary>英文:</summary>

I&#39;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:

```none
MethodInvocationException: /Users/XXX/XXX/MDM.ps1:80:17
Line |
  80 |  …             [System.Runtime.Interopservices.Marshal]::ReleaseComObjec …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling &quot;ReleaseComObject&quot; with &quot;1&quot; argument(s): &quot;COM Interop is not supported on this platform.&quot;
```

I have tried adding in `try`-`catch` mechanisms, `-ErrorAction SilentlyContinue`, `-WarningAction SilentlyContinue`, and a few other variations with no luck. I get this error:

```none
ParserError: /Users/XXX/XXX/MDM.ps1:80:83
Line |
  80 |  … nteropservices.Marshal]::ReleaseComObject($item) -ErrorAction:Silentl …
     |                                                     ~~~~~~~~~~~~
     | Unexpected token &#39;-ErrorAction&#39; in expression or statement.
```

</details>


# 答案1
**得分**: 0

```
**`[System.Runtime.InteropServices.Marshal]::ReleaseComObject($item)`是一个_expression(.NET方法调用)**,为了**抑制可能引发的异常**(也称为语句终止错误),您需要使用[`try { ... } catch { ... }`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Try_Catch_Finally):

```
# 仅使用{}作为catch块意味着任何异常都将被*静默忽略*。
try {
  $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item)
} catch {}
```

<sup>注意:`$null = ...`通常是比`... | Out-Null`更高效的替代方法 - 有关详情,请参见[此答案](https://stackoverflow.com/a/55665963/45375)。</sup>


注意:

* 如果需要,异常对象可以通过`catch`块内的[自动`$_`变量](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#_)获得;您可以使用`throw`(无需参数)简单地*重新引发*异常。

* 即使您只是_忽略_异常,它仍然会记录在PowerShell的会话范围错误日志中,即[自动`$Error`变量](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#error)。

---

至于**您尝试的内容**:

> [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) -ErrorAction SilentlyContinue

这会导致语法错误,因为[常见的`-ErrorAction`参数](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_CommonParameters#-erroraction)(就像所有[常见参数](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_CommonParameters)一样,例如`-WarningAction`)仅适用于_命令_,更具体地说,适用于*cmdlets*(例如,`Get-ChildItem`)和*高级](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Functions_Advanced)函数和脚本*(它们的行为类似于cmdlets,但与前者不同,它们是通过*编译代码*(.NET程序集)实现的,而是用PowerShell编写的)。

请注意,表达式和命令分别根据*单独的语法规则*,即*解析模式*进行解析,这些规则在概念上在[about_Parsing](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Parsing)帮助主题中讨论。
```

<details>
<summary>英文:</summary>

&lt;!-- language-all: sh --&gt;

**`[System.Runtime.InteropServices.Marshal]::ReleaseComObject($item)` is an _expression_ (a .NET method call)**, and in order to **silence an _exception_** (aka _statement-terminating error_) it may throw, **you need [`try { ... } catch { ... }`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Try_Catch_Finally)**:

```
# Using just {} as the catch block means that any exception is *quietly ignored*.
try { 
  $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) 
} catch {}
```

&lt;sup&gt;Note: `$null = ...` is a typically better-performing alternative to `... | Out-Null` - see [this answer](https://stackoverflow.com/a/55665963/45375) for details.&lt;/sup&gt;


Note:

* If needed, the exception object is available via the [automatic `$_` variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#_) inside the `catch` block; you an _re-throw_ the exception simply with `throw` (no argument needed).

* Even if you simply _ignore_ the exception, it is still recorded in PowerShell&#39;s session-wide error log, the [automatic `$Error` variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#error).

---

As for **what you tried**:


&gt;     [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) -ErrorAction SilentlyContinue  

This causes a syntax error, because the [common `-ErrorAction` parameter](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_CommonParameters#-erroraction) (like all [common parameters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_CommonParameters), i.e. `-WarningAction` too) only apply to _commands_, more specifically, to *cmdlets* (e.g., `Get-ChildItem`) and *[advanced](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Functions_Advanced) functions and scripts* (which behave like cmdlets, but, unlike the former, which are implemented via *compiled code* (.NET assemblies), are written *in PowerShell*).

Note that expressions and commands are each parsed according to *separate syntax rules* aka *parsing modes*, discussed in the conceptual [about_Parsing](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Parsing) help topic.




</details>



huangapple
  • 本文由 发表于 2023年3月4日 05:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定