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

时间:2013-07-20 05:47:15

标签: loops powershell match output

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

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

另外,我试图说明如果任何服务配置为在其恢复选项中运行程序,它应该能够打印出服务名称。我只知道如何匹配“运行过程”。我如何匹配服务名称?

我的部分代码如下。

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

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

1 个答案:

答案 0 :(得分:2)

将对象传递给foreach时,该对象中的每个对象都可以通过$ _ variable

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