有关多个集合的Powershell foreach
所以我有一个powershell脚本,我想请求社区的帮助。我不得不说,我并不总是最善于表达我想做的事情,部分原因是我没有编程经验,所以请容忍我,如果我用不正确的词来解释我的意思,请提问/纠正我 话虽如此,我想做的是: 增加服务和启动类型时:有关多个集合的Powershell foreach,powershell,foreach,powershell-5.0,Powershell,Foreach,Powershell 5.0,所以我有一个powershell脚本,我想请求社区的帮助。我不得不说,我并不总是最善于表达我想做的事情,部分原因是我没有编程经验,所以请容忍我,如果我用不正确的词来解释我的意思,请提问/纠正我 话虽如此,我想做的是: 增加服务和启动类型时: 停止服务器X上的服务A($services)($rebootingServer) 在服务器X上禁用服务A($services)($rebootingServer) 给定:我们知道在脚本运行之前,服务器Y上禁用了服务A 基于文本文件列表$startupty
- 停止服务器X上的服务A($services)($rebootingServer)
- 在服务器X上禁用服务A($services)($rebootingServer) 给定:我们知道在脚本运行之前,服务器Y上禁用了服务A
- 基于文本文件列表$startuptypes在服务器Y上启用服务
- 在服务器Y上启动服务A
- 冲洗并重复,直到$services和$startuptypes位于每个列表的末尾
位
应用管理
而$startuptypes有:
自动
手册
我希望分别应用它们(bits>自动应用管理>手动)
到目前为止,我所掌握的情况如下:
$services = Get-Content "C:\TEMP\services.txt"
$Startuptypes = Get-Content "C:\TEMP\StartupTypes.txt"
$RebootingServer = Read-Host 'Name of the server that you are bringing down'
$FailoverServer = Read-Host 'Name of the server it is failing over to'
#foreach ($service in $services && $Startuptype in $Startuptypes) {
Invoke-Command -ComputerName $RebootingServer -ArgumentList $service - ScriptBlock {param($service) Stop-Service $service}
Start-Sleep -s 3
Invoke-Command -ComputerName $RebootingServer -ArgumentList $service - ScriptBlock {param($service) set-service $service -StartupType Disabled}
Start-Sleep -s 10
Invoke-Command -ComputerName $FailoverServer -ArgumentList $service $StartupType -ScriptBlock {param($service,$startuptype) Set-Service $service -StartupType $startuptype}
Start-Sleep -s 3
Invoke-Command -ComputerName $FailoverServer -ArgumentList $service - ScriptBlock {param($service) Start-Service $service}
Start-sleep -s 10
}
“for each”语句是我希望它执行的操作的伪代码,但不确定它是否存在或如何相应地编写。我甚至不知道该怎么称呼。多重条件句?撇开这一点不谈,我该如何恰当地写出我正试图实现的目标呢?感谢您在advanced中提供的任何帮助 听起来您想以对应的对枚举2个集合的元素:在迭代1中,处理集合A中的元素1以及集合B中的元素1 PowerShell 6-解决方案:
# Sample collections.
# Note that their counts must match.
$services = 'serviceA', 'serviceB', 'serviceC'
$startupTypes = 'automatic', 'manual', 'disabled '
$i = 0 # helper index var.
foreach ($service in $services) { # enumerate $services directly
# Using the index variable, find the corresponding element from
# the 2nd collection, $startupTypes, then increment the index.
$startupType = $startupTypes[$i++]
# Now process $service and $startupType as needed.
}
PowerShell 7+解决方案:
# Sample collections.
# Note that their counts must match.
$services = 'serviceA', 'serviceB', 'serviceC'
$startupTypes = 'automatic', 'manual', 'disabled '
$i = 0 # helper index var.
foreach ($service in $services) { # enumerate $services directly
# Using the index variable, find the corresponding element from
# the 2nd collection, $startupTypes, then increment the index.
$startupType = $startupTypes[$i++]
# Now process $service and $startupType as needed.
}
PowerShell 7构建在.NET Core 3.1上,其中具有返回(2元素)元组的重载,这简化了解决方案:
- 注意事项:对于大小不同的输入集合,一旦较小的集合的项用完,枚举就会停止
[System.Linq.Enumerable]
),并且也缺乏对调用泛型方法的全面支持,尽管GitHub上有功能请求-请参阅和
上述收益率:
服务:服务a;启动类型:自动
服务:服务B;启动类型:手动
服务:服务C;启动类型:禁用