powershell - PowerShell 中的 foreach -parallel
问题描述
我已经编写了一个脚本来使用foreach
语法获取离线集群资源,它工作正常。但是我需要为 50 个集群获取集群离线资源,我已经尝试过foreach -parallel
并且遇到了错误。
workflow Test-Workflow {
Param ([string[]] $clusters)
$clusters = Get-Content "C:\temp\Clusters.txt"
foreach -parallel ($cluster in $clusters) {
$clu1 = Get-Cluster -Name $cluster
$clustername = $clu1.Name
echo $clustername
$clu2 = Get-Cluster $clustername |
Get-ClusterResource |
where { $_.Name -and $_.state -eq "offline" }
echo $clu2
}
}
Test-Workflow
输出:
slchypervcl003 slchypervcl004
Get-ClusterResource :输入对象不能绑定到任何参数 该命令要么是因为该命令不接受管道输入,要么是 输入及其属性与任何采用管道的参数都不匹配 输入。 在测试工作流程:8 字符:8 + + CategoryInfo : InvalidArgument: (slchypervcl003:PSObject) [Get-ClusterResource], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.FailoverClusters.PowerShell.GetResourceCommand + PSComputerName:[本地主机] Get-ClusterResource :输入对象不能绑定到任何参数 该命令要么是因为该命令不接受管道输入,要么是 输入及其属性与任何采用管道的参数都不匹配 输入。 在测试工作流程:8 字符:8 + + CategoryInfo : InvalidArgument: (slchypervcl004:PSObject) [Get-ClusterResource], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.FailoverClusters.PowerShell.GetResourceCommand + PSComputerName:[本地主机]
解决方案
Get-ClusterResource
只能将集群节点对象或集群组对象作为管道输入,传递给-InputObject
参数。该Get-Cluster
命令返回对象类型Microsoft.FailoverClusters.PowerShell.Cluster ,而不是管道输入所需的Microsoft.FailoverClusters.PowerShell.ClusterResource或Microsoft.FailoverClusters.PowerShell.ClusterNodeGet-ClusterResource
对象。如果您将代码更改为不使用管道,只是将集群名称作为参数值,您的结果有何不同?
更改以下内容:
$clu2 = Get-Cluster $clustername | Get-ClusterResource | where { $_.Name -
and $_.state -eq "offline"}
至:
$clu2 = Get-ClusterResource -Name $clustername | where { $_.Name -
and $_.state -eq "offline"}
或者:
$clu2 = Get-ClusterResource -Cluster $clustername | where { $_.Name -
and $_.state -eq "offline"}
推荐阅读
- java - 大 O 不明白代码是如何 O(log n)
- sql - Snowflake DB 无法识别 CTE 递归查询中使用的“OPTION (MAXRECURSION 1000)”
- apache-spark - Spark 集群未使用所有执行程序
- java - java ..我的数组列表[]一次又一次地存储相同的值
- python - 如何简化 Pandas 中的条件
- android - 无法在 XML .Android 工作室中更改按钮的背景颜色
- c++ - 在 C++ 中的向量中使用 push_back vs at
- android - 无法实例化以下类: - androidx.recyclerview.widget.RecyclerView
- json - 选择与jsonb不同,左连接表
- reactjs - t.map 不是反应静态服务器上的函数错误