Powershell – Add Server to NLB and Make its Default State to be Stopped

Add a member to the NLB Cluster and make its default state to be stopped

Background to this is: I was automating a build of a server. I have a csv file that I read that has details of all the servers, ipaddresses and environments.

CSV something like this

Computername,IPAddress,Prefix,DefaultGateway,Application,Environment,Description
Server1,10.108.65.101,18,10.108.127.254,Application Name,Prod,First Server
Server2,10.108.65.102,18,10.108.127.254,Application Name,Test,Second Server

This means I can use the same script for Production or Test environments  and it will join the server to the correct NLB Farm

In my case I wanted to join the server to the cluster , but not necessarily be available to be used by the cluster.
The annoying thing about the Add-NlbClusterNode is that you cannot set the default state, or its initial state (i.e stopped on restart of server and stopped as you join the NLB). So the below is for a web server NLB and I stop the Website (Default Website) first so that in the rare case a user might get to my server as I join it to the NLB, they will get a 404, decide to refresh the webpage and by that time the server will be stopped and one of the other NLB servers will take over.

After setting the new member to be Stopped, I then start the website. Why? because in my case we want QA people to directly access the server and check it before putting it back into the NLB

$ComputerName = $ENV:ComputerName
$ServerList = "C:\temp\Serverlist.csv"
$ListOfServers = Import-Csv $ServerList
#Find the Row that has this Computername in it and return the fields
$ComputernameDetails = $ListOfServers| Where-Object -property computername -eq $computername
#Get the Environment and assign it to $InstallEnvironment
$InstallEnvironment = $ComputernameDetails.Environment

$NLBClusterName = $Null
$NLBInterfaceName = "Ethernet" # I know that the NLB Interface name is for the cluster member

Function Add-ToNLB ()
{
    switch  ($InstallEnvironment ) #Depending on which Environment we are installing into the cluster name will be different
    {
    'Production' {
    Write-output "Prod"
    $NLBClusterName = "ProdCluster"
    Break}
    'Test' {
    Write-output "Test"
    $NLBClusterName = "TestCluster"
    Break}
    default {Write-Error $InstallEnvironment " No Install Environment defined"
    Exit}
    }
    Stop-WebSite -Name "Default Web Site" #stop website so no one can get to it, but that will result in when we join it to the NLB, someone will try to connect to it and get a 404
    if ($NLBClusterName)
    {
        try
        {
        Get-NlbClusterNode -HostName $ComputerName -ErrorAction Stop #Lets see if it is already in the cluster
        }
        catch [System.Net.WebException],[System.Exception] #If it is not then lets put it in there!
        {
            Write-Output "$ComputerName not in $NLBClusterName Farm:- adding it to it"
            Get-NlbCluster -HostName $NLBClusterName | Add-NlbClusterNode -NewNodeName $ComputerName -NewNodeInterface $NLBInterfaceName #Adding it
            Stop-NlbClusterNode -HostName $ComputerName #Lets abruptly stop it from being in NLB Farm otherwise people will connect to it and we may not want that
            Set-NlbClusterNode -InterfaceName $NLBInterfaceName -HostName $ComputerName -InitialHostState Stopped #Lets set its initial State to Stopped, in case we reboot the server
            Start-WebSite -Name "Default Web Site" #Lets restart the website now as it is not in the farm
        }
        finally #So if the try works do something. In this case, just show us the state of the cluster and the website
        {
                Get-NlbClusterNode #If it is in the NLB Farm, then show me
                Get-WebSite -Name "Default Web Site"
        }
    }
}
Add-ToNLB #Run the function to add the computer that the powershell is running on to the NLB

 

 

 

 

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.