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

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

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:

我收到的错误是:

  1. MethodInvocationException: /Users/XXX/XXX/MDM.ps1:80:17
  2. Line |
  3. 80 | [System.Runtime.Interopservices.Marshal]::ReleaseComObjec
  4. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. | Exception calling "ReleaseComObject" with "1" argument(s): "COM Interop is not supported on this platform."
  1. MethodInvocationException: /Users/XXX/XXX/MDM.ps1:80:17
  2. |
  3. 80 | [System.Runtime.Interopservices.Marshal]::ReleaseComObjec
  4. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. | 调用 "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,以及一些其他变化,但都没有成功。我得到这个错误:

  1. ParserError: /Users/XXX/XXX/MDM.ps1:80:83
  2. Line |
  3. 80 | nteropservices.Marshal]::ReleaseComObject($item) -ErrorAction:Silentl
  4. | ~~~~~~~~~~~~
  5. | Unexpected token '-ErrorAction' in expression or statement.
  1. ParserError: /Users/XXX/XXX/MDM.ps1:80:83
  2. |
  3. 80 | nteropservices.Marshal]::ReleaseComObject($item) -ErrorAction:Silentl
  4. | ~~~~~~~~~~~~
  5. | 表达式或语句中的未预期标记 '-ErrorAction'
  6. <details>
  7. <summary>英文:</summary>
  8. 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
}

  1. 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.
  2. The error I receive is:
  3. ```none
  4. MethodInvocationException: /Users/XXX/XXX/MDM.ps1:80:17
  5. Line |
  6. 80 | … [System.Runtime.Interopservices.Marshal]::ReleaseComObjec …
  7. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. | Exception calling &quot;ReleaseComObject&quot; with &quot;1&quot; argument(s): &quot;COM Interop is not supported on this platform.&quot;
  9. ```
  10. I have tried adding in `try`-`catch` mechanisms, `-ErrorAction SilentlyContinue`, `-WarningAction SilentlyContinue`, and a few other variations with no luck. I get this error:
  11. ```none
  12. ParserError: /Users/XXX/XXX/MDM.ps1:80:83
  13. Line |
  14. 80 | … nteropservices.Marshal]::ReleaseComObject($item) -ErrorAction:Silentl …
  15. | ~~~~~~~~~~~~
  16. | Unexpected token &#39;-ErrorAction&#39; in expression or statement.
  17. ```
  18. </details>
  19. # 答案1
  20. **得分**: 0
  21. ```
  22. **`[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):
  23. ```
  24. # 仅使用{}作为catch块意味着任何异常都将被*静默忽略*。
  25. try {
  26. $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item)
  27. } catch {}
  28. ```
  29. <sup>注意:`$null = ...`通常是比`... | Out-Null`更高效的替代方法 - 有关详情,请参见[此答案](https://stackoverflow.com/a/55665963/45375)。</sup>
  30. 注意:
  31. * 如果需要,异常对象可以通过`catch`块内的[自动`$_`变量](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#_)获得;您可以使用`throw`(无需参数)简单地*重新引发*异常。
  32. * 即使您只是_忽略_异常,它仍然会记录在PowerShell的会话范围错误日志中,即[自动`$Error`变量](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#error)。
  33. ---
  34. 至于**您尝试的内容**:
  35. > [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) -ErrorAction SilentlyContinue
  36. 这会导致语法错误,因为[常见的`-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编写的)。
  37. 请注意,表达式和命令分别根据*单独的语法规则*,即*解析模式*进行解析,这些规则在概念上在[about_Parsing](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Parsing)帮助主题中讨论。
  38. ```
  39. <details>
  40. <summary>英文:</summary>
  41. &lt;!-- language-all: sh --&gt;
  42. **`[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)**:
  43. ```
  44. # Using just {} as the catch block means that any exception is *quietly ignored*.
  45. try {
  46. $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item)
  47. } catch {}
  48. ```
  49. &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;
  50. Note:
  51. * 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).
  52. * 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).
  53. ---
  54. As for **what you tried**:
  55. &gt; [System.Runtime.Interopservices.Marshal]::ReleaseComObject($item) -ErrorAction SilentlyContinue
  56. 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*).
  57. 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.
  58. </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:

确定