Skip to content

Instantly share code, notes, and snippets.

@eguyd
Created June 16, 2019 01:18
Show Gist options
  • Save eguyd/60a7ddef26035a19af798f1ad2f943f1 to your computer and use it in GitHub Desktop.
Save eguyd/60a7ddef26035a19af798f1ad2f943f1 to your computer and use it in GitHub Desktop.
Implement Windows Server 2016 Load Balancing.
#
#AUTHOR: Guy Derenoncourt
#PROGRAM: Script to create a NLB-cluster.
#
#This Sample Code is provided for the purpose of illustration "AS IS" WITHOUT WARRANTY OF ANY KIND.
#
#
###########################################################################
#Set IP for NLB | YOU can change the IP to your desired CLASS
Write-Host "Set NLB IP and change Network adapter" -ForegroundColor yellow
Netsh interface ip set address name="Local Area Connection 3" static 10.0.0.2 255.255.255.0
Netsh interface set interface name="local area connection 3" newname="NLB"
#Set ExecutionPolicy
Write-Host "Set ExecutionPolicy" -ForegroundColor yellow
Set-ExecutionPolicy -scope LocalMachine RemoteSigned –force
#Importing Microsoft`s PowerShell-modules
Write-Host "Importing Microsoft`s PowerShell-modules" -ForegroundColor yellow
ImportSystemModules
#Add-WindowsFeature
Write-Host "Add-WindowsFeature NLB" -ForegroundColor yellow
Add-WindowsFeature NLB
#Importing Microsoft`s PowerShell-module for administering NLB Clusters
Write-Host "Importing Microsoft`s PowerShell-module for administering NLB Clusters" -ForegroundColor yellow
Import-Module NetworkLoadBalancingClusters
#function EndPSS { get-pssession | remove-pssession }
Write-Host "function EndPSS { get-pssession | remove-pssession }" -ForegroundColor yellow
function EndPSS { get-pssession | remove-pssession }
endpss
function EndPSS { get-pssession | remove-pssession }
#Variables for creating the new cluster
Write-Host "Variables for creating the new cluster" -ForegroundColor yellow
$ClusterFqdn = Read-Host "Enter NLB cluster Name FQDN"
$InterfaceName = Read-Host "Enter interface name for NLB-adapter"
$ClusterPrimaryIP = Read-Host "Enter cluster primary IP"
$ClusterPrimaryIPSubnetMask = Read-Host "Enter subnetmask for cluster primary IP"
Write-Host "Choose cluster operation mode"
Write-Host "1 - Unicast"
Write-Host "2 - Multicast"
Write-Host "3 - IGMP Multicast"
switch (Read-Host "Enter the number for your chosen operation mode")
{
1 {$OperationMode = "unicast"}
2 {$OperationMode = "multicastcast"}
3 {$OperationMode = "igmpmulticast"}
default {Write-Warning "Invalid option, choose '1', '2' or '3'";return}
}
#Creating new cluster
Write-Host "Creating NLB Cluster..." -ForegroundColor yellow
New-NlbCluster -ClusterName $ClusterFqdn -InterfaceName $InterfaceName -ClusterPrimaryIP $ClusterPrimaryIP -SubnetMask $ClusterPrimaryIPSubnetMask -OperationMode $OperationMode
#Removing default port rule for the new cluster
Write-Host "Removing default port rule..." -ForegroundColor yellow
Get-NlbClusterPortRule -HostName . | Remove-NlbClusterPortRule -Force
#Adding port rules
Add-NlbClusterPortRule -Protocol Tcp -Mode Multiple -Affinity Single -StartPort 80 -EndPort 80 -InterfaceName $InterfaceName | Out-Null
Write-Host "Added port rule for http (tcp 80)" -ForegroundColor yellow
Add-NlbClusterPortRule -Protocol Tcp -Mode Multiple -Affinity Single -StartPort 443 -EndPort 443 -InterfaceName $InterfaceName | Out-Null
Write-Host "Added port rule for https (tcp 443)" -ForegroundColor yellow
#Remove Old IP
Remove-NlbClusterNodeDIP 10.0.0.2 -force
Write-Host "Removed NLB Local IP" -ForegroundColor yellow
#Adding additional cluster nodes based on user input
Write-Host "Give Second NLB host" -ForegroundColor yellow
$Node2Fqdn = Read-Host "Enter 2e NLB node"
function EndPSS { get-pssession | remove-pssession }
#Set Network Adapter
#Enter-PSSession -ComputerName $Node2Fqdn
invoke-command -computername $Node2Fqdn -scriptblock { Netsh interface ip set address name="local area connection 3" static 10.0.0.3 255.255.255.0}
invoke-command -computername $Node2Fqdn -scriptblock { Netsh interface set interface name="local area connection 3" newname="NLB"}
Write-Host "Placed NLB IP and changed NIC to NLB" -ForegroundColor yellow
exit-PSSession
endpss
#Import-Module NetworkLoadBalancingClusters
Write-Host "Import-Module NetworkLoadBalancingClusters On Remote Node" -ForegroundColor yellow
Enter-PSSession -ComputerName $Node2Fqdn
invoke-command -computername $Node2Fqdn { Import-Module NetworkLoadBalancingClusters}
exit-pssession
endpss
#Add Remote Node To NLB
Write-Host "Adding cluster node $Node2Fqdn" -ForegroundColor yellow
Get-NlbCluster | Add-NlbClusterNode -NewNodeName $Node2Fqdn -NewNodeInterface NLB
#Remove Old IP
Remove-NlbClusterNodeDip 10.0.0.3 -Hostname $Node2Fqdn -force
Write-Host " Remove old IP node " -ForegroundColor yellow
Write-Warning "Before adding additional nodes, make sure that NLB are installed and the NLB-adapter are configured with a static IP-address on the remote node"
$additionalnodes = Read-Host "Add additional nodes to the cluster? Y/N"
if ($additionalnodes -like "y"){
do {
$NodeFqdn = Read-Host "Enter FQDN for the additional node"
$NewNodeInterface = Read-Host "Enter interface name for NLB-adapter on the additional node"
Write-Host "Adding cluster node $NodeFqdn" -ForegroundColor yellow
Get-NlbCluster -HostName . | Add-NlbClusterNode -NewNodeName $NodeFqdn -NewNodeInterface $NewNodeInterface
$additionalnodes = Read-Host "Add additional nodes to the cluster? Y/N"
} until ($additionalnodes -like "n")
}
#END OF SCRIPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment