在PowerShell中foreach -parallel

时间:2019-03-04 12:53:28

标签: powershell powershell-workflow

我已经编写了一个脚本来使用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 : The input object cannot be bound to any parameters for
the command either because the command does not take pipeline input or the
input and its properties do not match any of the parameters that take pipeline
input.
At Test-Workflow:8 char:8
+ 
    + CategoryInfo          : InvalidArgument: (slchypervcl003:PSObject) [Get-ClusterResource], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.FailoverClusters.PowerShell.GetResourceCommand
    + PSComputerName        : [localhost]

Get-ClusterResource : The input object cannot be bound to any parameters for
the command either because the command does not take pipeline input or the
input and its properties do not match any of the parameters that take pipeline
input.
At Test-Workflow:8 char:8
+ 
    + CategoryInfo          : InvalidArgument: (slchypervcl004:PSObject) [Get-ClusterResource], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.FailoverClusters.PowerShell.GetResourceCommand
    + PSComputerName        : [localhost]

1 个答案:

答案 0 :(得分:2)

Get-ClusterResource只能将群集节点对象或群集组对象作为管道输入,并传递给-InputObject参数。 Get-Cluster命令返回对象类型 Microsoft.FailoverClusters.PowerShell.Cluster ,而不是必需的 Microsoft.FailoverClusters.PowerShell.ClusterResource Microsoft.FailoverClusters .PowerShell.ClusterNode 对象,用于将管道输入到Get-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"}