Submitted By: Randy Johnson

Moves multiple Virtual Machines to a new subnet by changing the static IP address of each machine. There are actually two scripts here: Salmon.vbs and Spawn.vbs. Salmon.vbs reads a list of IP addresses, modifies Spawn.vbs, copies Spawn.vbs to the C: drive of the VM, and schedules Spawn.vbs to run at a certain day and time. When Spawn.vbs runs, it changes the IP address, then deletes itself.

Visual Basic
Edit|Remove
'Salmon.vbs

'   Change the IP address and default gateway on a list of computers by IP address 
'   First, read an IP address from a file which will be used to connect to the
'   computer and also used to create the new subnet address.


Dim strFilename
strFilename = "C:\scripts\addresses.txt"
Const ForReading = 1
Const ForWriting = 2
Const OverwriteExisting = TRUE

Sub DoObject(strName)
       ' Create new subnet address
        strNew = Replace(strName, ".117.", ".109.")        

            'first, turn on error trapping
            On Error Resume Next

        'Change the IP Address in spawn.vbs, a script which will be copied to the
        'computer and run locally using the Scheduler. After the script runs locally,
        'it will delete itself

        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objTextFile = objFSO.OpenTextFile("c:\scripts\spawn.vbs", ForReading)


        Do Until objTextFile.AtEndOfStream
           strNextLine = objTextFile.Readline

           'search the script for this line
           intLineFinder = InStr(strNextLine, "strIPAddress =")

           'create a new line with the new subnet address
           If intLineFinder <> 0 Then
              strNextLine = "strIPAddress = Array(" & chr(34) & strNew & chr(34) & ")"
           End If
           strNewFile = strNewFile & strNextLine & vbCrLf
        Loop

        objTextFile.Close

        'Now open the script for writing and make the change.

        Set objTextFile = objFSO.OpenTextFile("c:\scripts\spawn.vbs", ForWriting)

        objTextFile.WriteLine strNewFile
        objTextFile.Close

        'Copy the changed script to the computer's c: drive

        Set objFSO = CreateObject("Scripting.FileSystemObject")

        objFSO.CopyFile "C:\Scripts\spawn.vbs", _
        "\\" & strName & "\C$\", OverWriteExisting
            
        'Make a WMI connection 
        'We need to create a job to run the script automatically.

        Dim objWMIService


        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strName & "\root\cimv2")
            'Set objWMI = GetObject("winmgmts:\\" & strName & "\root\cimv2")


            'did an error occur?
            If Err <> 0 Then
                        WScript.Echo "Couldn't connect to " & strName
            Else

                'execute WMI query, then create a scheduled job to run once
                'After the script runs, it will delete itself

                Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

                ' days of week: 1-Mon, 2-Tue, 4-Wed, 8-Thu, 16-Fri, 32-Sat, 64-Sun

                errJobCreated = objNewJob.Create _
                    ("wscript.exe c:\spawn.vbs", "********220000.000000-240", _
                        False, 16, , False, JobID) 
                WScript.Echo "Status: " & errJobCreated        
            End If

            'turn off error trapping
            On Error GoTo 0

End Sub

 

Dim objFSO, objTS, strName      

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTS = objFSO.OpenTextFile(strFilename)

Do Until objTS.AtEndOfStream

            strName = objTS.ReadLine

            WScript.Echo "Read " & strName & " from file..."

            DoObject strName          

Loop

objTS.Close

 

 

'Spawn.vbs


'     Designed to be modified by another script, then copied to a remote
'     computer to be run locally by the Scheduler.
 

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True")
 

' This line is modified by the other script
strIPAddress = Array("10.97.109.231")
 

' These lines are static
strSubnetMask = Array("255.255.255.0")
strGateway = Array("10.97.109.254")
strGatewayMetric = Array(1)
 

' This sets the new IP Address, subnet mask, default gateway, and gateway metric
For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateway = objNetAdapter.SetGateways(strGateway, strGatewayMetric)
Next

 
' Wait three seconds after finishing and either die or get eaten by a bear
Set objFSO = CreateObject("Scripting.FileSystemObject")

Wscript.Sleep 3000

strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript)