2013-03-07 58 views
0

我不确定是否有更高效的方法可以做到这一点,或者如果组合一段时间,for循环甚至可以工作,但我怎样才能解决这个问题,只有当for循环中的每个项目都返回时才能运行该函数真正?如何设置do-while(for())循环?

$In = @() 
While ($In[-1] -ne 'done') {$In += @(Read-Host 'Prompt')} 

do { 
    for($i=0;$i-le $In.length-2;$i++) {function($In[$i])} 
    } 
while (for($j=0;$j-le $In.length-2;$j++) 
    {Get-Service ($In[$j]) | $_.status} -eq "Running") 

以$在各种服务名称的数组,该做的-while应该在服务中的任何一个返回“已停止”状态重新启动阵列中的每个服务。

+1

这非常复杂。这应该是做什么? – 2013-03-07 07:11:49

+0

对不起,我试图摆脱不必要的信息。 – Forager 2013-03-07 07:28:18

+0

考虑到你的意图,我会做'a','b','c'| ? {(Get-Service -name $ _)。Status -eq'Stopped'} |重启Service' – 2013-03-07 07:37:55

回答

0

I'ld做这样的:

$In = @() 
do 
{ 
$c = (Read-Host 'Enter service name') 
if ($c) { $in+=$c} 
} 
while ($c) #just a void input to end  

$in | where-object {(Get-Service -name $_).Status -match 'Stopped'} | Restart-Service 
0

也许OT,但如果你已经有了V3在该系统上我会做这种方式:

Get-Service | sort Status | Out-GridView -Title 'Select services to restart' -OutputMode Multiple | Restart-Service 
0
while(($ans = Read-Host 'Give me something') -ne 'done'){ 
    Restart-Service $ans 
}