[英]powershell Collection was modified stop all running manual services
The following powershell statement fails with the message: Collection was modified;以下 powershell 语句失败并显示消息:集合已修改; enumeration operation may not execute.
枚举操作可能无法执行。
Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$_.Status -eq "Running" -and $_.StartType -eq "Manual"} | Stop-Service -Force
The solution is to not enumerate and stop but to first convert the items to an array/list but I can't find how to do that.解决方案是不枚举和停止,而是首先将项目转换为数组/列表,但我找不到如何做到这一点。
I would expect something like ToArray
or ToList
as is possible in c# and then iterate over the array instead of using the services enumerator but I cannot find powershell documentation on how to do this.我希望在 c# 中可能会出现类似
ToArray
或ToList
的内容,然后迭代数组而不是使用服务枚举器,但我找不到 powershell 文档关于如何执行此操作。
I only see samples that do something with a foreach
that are converted to a for
but I would like to keep it a oneline and just want to convert the enumerable to an list/array.\我只看到使用
foreach
执行某些操作的示例,这些示例转换为for
但我想将其保留为单行,只想将可枚举转换为列表/数组。\
Is that possible and how?这可能吗?怎么做?
Just assign to a variable before calling Stop-Service
:只需在调用
Stop-Service
之前分配给变量:
$servicesToStop = Get-Service |Where-Object {$_.Status -eq "Running" -and $_.StartType -eq "Manual"}
$servicesToStop |Stop-Service -Force
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.