gpt4 book ai didi

loops - 使用 Powershell 循环遍历单个输出中的每个项目

转载 作者:行者123 更新时间:2023-12-02 23:47:04 25 4
gpt4 key购买 nike

目前我正在尝试编写一个脚本来获取我计算机上所有服务的列表,然后尝试使用该输出中的每个服务名称来运行“sc.exe qfailure [service]”。

但是,我不确定为什么我的输出始终一遍又一遍地运行单个服务的名称。这是我的代码。

此外,我正在努力做到,如果任何服务被配置为在其恢复选项中运行程序,它应该能够打印出服务名称。不过,我只知道如何匹配“RUN PROCESS”。如何匹配服务名称?

下面是我的部分代码。

    $services = get-service | select -expand name;
Write-Host $services
$output = $services | ForEach {sc.exe qfailure $services in};

将不胜感激任何帮助。非常感谢!

最佳答案

当您将一个对象通过管道传递给 foreach 时,该对象中的每个对象都可以由 $_ 变量表示

$output = $services | ForEach {sc.exe qfailure $_ in};

关于loops - 使用 Powershell 循环遍历单个输出中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com