用于存储和移动集群的Powershell脚本

时间:2018-02-21 21:45:50

标签: powershell powershell-v2.0 hyper-v

我正在尝试创建一个PowerShell脚本来执行某些Hyper-V群组迁移。

我的脚本基本上将服务器主机放入群集组并将其名称存储到变量中。

此时我已经成功设法在群集只有两个物理服务器时执行此操作,但是当它有两个以上时我无法执行此操作。

请参阅下面的脚本:

### Get all cluster nodes in an array ###
Write-Host 'The possible servers to host the Quorum disk are:'
$AllNodes = Get-ClusterNode #| Select Name
Write-host $AllNodes

Write-Host ''

### Get current cluster owner ###
$ClusterOwner = Get-ClusterGroup -Name "Cluster Group" | Select-Object -expand OwnerNode
Write-Host 'The current owner of Cluster Group is:' $ClusterOwner

### Determine inactive node as a string ###
$InactiveNode = ($AllNodes -notmatch $ClusterOwner) | Select-Object -ExpandProperty Name

Write-Host ''

### Get User input to confirm or cancel the Quorum move ###
$confirmation = Read-Host "Confirm moving Quorum disk to" $InactiveNode". Please confirm with Y/N"

if ($confirmation -eq 'y'){
    Write-host ''   
    Write-host 'Moving Quorum disk...'
    Move-ClusterGroup "Cluster Group" -Node $InactiveNode
} else{
    Write-host ''   
    Write-host 'Quorum move cancelled!'

正如您在上面的代码中所看到的,我将所有节点存储到$ AllNodes变量中,并且我将最终的非活动节点进行比较以确定我应该提示哪个节点。

这个脚本适用于我只有2台服务器而不是更多的服务器,因为它会在变量中存储多个名称。

有没有办法将每个主机存储在另一个变量中?

谢谢

1 个答案:

答案 0 :(得分:0)

我将假设这不是所有相关代码。

没有理由存储在单独的变量中,不建议使用。

只要您将所有节点名称收集为数组,然后循环处理它们,您应该没问题。截至发布的内容,我没有看到任何代码执行迭代收集/存储在变量中的节点..

 $AllNodes = 'node01','node02','node03'

"There are $($AllNodes.Count) in the list to process"

ForEach ($NodeName in $AllNodes)
{
    "Processing node named $NodeName"
}

There are 3 in the list to process

Processing node named node01
Processing node named node02
Processing node named node03

此外,没有理由使用Write-Host来做你正在做的事情。写入屏幕是默认设置。只需使用带引号的字符串的变量名称。这是一回事。

根据您拥有的PoSH版本,使用Write-Host清除缓冲区,这意味着您无需在管道上使用。