Automation script help RRS feed

  • Question

  • I am having an issue with If statement in my script.   This part of the script will figure out if the server that I am running on is an odd or even node.   If its an even node let's say 02 it will set the variable to 01 to create the windows cluster, if the node is 01 it will make the node variable to 02.  

    The Output should be KLLOCWAPPSQL02P however this is the output I am getting below.   

    KLLOCWAPPSQL"{0:D2}" -f [int]$ServerNumber++P

    This is the script I have a bit commented out to test out this script.   

    ### Sets the Exectuion policy so the script can run remotely.###
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

    ##Gets the Server Name, The Application that is on the server, Server Number and the Enviroment (Prod, Perf and so on) of the Server##

    $ServerName = $env:computername
    #Write output $ServerName

    $FullServerName = (Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
    #Write output = $FullServerName

    $ServerName1 = $env:computername.Substring(0,12)
    #Write output $ServerName1

    $App = $env:computername.Substring(6,3)
    #Write output $App

    $ServerNumber =$env:computername.Substring(12,2)
    #Write output $ServerNumber

    $Environment = $env:computername.Substring(14)
    #Write output $Environment

    $IPAddress = Read-Host -Prompt 'What Ip Address do you want to add to the firewall rule?'
    #$Port = Read-Host -Prompt 'What port need to be open for the Application Traffic?'
    #$ClusterName_Cluster = Read-Host -Prompt 'What is the Cluster Name?'
    $IPAddress_Cluster = Read-Host -Prompt 'What is the IP address for the to use for the Cluster?'
    $Node_Cluster = Read-Host -Prompt 'What Node do we want to add to the cluster?'

    ###This removed the SMB1 Protocal which is a security risk, we do not use SMB1 anymore.   This part also installs Failover Clustering which is needed for AlwaysOn### 
    Disable-WindowsOptionalFeature -Online -Featurename  SMB1Protocol
    Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools
    Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools -ComputerName $Node_Cluster

    ##This If Statement will be used to set the cluster name based on which environment it is in.## 

    if($Environment -match 'P'){
       $level = ("Prod")

    } elseif($Environment -match 'C'){
       $level = ("Perf")

    } elseif($Environment -match 'S'){
       $level = ("Stage")
       elseif($Environment -match 'Q'){
       $level = ("QA")
       elseif($Environment -match 'T'){
       $level = ("Test")
      else { ($Environment -match 'D')
       $level = ("Dev")
    #Write Output $level

    ##This If Statement will be used to set port in the firewall rule based on if the servers are Production or Non-Production.## 

    if($Environment -match 'P'){
       $Port = ("1414")

      else { 
       $Port = ("1416")

    #Write Output $Port

    ##This variable combines the App and Environment Level variables to create the Cluster name##

    $ClusterName_Cluster = $App + $Level + '_' + 'Cluster'
    Write Output $ClusterName_Cluster

    ##If the server is an Odd server it will make the 2nd node the 02, 04, 06 and so on server, and if the server is a even server it will make the 2nd node the 01, 03, 05 and so on server##

    if($ServerNumber % 2 -eq '0'){
       $Node = ($ServerName1 + '"{0:D2}" -f [int]$ServerNumber--'  + $Environment )

      else { ($ServerNumber % 2 -eq '1')
       $Node = ($ServerName1 + '"{0:D2}" -f [int]$ServerNumber++'  + $Environment)
    Write Output = $Node

    #New-NetFirewallRule  -DisplayName 'Allow SQL Traffic between AlwaysOn Nodes' -Enabled True -Direction Inbound -Protocol ANY -Action Allow -Profile ANY -RemoteAddress $IPAddress
    #New-NetFirewallRule  -DisplayName 'Allow SQL Traffic from Application' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort $Port,5022 -Profile ANY 
    #New-Cluster -Name $ClusterName_Cluster -Node $FullServerName,$Node_Cluster -StaticAddress "$IPAddress_Cluster"

    Friday, June 14, 2019 5:34 PM

All replies

  • Try this:

    if($ServerNumber % 2 -eq '0'){
        $Node = "{0}{1:D2}{2}" -f $ServerName1, [int]--$ServerNumber, $Environment
      else { ($ServerNumber % 2 -eq '1')
        $Node = "{0}{1:D2}{2}" -f $ServerName1, [int]++$ServerNumber, $Environment

    BTW, you're using post-increment/decrement operators (++/--) where you actually want to increment/decrement the $ServerNumber variable before you use it in the $Node result.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, June 14, 2019 6:12 PM
  • Just use basic math.

    even/odd = number mod 2

    if($num % 2){
        # number is odd
    $node = '02' }else{ # number is even
    $node = '01' }
    This works for any number from 0 to infinity.


    Friday, June 14, 2019 6:14 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Tuesday, June 18, 2019 9:13 AM