将当前节点设置为备用节点:
powershell
# 检查是否以管理员权限运行
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process powershell.exe -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
exit
}
# 获取所有 NLB 集群
$clusters = Get-NlbCluster
# 删除所有现有集群
foreach ($cluster in $clusters) {
Write-Host "正在删除集群:$($cluster.ClusterName)"
Remove-NlbCluster -ClusterName $cluster.ClusterName
}
# 安装 NLB 功能
Write-Host "正在安装 NLB 功能..."
Install-WindowsFeature NLB
# 配置新的 NLB 集群
$newClusterName = "MyNewNLBCluster"
$clusterIP = "192.168.1.100"
$nodes = @("192.168.1.1", "192.168.1.2")
Write-Host "创建新的 NLB 集群..."
New-NlbCluster -InterfaceName "Ethernet" -ClusterName $newClusterName -ClusterPrimaryIP $clusterIP
foreach ($node in $nodes) {
Write-Host "将节点 $node 添加到新集群..."
Add-NlbClusterNode -InterfaceName "Ethernet" -ClusterName $newClusterName -NewNodeName $node -NewNodeIP $node
}
# 获取当前节点的 IP 地址
$currentNodeIP = (Get-NetIPAddress -InterfaceAlias "Ethernet").IPAddress
# 将当前节点设置为备用节点
Write-Host "将当前节点设置为备用节点..."
(Get-NlbCluster -ClusterName $newClusterName).Nodes | Where-Object { $_.HostName -eq $currentNodeIP } | Set-NlbClusterNode -Priority 50
Write-Host "NLB 安装配置完成。"
在这个脚本中,通过设置较低的优先级(这里设置为 50)将当前节点配置为备用节点。同样,这里假设当前节点的网络接口别名为 “Ethernet”,你可能需要根据实际情况进行调整。