简体   繁体   English

ForEach对象操作Powershell

[英]ForEach-Object actions Powershell

I am trying to specify a structured order of commands and I am looking for an easier way than I currently have. 我正在尝试指定命令的结构化顺序,并且我正在寻找一种比当前更简单的方法。 I have an ordered reboot of a series of servers. 我命令重启一系列服务器。 The servers are in tiers, so the tiers must be shut down in a particular order, then brought back up in the reverse order. 服务器位于层中,因此必须按特定顺序关闭层,然后以相反的顺序恢复。 Also time is a concern as there are a lot of servers and I don't want a ton of down time. 时间也是一个问题,因为有很多服务器,而且我不想浪费大量时间。

What I have is this... 我有这个...

$Servers | Where-Object {$_.Tier -match "App"} | ForEach-Object {
    Shutdown-VMGuest -VM $_.Name -Confirm:$false | Out-Null
    }
$Servers | Where-Object {$_.Tier -match "App"} | ForEach-Object {
    while(((Get-VM $_.Name).PowerState -ne "PoweredOff") -and ($sw.Elapsed.TotalSeconds -lt $timeout)) {
    Start-Sleep -Seconds 5
    }
if ((Get-VM $_.Name).PowerState -ne "PoweredOff") {
    Stop-VM -VM $_.Name -Confirm:$false | Out-Null
    }
}

I then just repeat this code over and over for all of the tiers. 然后,我要对所有层一遍又一遍地重复此代码。 Is there an easier way to shut the servers down FIRS, then go into my verify loop (using a pipe maybe). 有没有更简单的方法来关闭服务器FIRS,然后进入我的验证循环(可能使用管道)。 Also, is there a way I can make a "command loop" so I don't have to have such a long an unmanageable script? 另外,有没有一种方法可以使“命令循环”,所以我不必花那么长时间来管理脚本?

可能您应该使用工作流程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM