I was working on a client site last year and setting them up a reference image task sequence for Windows 10. The VMs that I was working with were hosted in a subnet that did not have DHCP configured. This gave me the issue of configuring the IP statically on the VM that was used to build and capture the reference image BEFORE light touch is launched. So I wrote this little script (with google’s help) to grab the MAC address of the virtual NIC that had been initialised on the VM, match it to a variable and set the IP accordingly to the adaptor by referencing its name.
To implement this script into your MDT boot image there are a couple of steps that have to be performed. The first is to tell the boot image to run it beofre litetouch is called! To do this we have to place an updated Unattend.xml into the boot image.
Thankfully MDT has a proccess for adding extrafiles to your bootimage.
But firtly, we need to get sort out the script and xml.
So…
-
Create a folder called “Static IP”.
-
Then copy and paste the XML code from below into a text document and save it in the Static IP folder called “Unattend.xml”.
NOTE: This XML has been built to work with x64 boot images and x86.)
<?xml version="1.0" encoding="utf-8"?> <unattend xmlns="urn:schemas-microsoft-com:unattend"> <settings pass="windowsPE"> <component name="Microsoft-Windows-Setup" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <RunSynchronous> <RunSynchronousCommand wcm:action="add"> <Description>Set Static IP VM Based</Description> <Order>1</Order> <Path>cscript.exe x:\Set-WinPE-StaticIP.vbs</Path> </RunSynchronousCommand> <RunSynchronousCommand wcm:action="add"> <Description>MDT</Description> <Path>wscript.exe X:\Deploy\Scripts\LiteTouch.wsf</Path> <Order>2</Order> </RunSynchronousCommand> </RunSynchronous> </component> <component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <RunSynchronous> <RunSynchronousCommand wcm:action="add"> <Description>Set Static IP VM Based</Description> <Order>1</Order> <Path>cscript.exe x:\Set-WinPE-StaticIP.vbs</Path> </RunSynchronousCommand> <RunSynchronousCommand wcm:action="add"> <Path>wscript.exe X:\Deploy\Scripts\LiteTouch.wsf</Path> <Order>2</Order> <Description>MDT</Description> </RunSynchronousCommand> </RunSynchronous> </component> </settings> </unattend>
-
Now open notepad again, copy and paste the VBscript from below and then save it as “Set-WinPE-StaticIP.vbs” into the “Static IP” Folder.
-
Now identify the MAC address of the VM(s) you will be targeting as this is what the script identifies the machine with, and place them into the MAC address variable fields (strRefVM1 & strRefVM2).
-
Now populate the IPs as required.
-
At this point is up to you to find the description of the network adaptor as this is used to grab the MAC address. I have supplied the two that I usually see for HyperV and VMware VMs but it is always best to check!
-
So boot up your current MDT Boot ISO and grab the description from the network adaptor that has been initialised by running an “Ipconfig /all”.
-
Now copy and paste it into the variable value for “strNetworkAdapter” or just uncomment the one you require.
- And finally comment out the Ethernet name (strEthName) that does NOT apply to your environment. For example if you are running Hyper V VMs comment out the VMware line and vice versa.
'==================================================== 'Author: Richie Schuster C5 Alliance 'Date : 19-09-2017 'Info : Script sets IP address in WinPE for MDT by Machine MAC '==================================================== 'Variables and Const Dim strMAC Const strMachineName = "." Const strNetworkAdapter = "Intel(R) 82574L Gigabit Network Connection" 'Vmware 'Const strNetworkAdapter = "Intel 21140-Based PCI Fast Ethernet Adapter (Emulated)" 'HyperV Const strRefVM1 = "00:0C:29:8B:7C:AD" Const strRefVM2 = "00:0C:29:8B:7C:A2" Const strDNS1 = "10.193.0.1" Const strDNS2 = "10.193.0.2" Const strGW = "10.193.16.254" Const strSubNet = "255.255.255.0" Const strRefVM1ip = "10.193.16.15" Const strRefVM2ip = "10.193.16.16" Const strEthName = "Ethernet0" 'VMWare 'Const strEthName = "Ethernet" 'HyperV 'Create Shell object Set objShell = WScript.CreateObject("WScript.Shell") 'Run Sub to get MAC of machine getMAC strMachineName 'Echo the MAC Wscript.Echo "Machine MAC Address: " & strMAC 'Check which machine it is and then set IP and DNS servers accordingly If strMAC = strRefVM1 Then WScript.Echo "Reference Machine 1 found. Setting IP..." Return = objShell.Run("cmd.exe /c netsh int ipv4 set address name=" & strEthName & " static " & strRefVM1ip & " " & strSubNet & " " & strGW & " 1", 1, true) Return = objShell.Run("cmd.exe /c netsh interface ipv4 set dns " & strEthName & " static " & strDNS1, 1, true) Return = objShell.Run("cmd.exe /c netsh interface ipv4 add dnsserver name=" & strEthName & " address=" & strDNS2 & " index=2", 1, true) ElseIf strMAC = strRefVM2 Then WScript.Echo "Reference Machine 2 found. Setting IP..." Return = objShell.Run("cmd.exe /c netsh int ipv4 set address name=" & strEthName & " static " & strRefVM2ip & " " & strSubNet & " " & strGW & " 1", 1, true) Return = objShell.Run("cmd.exe /c netsh interface ipv4 set dns " & strEthName & " static " & strDNS1, 1, true) Return = objShell.Run("cmd.exe /c netsh interface ipv4 add dnsserver name=" & strEthName & " address=" & strDNS2 & " index=2", 1, true) Else WScript.Echo "Machine not Configured for Static IP in script." End If 'Sleep to allow for machine to correctly connect. WScript.Sleep(10000) Sub getMAC(strComputer) Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration Where Description = '" & strNetworkAdapter & "'") For Each objItem in colItems strMAC = objItem.MACAddress Next End Sub
-
Now open MDT and right click your deployment share and click “properties”.
-
Now select the “Windows PE” tab.
-
For each boot image you require the static IPs to be set click browse on the “Extra directory to add” field and import the “Static IP” folder.
-
Next click Apply.
-
Now right click the deployment share and update your boot images.
-
And thats it! Mount the ISO on your VM and hey presto…. no more IP issues! 🙂