无法删除变量,因为它已经过优化并且无法删除-释放COM对象 [英] Cannot remove variable because it has been optimized and is not removable - releasing a COM object

查看:56
本文介绍了无法删除变量,因为它已经过优化并且无法删除-释放COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在脚本结尾处,我使用’ie’| ForEach-Object {删除变量$ _ -Force} 。在PS 2(Windows 7)中可以正常工作,但PS 5(Windows 10)会引发错误:

At the end of my script I use 'ie' | ForEach-Object {Remove-Variable $_ -Force}. It works fine in PS 2 (Windows 7) but PS 5 (Windows 10) throws an error:


无法删除变量,即因为变量已优化,
不可移动。尝试使用Remove-Variable cmdlet(不带任何
别名),或者点源删除要用来删除
变量的命令。

Cannot remove variable ie because the variable has been optimized and is not removable. Try using the Remove-Variable cmdlet (without any aliases), or dot-sourcing the command that you are using to remove the variable.

我如何使其在PS 5上玩起来不错?还是应该只使用 Remove-Variable'ie'-Force

How can I make it play nice with PS 5; or should I just use Remove-Variable 'ie' -Force?

推荐答案

删除COM对象的推荐方法是调用ReleaseComObject方法,将对象引用($ ie)传递给COM对象的实例。

The recommended way to remove COM objects is to call the ReleaseComObject method, passing the object reference ($ie) to the instance of your COM object.

更多 Windows PowerShell本周技巧的详细说明和示例代码显示了如何摆脱COM对象:

Here is more detailed explanation and sample code from a Windows PowerShell Tip of the Week that shows how to get rid of COM objects:


无论何时从公共语言运行库中调用COM对象(
都会发生这是从
Windows PowerShell调用COM对象时要做的事情,该COM对象被包装在运行时可调用
包装器中,并且引用计数增加;引用计数
有助于CLR(公共语言运行时)跟踪正在运行的COM
对象以及正在运行的COM对象数。当
从Windows PowerShell中启动Excel时,Excel会在运行时可调用包装器中打包成
,并且引用计数将
递增为1。

Whenever you call a COM object from the common language runtime (which happens to be the very thing you do when you call a COM object from Windows PowerShell), that COM object is wrapped in a "runtime callable wrapper," and a reference count is incremented; that reference count helps the CLR (common language runtime) keep track of which COM objects are running, as well as how many COM objects are running. When you start Excel from within Windows PowerShell, Excel gets packaged up in a runtime callable wrapper, and the reference count is incremented to 1.

那很好,除了一件事:当您调用Quit方法并且
终止Excel时,CLR的引用计数不会减少
(也就是说,它不会被重置)到0)。并且由于引用
的计数不为0,CLR保留了对COM对象的保留:除了
以外,这意味着我们的对象引用($ x)仍然是有效的
,并且Excel.exe进程继续运行。那绝对不是
的好事;毕竟,如果我们希望Excel继续运行,那么
可能根本不会调用Quit方法。 ...

That’s fine, except for one thing: when you call the Quit method and terminate Excel, the CLR’s reference count does not get decremented (that is, it doesn’t get reset back to 0). And because the reference count is not 0, the CLR maintains its hold on the COM object: among other things, that means that our object reference ($x) is still valid and that the Excel.exe process continues to run. And that’s definitely not a good thing; after all, if we wanted Excel to keep running we probably wouldn’t have called the Quit method in the first place. ...

... [使用]我们的
Excel实例调用ReleaseComObject方法...减少$ b $中对象的引用计数b问题。在这种情况下,这意味着它将把我们的Excel实例的引用
计数从1更改为0。这是一件好事:
一旦引用计数达到0,CLR就会释放其保留状态
对象,过程终止。 (这一次它确实确实终止了
。)

... calling the ReleaseComObject method [with] our instance of Excel ... decrements the reference count for the object in question. In this case, that means it’s going to change the reference count for our instance of Excel from 1 to 0. And that is a good thing: once the reference count reaches 0 the CLR releases its hold on the object and the process terminates. (And this time it really does terminate.)

$x = New-Object -com Excel.Application
$x.Visible = $True
Start-Sleep 5
$x.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($x)
Remove-Variable x


消息无法删除变量,即因为该变量已经过优化并且无法删除。您将得到,很可能意味着您试图访问(检查,监视或以其他方式访问)已由优化程序删除的变量。

The message "Cannot remove variable ie because the variable has been optimized and is not removable." you get, most likely means you have tried to access (inspect, watch, or otherwise access) a variable which has been already removed by the optimizer.

这篇关于无法删除变量,因为它已经过优化并且无法删除-释放COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆