【问题标题】:foreach -parallel in PowerShellPowerShell 中的 foreach -parallel
【发布时间】:2019-07-25 19:02:19
【问题描述】:

我已经编写了一个脚本来使用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:[本地主机]

【问题讨论】:

    标签: powershell powershell-workflow


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多