任何人都知道使用 Powershell 从 COM+ 应用程序中一次删除所有组件的语法
Anyone have syntax for removing all components at once from COM+ Application using Powershell
我在尝试遍历并从我的 COM+ 应用程序中删除 91 个组件时遇到问题
这是我的 Powershell 代码:
$app = $apps | Where-Object {$_.Name -eq 'pkgAdap2'}
$compColl = $apps.GetCollection("Components", $app.Key)
$compColl.Populate()
$index = 0
foreach($component in $compColl) {
$compColl.Remove($index)
$compColl.SaveChanges()
$index++
}
该代码似乎可以工作,但它只删除了一半的组件,对于 $index
循环的其余部分 returns 此错误:
Value does not fall within the expected range.
At line:4 char:5
+ $compColl.Remove($index)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
所以我保留运行它,剩下的组件数量不断减少一半。
我认为原因是 array/collection 我 "removing" 来自重新排序剩余的索引,每次都移动它们。所以在 $index
超出范围之前我只完成了一半。我唯一能想到的就是这样做。因此我也尝试了另一种方法:
while($compColl.Count > 0) {
$compColl.Remove($compColl.Count)
}
但是也没用
有谁知道如何一次删除所有组件?
听起来您的 collection 的索引是基于 0
的,因此以下内容应该有效:
while($compColl.Count -gt 0) {
$compColl.Remove($compColl.Count - 1) # remove last element, which updates .Count. Using 0 to remove the first one is a good option to.
}
$compColl.SaveChanges()
如果您确定 collection 在枚举时不会发生变化,则此变体可能稍微更有效:
for ($i = $compColl.Count - 1; $i -ge 0; --$i) {
$compColl.Remove($i)
}
$compColl.SaveChanges()
您原来的方法的问题是每个 $compColl.Remove($index)
调用都会隐式递减剩余项目的索引,因此 $index++
最终 跳过 个项目直到达到超出剩余最高索引的值并失败。
通常,在循环体中修改 collection 的同时逐项遍历 collection 是有问题的。
我在尝试遍历并从我的 COM+ 应用程序中删除 91 个组件时遇到问题
这是我的 Powershell 代码:
$app = $apps | Where-Object {$_.Name -eq 'pkgAdap2'}
$compColl = $apps.GetCollection("Components", $app.Key)
$compColl.Populate()
$index = 0
foreach($component in $compColl) {
$compColl.Remove($index)
$compColl.SaveChanges()
$index++
}
该代码似乎可以工作,但它只删除了一半的组件,对于 $index
循环的其余部分 returns 此错误:
Value does not fall within the expected range.
At line:4 char:5
+ $compColl.Remove($index)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
所以我保留运行它,剩下的组件数量不断减少一半。
我认为原因是 array/collection 我 "removing" 来自重新排序剩余的索引,每次都移动它们。所以在 $index
超出范围之前我只完成了一半。我唯一能想到的就是这样做。因此我也尝试了另一种方法:
while($compColl.Count > 0) {
$compColl.Remove($compColl.Count)
}
但是也没用
有谁知道如何一次删除所有组件?
听起来您的 collection 的索引是基于 0
的,因此以下内容应该有效:
while($compColl.Count -gt 0) {
$compColl.Remove($compColl.Count - 1) # remove last element, which updates .Count. Using 0 to remove the first one is a good option to.
}
$compColl.SaveChanges()
如果您确定 collection 在枚举时不会发生变化,则此变体可能稍微更有效:
for ($i = $compColl.Count - 1; $i -ge 0; --$i) {
$compColl.Remove($i)
}
$compColl.SaveChanges()
您原来的方法的问题是每个 $compColl.Remove($index)
调用都会隐式递减剩余项目的索引,因此 $index++
最终 跳过 个项目直到达到超出剩余最高索引的值并失败。
通常,在循环体中修改 collection 的同时逐项遍历 collection 是有问题的。