PowerShell功能未按计划运行

问题描述

我在创建我的第一个PowerShell函数时遇到了麻烦,这让我很烦。我正在尝试使用生产站点中的复制磁盘的DR服务器上的脱机和联机群集磁盘。这是函数

    function Update-Cluster {
    param (
        [string]$cluster,[string]$direction
    )
    $cluster
    $direction
<#
This section goes to the cluster and onlines the disk. They had been originally set up as resources so they show as off line.
#>
    if ($direction -like "*failover*") {
        Write-Host "Collecting disk information from host"
        $ondisk = Invoke-Command -ComputerName $cluster -ScriptBlock {Get-ClusterResource  | Where-Object {$_.name -like "*disk*" -and $_.OwnerGroup -like "Available Storage"}}
        Write-Host "Onlining disk"
        foreach ($cdisk in $ondisk) {
            $onCommand = @{ 
                ComputerName = $cluster
                ScriptBlock = { Start-ClusterResource $args[0] }
                ArgumentList = $cdisk.name
            }
        Invoke-Command @onCommand 
        }
    }
    else {
<#
Here we offline the disk that have been removed as a sql resource
#>
        Write-Host "Colecting disk information from host"
        $offdisk = Invoke-Command -ComputerName $cluster -ScriptBlock {Get-ClusterResource | Where-Object {$_.name -like "*disk*" -and $_.OwnerGroup -like "Available Storage"}}
        Write-Host "Offlining disk"
        foreach ($cdisk in $offdisk) {
            $offCommand = @{ 
                ComputerName = $cluster
                ScriptBlock = { Stop-ClusterResource $args[0] }
                ArgumentList = $cdisk.name
            }
            Invoke-Command @offCommand 
        }
    }
}

我遇到的第一个问题是它使if语句失败。我用“ Update-Cluster(na2-pdsqldb13,$ stage)来调用函数。在第一遍中,$ stage的值是“故障转移”。我验证参数是否正确地重复了函数参数($ cluster和$ direction)),但是显然故障转移不像故障转移。第二个问题是它在第一个Invoke-Command上失败,因为某些原因,$ cluster的值替换为$ direction的值:

[failover] Connecting to Remote Server failover Failed with the following error message : WinRM cannot process the request. The following error occurred while using Kerberos authentication: 
Cannot find the computer failover. Verify that the computer exists on the network and that the name provided is spelled correctly. For more information,see the about_Remote_Troubleshooting 
Help topic.
    + CategoryInfo          : OpenError: (failover:String) [],PSRemotingTransportException
    + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStatebroken

感谢您的帮助。

解决方法

好。我通过将函数调用从位置调用Update-Cluster("na2-pdsqldb13,$stage)更改为命名调用Update-Cluster -cluster "na2-pdsqldb13" -direction $stage来“修复”了它。不知道为什么这样做有效,而另一个却没有。