简体   繁体   English

powershell 集合被修改停止所有正在运行的手动服务

[英]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# 中可能会出现类似ToArrayToList的内容,然后迭代数组而不是使用服务枚举器,但我找不到 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.

相关问题 如何使用Powershell列出服务器中使用服务帐户运行的所有服务 - How to list all the services running with a service account in a server using Powershell 在 PowerShell 列表中创建一个 txt 文件,列出所有正在运行的服务 - create a txt file in PowerShell list of all running services 列出 PowerShell 中的所有服务 - List all services in PowerShell Powershell停止服务,清理磁盘,然后启动服务 - Powershell Stop Services, defrg disks and then start services 使用内联管理员凭据运行 Powershell 脚本来启动和停止 Windows 服务 - Running Powershell script using inline admin credentials to start and stop windows services 如果 Powershell 未以管理员身份运行,则停止脚本 - Powershell stop script if it not running as admin 使用Powershell识别程序修改的所有文件 - Using Powershell to identify all files modified by a program Powershell脚本-运行eq OK的服务或列出未运行的服务 - Powershell Script - Services Running eq OK or list Services Not Running 如何解决“收藏集已修改; 枚举操作可能无法在Powershell中执行” - How to fix “Collection was modified; enumeration operation may not execute in powershell” Powershell:如何启动/停止服务以及启动时尚未运行的所有必需服务? - Powershell: How do I Start/Stop a Service and all RequiredServices that are not already running when started?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM