微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PowerShell功能未按计划运行

如何解决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来“修复”了它。不知道为什么这样做有效,而另一个却没有。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。