如何分别处理多个foreach语句?
how to process multiple foreach statements separately?
我是 PowerShell 的新手,我什至不知道如何正确地Google。
这就是我正在尝试做的事情:运行 在多台计算机上执行一些命令。
我可以让它们到达 运行 计算机 A 的命令 1,然后计算机 A 的命令 2,然后计算机 A 的命令 3...然后计算机 B 的命令 1 计算机 B 的命令 2...等等
但我想 运行 在所有计算机上命令 1
然后在所有计算机上使用 command2
然后 command3 ...等等
现在是这样的:
1A
2A
3A
1B
2B
3B
1C
2C
3C...
是否可以在 if $state -eq 'Start' 语句中执行此操作?
1A
1B
1C
2A
2B
2C
3A
3B
3C...不创建另一个函数?
我不想反转所有内容,只是我需要遵循该模式的“开始”语句。
这就是我现在的基本情况:
$Computers = "ComputerA","ComputerB","ComputerC"
function Set-CService {
param(
[Parameter(Mandatory)]
[ValidateSet('Start','Stop','Restart','Install')]
[string]$State,
[Parameter(Mandatory, ValueFromPipeline)]
[array]$Computers
)
process {
$ErrorActionPreference = "SilentlyContinue"
If ($state -eq 'Start') {
foreach ($Computer in $Computers) {Write-Host "statement1 on $Computer"}
foreach ($Computer in $Computers) {Write-Host "statement2 on $Computer"}
foreach ($Computer in $Computers) {Write-Host "statement3 on $Computer"}
}
foreach ($Computer in $Computers){
If ($State -eq 'Stop') {Write-Host "It is stopping on $Computer"}
If ($state -eq 'Restart') {
Write-Host "Restart statement1 on $Computer"
write-host "Restart statement2 on $Computer"
Write-Host "Restart statement3 on $Computer"}
If ($State -eq 'Install') {
Write-Host "Install statement1 on $Computer"
write-host "Install statement2 on $Computer"
Write-Host "Install statement3 on $Computer"}
}
}
}
$Computers | Set-CService -State Start
使用 Invoke-Command -AsJob
调用远程工作负载作为后台作业:
If ($state -eq 'Start') {
# Kick off remoting jobs on each computer in $Computers
$jobs = Invoke-Command -ComputerName $Computers -Scriptblock {
Write-Host "statement1 on $env:ComputerName"
Write-Host "statement2 on $env:ComputerName"
Write-Host "statement3 on $env:ComputerName"
} -AsJob
# Wait for jobs to succeed, then receive the output
$jobs |Receive-Job -Wait
}
我想这就是你的意思。每个语句在所有 3 台计算机上并行运行,但在继续下一个语句之前等待。
$computers = echo a001 a002 a003
$statements = {Write-Host "statement1 on $env:Computername"},
{Write-Host "statement2 on $env:computername"},
{Write-Host "statement3 on $env:Computername"}
foreach ($statement in $statements) {
invoke-command $computers $statement
}
statement1 on A001
statement1 on A002
statement1 on A003
statement2 on A001
statement2 on A002
statement2 on A003
statement3 on A003
statement3 on A002
statement3 on A001
我是 PowerShell 的新手,我什至不知道如何正确地Google。
这就是我正在尝试做的事情:运行 在多台计算机上执行一些命令。
我可以让它们到达 运行 计算机 A 的命令 1,然后计算机 A 的命令 2,然后计算机 A 的命令 3...然后计算机 B 的命令 1 计算机 B 的命令 2...等等
但我想 运行 在所有计算机上命令 1 然后在所有计算机上使用 command2 然后 command3 ...等等
现在是这样的: 1A 2A 3A 1B 2B 3B 1C 2C 3C...
是否可以在 if $state -eq 'Start' 语句中执行此操作?
1A 1B 1C 2A 2B 2C 3A 3B 3C...不创建另一个函数?
我不想反转所有内容,只是我需要遵循该模式的“开始”语句。
这就是我现在的基本情况:
$Computers = "ComputerA","ComputerB","ComputerC"
function Set-CService {
param(
[Parameter(Mandatory)]
[ValidateSet('Start','Stop','Restart','Install')]
[string]$State,
[Parameter(Mandatory, ValueFromPipeline)]
[array]$Computers
)
process {
$ErrorActionPreference = "SilentlyContinue"
If ($state -eq 'Start') {
foreach ($Computer in $Computers) {Write-Host "statement1 on $Computer"}
foreach ($Computer in $Computers) {Write-Host "statement2 on $Computer"}
foreach ($Computer in $Computers) {Write-Host "statement3 on $Computer"}
}
foreach ($Computer in $Computers){
If ($State -eq 'Stop') {Write-Host "It is stopping on $Computer"}
If ($state -eq 'Restart') {
Write-Host "Restart statement1 on $Computer"
write-host "Restart statement2 on $Computer"
Write-Host "Restart statement3 on $Computer"}
If ($State -eq 'Install') {
Write-Host "Install statement1 on $Computer"
write-host "Install statement2 on $Computer"
Write-Host "Install statement3 on $Computer"}
}
}
}
$Computers | Set-CService -State Start
使用 Invoke-Command -AsJob
调用远程工作负载作为后台作业:
If ($state -eq 'Start') {
# Kick off remoting jobs on each computer in $Computers
$jobs = Invoke-Command -ComputerName $Computers -Scriptblock {
Write-Host "statement1 on $env:ComputerName"
Write-Host "statement2 on $env:ComputerName"
Write-Host "statement3 on $env:ComputerName"
} -AsJob
# Wait for jobs to succeed, then receive the output
$jobs |Receive-Job -Wait
}
我想这就是你的意思。每个语句在所有 3 台计算机上并行运行,但在继续下一个语句之前等待。
$computers = echo a001 a002 a003
$statements = {Write-Host "statement1 on $env:Computername"},
{Write-Host "statement2 on $env:computername"},
{Write-Host "statement3 on $env:Computername"}
foreach ($statement in $statements) {
invoke-command $computers $statement
}
statement1 on A001
statement1 on A002
statement1 on A003
statement2 on A001
statement2 on A002
statement2 on A003
statement3 on A003
statement3 on A002
statement3 on A001