PowerShell: Set Hyper-V Cluster Quorum

# setClusterQuorum.ps1
# version 0.01

$clustername=$null
$clusterQuorum="\\FILESHERVER007\CLUSTER007"
$resetPrior=$false
 
function setClusterQuorum($clusterName,$clusterQuorum,$resetPrior=$false){
    
    # Get clustername, if not defined
    $clustername=if(!$clustername){(get-cluster).name}else{$clustername}

    # Cluster Name Object (CNO) must have write permissions on the share
    function addNtfsPermissions ($path,$accountsToAdd,$permissions){
        $acl = Get-ACL $path
        $accessRule=New-Object System.Security.AccessControl.FileSystemAccessRule($accountsToAdd,$permissions,"Allow")
        $acl.AddAccessRule($accessRule)
        Set-Acl $path $acl
        Get-ACL $path
    }
    
    addNtfsPermissions $clusterQuorum $env:USERDOMAIN+"\$clustername`$" 'Full'

    # First change quorum mode to defaults
    if($resetPrior){Set-ClusterQuorum -Cluster $clustername -NodeMajority}

    $quorumType=if($clusterQuorum -match '^\\\\'){'NodeAndFileShareMajority'}else{'NodeAndDiskMajority'}
    if(test-path $clusterQuorum){
        if($quorumType -eq 'NodeAndFileShareMajority'){
            Set-ClusterQuorum -NodeAndFileShareMajority $clusterQuorum
        }elseif($quorumType -eq 'NodeAndDiskMajority'){
            Set-ClusterQuorum -NodeAndDiskMajority $clusterQuorum
        }else{
            write-warning 'Path is invalid.'
            }
    }else{
        write-warning "$clusterQuorum is unreachable from $env:computername using account $env:username"
        }

    # Check quorum status
    # Option 1: Get-ClusterResource -Name $currentQuorum | Select *
    # Option 2:
    $result=((Get-ClusterQuorum).QuorumResource|Select-Object Characteristics,Cluster,Name,OwnerNode,State|out-string).trim()
    write-host $result
}

setClusterQuorum $clusterName $clusterQuorum $resetPrior

Leave a Reply

Your email address will not be published. Required fields are marked *