none
VB script RRS feed

  • General discussion

  • Hi,

    I need VB script for Microsoft Failover Cluster.

    Script should remove/delete an IP address from network adapter and add another IP address.

    Key point is to add IP address, not to change current.

    I'm using cluster to manage print servers, and users can access shared printers by "X" IP address.

    When another node take over, role will use script to remove one IP address for selected adapter and add "X" IP address for same adapter. 

    If someone can help me,

    Thanks

    • Changed type Bill_Stewart Thursday, September 5, 2019 9:39 PM
    • Moved by Bill_Stewart Thursday, September 5, 2019 9:39 PM This is not "scripts on demand"
    Wednesday, July 3, 2019 2:40 PM

All replies

  • Please read the following post first. It is the very first post right at the top of this forum.

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 3, 2019 2:59 PM
  • Here's a vb script designed to reconfigure an adapter with a fixed IP, and it's designed to work across different networks.  I wrote it to migrate computer's with fixed IP's to their new subnets when the entire network changed structure (got a new ISP).  It bases the decision on the existing gateway found on adapters with fixed IP addresses (basically, you are currently on this subnet).  Perhaps the code might be useful in showing you how to discover adapters with fixed IPs, getting their existing settings (IP, gateway, subnet mask), then changing that to work on a new subnet.  The script was designed to run on the computer itself (based on strcomputer = ".").

    ' begin vbscript
    ' ************************************
    ' *  Wait 5 min for the network to come up
    ' *  Determine if the IPv4 address is fixed IP (not DHCP enabled)
    ' *  If yes then
    ' *    Determine if the IPv4 gateway matches any of the old gateways in MY_OLD_GATEWAYS
    ' *    If yes then
    ' *      Computer is using old IPV4 address, need to change it to new subnet
    ' *      Get last octect of current IP address
    ' *      Combine with first 3 octets of of MY_NEW_GATEWAY
    ' *      This will be <New IP address>
    ' *      Reconfigure Network Interface to a new IP address (will configure a single IP)
    ' *      IPv4 address = <New IP address>
    ' *      IPv4 Subnet mask = MY_NEW_SUBNET_MASK
    ' *      IPv4 DNS1 = MY_NEW_DNS1
    ' *      IPv4 DNS2 = MY_NEW_DNS2
    '
    ' ************************************
    '
    ' Reference Info Contoso Paper:
    ' Old Subnets:  all 255.255.255.0
    ' Old DNS1:  all 10.0.0.2 (SASVR) except Houston 10.0.3.2 (HOUSVR)
    ' Old DNS2:  all 10.0.3.2 (HOUSVR) except Houston 10.0.0.2 (SASVR)
    ' Existing sites:     San Antonio,    Austin,        Corpus Christi, Houston,        Dallas
    ' Old gateway:        10.0.0.254,     10.0.2.254,    10.0.1.254,     10.0.3.253,     10.0.5.1
    ' New Gateway:        10.144.28.1,    10.144.32.1,   10.144.36.1,    10.144.40.1,    10.144.44.1
    ' New Subnet:         255.255.252.0,  255.255.252.0, 255.255.252.0,  255.255.252.0,  255.255.252.0
    ' New DNS1:           10.144.28.2,    10.144.28.2,   10.144.28.2,    10.144.40.2,    10.144.28.2
    ' New DNS2:           10.144.40.2,    10.144.40.2,   10.144.40.2,    10.144.28.2,    10.144.40.2
    '
    ' Exclude these domain PCs: SASVR,HOUSVR
    '
    ' ************************************
    '
    ' SETTINGS (edit settings as desired)
    '
    ' List of PCs you do not want to change IPs (i.e. servers) seperated by commas (no spaces)
    Const MY_EXCLUDE_PCs = "SASVR,HOUSVR"
    '
    ' for comma seperated fields MY_OLD_GATEWAYS, MY_NEW_GATEWAYS, MY_NEW_SUBNET_MASKS, MY_NEW_DNS1, MY_NEW_DNS2,
    ' the number of entries must be equal
    ' The order of each entry must match the corresponding entry in the other fields for each network
    ' i.e. the 5th entry in each field is for network 5
    ' Set MY_OLD_GATEWAYS to old IPv4 gateway for each network seperated by commas (no spaces)
    Const MY_OLD_GATEWAYS="10.0.0.254,10.0.2.254,10.0.1.254,10.0.3.253,10.0.5.1"
    ' Set MY_NEW_GATEWAYS to new IPv4 gateway for each network seperated by commas (no spaces)
    Const MY_NEW_GATEWAYS="10.144.28.1,10.144.32.1,10.144.36.1,10.144.40.1,10.144.44.1"
    ' Set MY_NEW_SUBNET_MASKS to new IPv4 subnet mask for each network seperated by commas (no spaces)
    Const MY_NEW_SUBNET_MASKS="255.255.252.0,255.255.252.0, 255.255.252.0,255.255.252.0,255.255.252.0"
    ' Set the MY_NEW_DNS1 to the Primary DNS server for each network seperated by commas (no spaces)
    Const MY_NEW_DNS1="10.144.28.2,10.144.28.2,10.144.28.2,10.144.40.2,10.144.28.2"
    ' Set the MY_NEW_DNS2 to the Secondary DNS server for each network seperated by commas (no spaces)
    Const MY_NEW_DNS2="10.144.40.2,10.144.40.2,10.144.40.2,10.144.28.2,10.144.40.2"
    '
    ' To write a log file, set WRITE_LOG = true
    ' To not write a log file, set WRITE_LOG = false
    Const WRITE_LOG = true
    ' Path and filename of logfile (path must be present on every computer)
    Const MY_LOG_FILE = "c:\IP-Change.log"
    '
    ' ************************************
    ' Main Program
    On Error Resume Next
    ' sleep for 5 min
    wscript.sleep 300000
    ' end sleep
    strComputer = "."
    strReport=""
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim arrOldIP()
    Dim arrOldIPSubnet()
    Dim arrOldGateway()
    Dim arrOldDNS()
    Dim arrOldDHCPServer()
    arrExcludePCs = Split(MY_EXCLUDE_PCs,",")
    arrMYOLDGATEWAYS = Split(MY_OLD_GATEWAYS,",")
    arrMYNEWGATEWAYS = Split(MY_NEW_GATEWAYS,",")
    arrMYNEWSUBNETS = Split(MY_NEW_SUBNET_MASKS,",")
    arrMYNEWDNS1 = Split(MY_NEW_DNS1,",")
    arrMYNEWDNS2 = Split(MY_NEW_DNS2,",")
    Set objDictionaryGateways = CreateObject("Scripting.Dictionary")
    Set objDictionaryExcludePCs = CreateObject("Scripting.Dictionary")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set IPConfigSet = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE",,48)
    For Each objItem in IPConfigSet
      If Not IsNull(objItem.IPAddress) Then
        strReport = strReport & "Host Name:            " & objItem.DNSHostName & vbcrlf
        strReport = strReport & "Network Interface:    " & objItem.Description & vbcrlf
        strReport = strReport & "Physical Address:     " & objItem.MACAddress & vbcrlf
        strReport = strReport & "DHCP Enabled:         " & objItem.DHCPEnabled & vbcrlf
        strReport = strReport & "DNS Suffix:           " & objItem.DNSDomain & vbcrlf
        ReDim arrOldIP(Ubound(objItem.IPAddress))
        ReDim arrOldIPSubnet(Ubound(objItem.IPAddress))
        ReDim arrOldDHCPServer(Ubound(objItem.IPAddress))
        For i=LBound(objItem.IPAddress) to UBound(objItem.IPAddress)
          arrOldIP(i) = objItem.IPAddress(i)
          arrOldIPSubnet(i) = objItem.IPSubnet(i)
          arrOldDHCPServer(i) = objItem.DHCPServer(i)
        Next
        For i=LBound(arrOldIP) to UBound(arrOldIP)
          If Not Instr (arrOldIP(i), ":") > 0 Then
            strReport = strReport & "IPv4 Address:         " & arrOldIP(i) & vbcrlf
            strReport = strReport & "IPv4 Subnet Mask:     " & arrOldIPSubnet(i) & vbcrlf
            strReport = strReport & "IPv4 DHCPServer:      " & arrOldDHCPServer(i) & vbcrlf
          Else
            'strReport = strReport & "IPv6 Address:         " & arrOldIP(i) & vbcrlf
            'strReport = strReport & "IPv6 Subnet Mask:     " & arrOldIPSubnet(i) & vbcrlf
            'strReport = strReport & "IPv4 DHCPServer:      " & arrOldDHCPServer(i) & vbcrlf
          End If
        Next
        If Not IsNull(objItem.DefaultIPGateway) Then
          ReDim arrOldGateway(UBound(objItem.DefaultIPGateway))
          For i=LBound(objItem.DefaultIPGateway) to UBound(objItem.DefaultIPGateway)
            arrOldGateway(i) = objItem.DefaultIPGateway(i)
          Next
          For i=LBound(arrOldGateway) to UBound(arrOldGateway)
            strReport = strReport & "Default Gateway:      " & arrOldGateway(i) & vbcrlf
          Next
        End If
        If Not IsNull (objItem.DNSServerSearchOrder) Then
          ReDim arrOldDNS(UBound(objItem.DNSServerSearchOrder))
          For i=LBound(objItem.DNSServerSearchOrder) to UBound(objItem.DNSServerSearchOrder)
            arrOldDNS(i) = objItem.DNSServerSearchOrder(i)
          Next
          For i=LBound(arrOldDNS) to UBound(arrOldDNS)
            strReport = strReport & "DNS" & i+1 & ": " & arrOldDNS(i) & vbcrlf
          Next
        End If
      End If
      strmatch = false
      For i=LBound(arrExcludePCs) to UBound(arrExcludePCs)
        if UCase(objItem.DNSHostName) = UCase(arrExcludePCs(i)) then
          strmatch = true
          If objItem.DHCPEnabled = false Then
            strReport = strReport & vbcrlf & "WARNING:  " & objItem.DNSHostName & " excluded from IP Changes." & vbcrlf
          End If
        End If
      Next
      If strmatch = false then
        If objItem.DHCPEnabled = false Then
          for j=LBound(arrMYOLDGATEWAYS) to UBound(arrMYOLDGATEWAYS)
            strmatch = false
            for i=LBound(arrOldGateway) to UBound(arroldGateway)
              If arrMYOLDGATEWAYS(j) = arrOldGateway(i) then
                strmatch = true
              End if
            Next
            If strmatch = true Then
              strIP = ""
              strMask = ""
              arrGateways = ""
              For i=LBound(arrOldIP) to UBound(arrOldIP)
                If Not Instr (arrOldIP(i), ":") > 0 Then
                  intLastDecimal = 1
                  For intCounter = 1 to 3
                    intLastDecimal = InStr(intLastDecimal, arrOldIP(i), ".") + 1
                  Next
                  strNewIP = Right(arrOldIP(i), Len(arrOldIP(i)) - intLastDecimal + 1)
                  intLastDecimal = 1
                  For intCounter = 1 to 3
                    intLastDecimal = InStr(intLastDecimal,arrMYNEWGATEWAYS(j),".") + 1
                  Next
                  strNewIP = Left(arrMYNEWGATEWAYS(j),intLastDecimal - 1) & strNewIP
                  strIP = strNewIP
                  strMask = arrMYNEWSUBNETS(j)
                End If
              Next
              ' Commit Changes
              errEnable = objItem.EnableStatic(Array(strIP),Array(strMask))
              errGateway = objItem.SetGateways(Array(arrMYNEWGATEWAYS(j)),Array(1))
              ErrDNS = objItem.SetDNSServerSearchOrder(Array(arrMYNEWDNS1(j), arrMYNEWDNS2(j)))
              ' Changes Commited
              strReport = strReport & "New IPv4 Address:     " & strNewIP & vbcrlf
              strReport = strReport & "New IPv4 Subnet Mask: " & arrMYNEWSUBNETS(j) & vbcrlf
              strReport = strReport & "New IPv4 Gateway:     " & arrMYNEWGATEWAYS(j) & vbcrlf
              strReport = strReport & "New IPv4 DNS1:        " & arrMYNEWDNS1(j) & vbcrlf
              strReport = strReport & "New IPv4 DNS2:        " & arrMYNEWDNS2(j) & vbcrlf
            End If
          Next
        End If
      End If
      strReport = strReport & vbcrlf
    Next
    ' Reporting
    If WRITE_LOG = true then
      Set objFSO = CreateObject("Scripting.FilesystemObject")
      If objFSO.FileExists(MY_LOG_FILE) then
        'File Exists, from prior run of script.  Do not overrwite
      Else
        Set objOutput = objFSO.OpenTextFile(MY_LOG_FILE, ForWriting, True)
        objOutput.Writeline(strReport)
        objOutput.close
      End If
    End If
    'wscript.echo strReport
    ' ************************************
    ' End vbscript

    Wednesday, July 3, 2019 4:11 PM
  • Your script has broken (actually, nonexistent) error handling. It is not good practice to insert a blanket On Error Resume Next at the top of the script. It is unwise to use that line of code unless you understand what it does and how it works.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 3, 2019 4:17 PM
  • Thank you, I've read the post. I'm sorry for writing it that way, I've lost ours yesterday searching for script that I maybe could change to met my needs. I've never been scripting before except copying some simple scripts.

    Now I've run into a problem where I need to represent print queues to end users with single IP address for two print servers. One server has enabled print spooler service it is controlled by failover cluster. When that node fails, print spooler is started on other node, but end users can't access it if it doesn't have same IP address as previous node.

    Thursday, July 4, 2019 7:26 AM
  • You need to post your issue in the clustering forum.  There is no need to do what you are trying to do.  Failover clustering is what we use to allow seamless access to service on failover.  Once a cluster resource is allocated and used, in this case a print queue,  the clustering system handles the rest.  The client system has no idea that it is using a cluster.  The clustering forum will help you understand how to set this up.  It does not require any scripting.


    \_(ツ)_/

    Thursday, July 4, 2019 7:37 AM