Using PowerCLI to set IP and Update VMware Tools

This is a continuation of my last post, Using PowerCli to Build multiple VMs.

After automating the building of VMs, what’s the sense of having to manually add the IP information or updating the VMware tools? It’s pointless. So we need to add the IP information to the CSV file that was used to build the VMs.

CSV file to pull configuration information for setting VM properties
CSV file to pull configuration information for setting VM properties

Starting where I left off, the newly built VMs will need to be powered on.

####### Start VM
foreach ($vm in $vms){
      $VMName = $vm.Name
      Start-VM -VM $VMName -Confirm:$False
}

That was simple enough. Now the VMs have been powered on, and are running through the VM customization that I set during the build process. So, I need to figure out a way to make sure that the script waits until the system has finished this process before continuing. When I first started this process, I thought about just using the sleep command.

Sleeep -seconds 300

Seems good, but what if am “sleeping” the script for longer than I need to, or not long enough? But I tried it anyway, it failed bad, and didn’t work the way I wanted it to. What about waiting for the VMware tools to be running? It has to be running when the OS is booted, right?

$VMTool = Get-VM $VMName | Out-Null
$VMTool | Select -ExpandProperty ExtensionData | Select -ExpandProperty guest
$VMToolStatus = $VMTool.ToolsRunningStatus
Write-host "Checking that VMWare Tools are running on"$VMName -ForegroundColor Yellow
Sleep -Seconds 5
Do {Write-host "Still checking for VMWare Tools on"$VMName -ForegroundColor Yellow; sleep -Seconds 5}
While ($VMToolStatus -eq "guestToolsRunning")
Write-Host "VMWare tools are now running on"$VMName -ForegroundColor Green

The above seems like the perfect fix for getting the system to wait until the system was finished doing the customizations. But as it turns out, it isn’t, and I’m back at the drawing board. Although I did leave it in my script, but only to make sure that the tools upgrade works. Then it hit me…

DO {(Get-VMGuest $VMName).HostName}
While (((Get-VMGuest $VMName).HostName) -Ne "$VMName")
General information about VM
General information about VM

I just need to wait until the DNS name, matches the name that is in the CSV file. I put this piece into the script, and it worked perfectly. The script waited just the correct amount of time for the customization to complete.

 

 

The next step in the script updates the VMware Tools

Get-VM $VMName | Update-Tools

Simple and straight forward command. Update-Tools. Now the fun part, configuring the NIC. This was a tough one to get to working correctly. A few PowerCli versions back, there was a cmdlet that could be used to set the network information, Set-VMGuestNetworkInterface, but it only worked with Windows Server 2008 R2 and below. This worked great, until Windows Server 2012 came out and then the cmdlet was deprecated. So at one time during this script’s evolution, I had two sections that would be used to set the network adapter. It was based on whether it was Windows Server 2008 R2 and below, or Windows Server 2012. So the solution that I was able to come up with is as follows:

$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "Local_Admin_Account", "")

$Network = Invoke-VMScript -VM $VMName -ScriptType Powershell -ScriptText "(gwmi Win32_NetworkAdapter -filter 'netconnectionid is not null').netconnectionid" -GuestUser administrator -GuestPassword password
$NetworkName = $Network.ScriptOutput
$NetworkName = $NetworkName.Trim()
Write-Host "Setting IP address for $VMname..." -ForegroundColor Yellow
Sleep -Seconds 60
$netsh = "c:\windows\system32\netsh.exe interface ip set address ""$NetworkName"" static $IP $SNM $GW" 
$netsh2 = "c:\windows\system32\netsh.exe interface ip set dnsservers ""$NetworkName"" static $DNS1"
$netsh3 = "c:\windows\system32\netsh.exe interface ip add dnsservers ""$NetworkName"" $DNS2"
$netsh4 = "c:\windows\system32\netsh.exe interface ip set winsservers ""$NetworkName"" static $WINS1"
$netsh5 = "c:\windows\system32\netsh.exe interface ip add winsservers ""$NetworkName"" $WINS2"
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh  
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh2  
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh3  
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh4  
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh5  
Write-Host "Setting IP address completed." -ForegroundColor Green

I had to start with authenticating to the guest VM. This script works with multiple different operating sytems, so it needs to be universal. As a result, the first part of the script uses a WMI query to get the network card name, and scrubs the variable into a usable information. Then uses NETSH.exe to configure the network interface. So the line starting with $netsh sets the static IP address, subnet mask, and gateway using the information from the WMI query. Then the next 2 line sets the DNS settings and the last 2 lines sets the WINS. It then uses Invoke-VMScript to push these settings to the VM via the VMware Tools, which is why it so important that the tools are up to date.

So the completed script looks like this.

$vms = Import-CSV "C:\Scripts\VMWare\VM Creation\NewVMs.csv"
$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "Administrator", "")

foreach ($vm in $vms){
$VMName = $vm.Name
$IP = $vm.IP
$SNM = $vm.SubnetMask
$GW = $vm.Gateway
$DNS1 = $vm.DNS1
$DNS2 = $vm.DNS2
$WINS1 = $vm.WINS1
$WINS2 = $vm.WINS2
$Network = $vm.Network
$Template = $vm.Template

DO {(Get-VMGuest $VMName).HostName}
while (((Get-VMGuest $VMName).HostName) -Ne "$VMName")

Get-VM $VMName | Update-Tools
$VMTool = Get-VM $VMName | Out-Null
$VMTool | Select -ExpandProperty ExtensionData | Select -ExpandProperty guest
$VMToolStatus = $VMTool.ToolsRunningStatus
Write-host "Checking that VMWare Tools are running on"$VMName -ForegroundColor Yellow
Sleep -Seconds 5
Do {Write-host "Still checking for VMWare Tools on"$VMName -ForegroundColor Yellow; sleep -Seconds 5}
While ($VMToolStatus -eq "guestToolsRunning")
Write-Host "VMWare tools are now running on"$VMName -ForegroundColor Green

$Network = Invoke-VMScript -VM $VMName -ScriptType Powershell -ScriptText "(gwmi Win32_NetworkAdapter -filter 'netconnectionid is not null').netconnectionid" -GuestUser administrator -GuestPassword password
$NetworkName = $Network.ScriptOutput
$NetworkName = $NetworkName.Trim()
Write-Host "Setting IP address for $VMname..." -ForegroundColor Yellow
Sleep -Seconds 60
$netsh = "c:\windows\system32\netsh.exe interface ip set address ""$NetworkName"" static $IP $SNM $GW"
$netsh2 = "c:\windows\system32\netsh.exe interface ip set dnsservers ""$NetworkName"" static $DNS1"
$netsh3 = "c:\windows\system32\netsh.exe interface ip add dnsservers ""$NetworkName"" $DNS2"
$netsh4 = "c:\windows\system32\netsh.exe interface ip set winsservers ""$NetworkName"" static $WINS1"
$netsh5 = "c:\windows\system32\netsh.exe interface ip add winsservers ""$NetworkName"" $WINS2"
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh2
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh3
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh4
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh5
Write-Host "Setting IP address completed." -ForegroundColor Green
}

Hopefully that clarifies some of the issues from the last post, and if you have any questions, hit me up in the comments.

-Stuart

Find this and all of my scripts at https://github.com/NotesofaScripter/PowerCLI

18 thoughts on “Using PowerCLI to set IP and Update VMware Tools”

    1. If you have a single ESXi host you can use the Connect-VIServer to connect to the host and remove the instances of get-cluster from the script. If you have issues, post the part of the script giving errors and I’ll assist with updating it.

  1. Hi,
    I am totally new to powershell scripting and want to automate the VM creation in my home lab.

    I managed to get the first part of the VM script to create a new VM from a Template – all works fine, but when I try to add the second part for setting the IP it seems to be failing in two sections.

    I noticed the variable using in line 2 – $GC = $Host.UI.PromptForCredential(“Please enter credentials”, “Enter Guest credentials for VM”, “Administrator”, “”) is not referenced in the script and I don’t know where I need to add it. Perhaps you can assist me.

    Below are the two errors I think these are being cause because the credential have not been passed.

    Update-Tools : 21/12/2018 19:47:03 Update-Tools Server task failed: Error upgrading VMware Tools. vix error code = 21009
    At line:19 char:18
    + Get-VM $VMName | Update-Tools

    Then it fails again, I noticed that the

    Invoke-VMScript : 21/12/2018 19:47:15 Invoke-VMScript Failed to authenticate with the guest operating system using the supplied credentials.
    At line:29 char:12
    + $Network = Invoke-VMScript -VM $VMName -ScriptType Powershell -Script …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    1. Mayur,
      Happy holidays! Sorry for the delay in my response, the holidays are rather busy at my house. I have read your comments and I hope I have answered everything.
      1. The variable $GC is used with the lines that start with Invoke-VMScript. This allows for authentication to the guest VM.
      2. Just looking at the errors you posted you might need to check out the template you are using as it might be the issues -> https://kb.vmware.com/s/article/2129927.
      3. I have a better script for building VMs that you might give a try, link here -> (https://github.com/NotesofaScripter/PowerCLI/blob/master/Building_VMs_using_OSCustomizationSpec.ps1).

      This one does the IP’ing of the VM via the OS customization spec, and it can also add to the domain if you have one (link to the blog
      post
      .

      I hope that this helps, and if you need anything else just let me know.

      -Stuart

  2. The requested operation requires elevation (Run as administrator). It won’t let me set Ip address on Windows 10 vm using this method. Any suggestion

    1. Ketan,
      Sorry this isn’t work. There is another way to assign the IPs. You would need to setup a OS customiztion spec, then followed up with a OS customization NIC mapping. This is like sysprep’ing a windows image for deployment, but it includes the IP information for the VM. I have a been meaning to write a post about it, but work has been keeping very busy. Nevertheless, the basic commands are the following.

      New-OSCustomizationSpec -OrgName "Company Name" -OSType Windows -ChangeSid -Server $global:DefaultVIServer -Name $OSCustomization -FullName "CompanyName" -Type Persistent -AdminPassword "Password" -TimeZone 'Eastern (U.S. and Canada)' -AutoLogonCount 1 -NamingScheme Vm -Description "PowerCli Use only" -LicenseMode PerServer -LicenseMaxConnections 5 -Confirm:$false -ErrorAction Stop
              
      Get-OSCustomizationNicMapping -OSCustomizationSpec $OSCustomization | Set-OSCustomizationNicMapping -Position 1 -IpMode UseStaticIP -IpAddress $IP_NIC1 -SubnetMask $SubnetMask_NIC1 -DefaultGateway $Gateway_NIC1 -Dns $DNS1_NIC1,$DNS2_NIC1 -Confirm:$false -ErrorAction Stop
      

      Source –
      New-OSCustomizationSpec – https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/New-OSCustomizationSpec.html
      Get-OSCustomizationNicMapping – https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Set-OSCustomizationNicMapping.html

      -Stuart

  3. Hi, I need to change the IP,subnet and DG on multiple VMs. Could I use this script purely for this? I have a script I can run which will then change the port groups of each VM to the new network. I just need a script to actually change the IP settings.

    1. Dara,
      Yes, this should work as I used it to change an entire environment before to a new subnet. Athough, I would try on a test machine first, just to make sure.

      -Stuart

        1. When I ppulate the CSV with a single VM and run the script it prompts for credentials which I enter and then I ht enter to run the script and it just keeps returning the server name over and over until I cancel it. Any ideas? Thanks

          1. I’ve tweaked script to below

            $vms = Import-CSV "C:\Users\dodoherty\Desktop\2019\IP_Address_Changes\NewVMwareScript\VMs.csv"
            $GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "Administrator", "")
            
            
            foreach ($vm in $vms){
                    $VMName = $vm.Name
                    $IP = $vm.IP
                    $SNM = $vm.SubnetMask
                    $GW = $vm.Gateway
                    $DNS1 = $vm.DNS1
                    $DNS2 = $vm.DNS2
                    $Network = $vm.Network
            
                    Get-VM $VMName
                    $VMTool = Get-VM $VMName | Out-Null
                    $VMTool | Select -ExpandProperty ExtensionData | Select -ExpandProperty guest
                    $VMToolStatus = $VMTool.ToolsRunningStatus
                    Write-host "Checking that VMWare Tools are running on"$VMName -ForegroundColor Yellow
                    Sleep -Seconds 5
                    Do {Write-host "Still checking for VMWare Tools on"$VMName -ForegroundColor Yellow; sleep -Seconds 5}
                    While ($VMToolStatus -eq "guestToolsRunning")
                    Write-Host "VMWare tools are now running on"$VMName -ForegroundColor Green
                  
                    
                    $Network = Invoke-VMScript -VM $VMName -ScriptType Powershell -ScriptText "(gwmi Win32_NetworkAdapter -filter 'netconnectionid is not null').netconnectionid" -GuestUser administrator -GuestPassword 
                    $NetworkName = $Network.ScriptOutput
                    $NetworkName = $NetworkName.Trim()
                    Write-Host "Setting IP address for $VMname..." -ForegroundColor Yellow
                    Sleep -Seconds 60
                    $netsh = "c:\windows\system32\netsh.exe interface ip set address ""$NetworkName"" static $IP $SNM $GW" 
                    $netsh2 = "c:\windows\system32\netsh.exe interface ip set dnsservers ""$NetworkName"" static $DNS1"
                    $netsh3 = "c:\windows\system32\netsh.exe interface ip add dnsservers ""$NetworkName"" $DNS2"
                    Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh 
                    Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh2  
                    Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh3 
                    Write-Host "Setting IP address completed." -ForegroundColor Green
                    }
            

            However when I run it I get the below 3 errors.

            Name PowerState Num CPUs MemoryGB
            —- ———- ——– ——–
            IE-TEST-VM PoweredOn 2 10.000
            Checking that VMWare Tools are running on IE-TEST-VM
            Still checking for VMWare Tools on IE-TEST-VM
            VMWare tools are now running on IE-TEST-VM
            Setting IP address for IE-TEST-VM…
            Invoke-VMScript : 19/06/2019 11:50:32 Invoke-VMScript A specified parameter was not correct:
            At line:29 char:9
            + Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType …
            + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            + CategoryInfo : NotSpecified: (:) [Invoke-VMScript], InvalidArgument
            + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RunScriptInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

            Invoke-VMScript : 19/06/2019 11:50:33 Invoke-VMScript A specified parameter was not correct:
            At line:30 char:9
            + Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType …
            + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            + CategoryInfo : NotSpecified: (:) [Invoke-VMScript], InvalidArgument
            + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RunScriptInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

            Invoke-VMScript : 19/06/2019 11:50:35 Invoke-VMScript A specified parameter was not correct:
            At line:31 char:9
            + Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType …
            + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            + CategoryInfo : NotSpecified: (:) [Invoke-VMScript], InvalidArgument
            + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RunScriptInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

            Setting IP address completed.

          2. Dara,
            I look it over and there was some pieces missing and there was some extra bits that weren’t needed. Try this on a test machine and see how it works.

            The CSV file that you should be using only needs columns for Name, IP, SubnetMask, Gateway, DNS1, DNS2.

            $vms = Import-CSV "C:\Users\dodoherty\Desktop\2019\IP_Address_Changes\NewVMwareScript\VMs.csv"
            $GC = Get-Credential -Message "Enter Guest credentials for VM"
            
            foreach ($vm in $vms){
            # Defines Variables
            $VMName = $vm.Name
            $IP = $vm.IP
            $SNM = $vm.SubnetMask
            $GW = $vm.Gateway
            $DNS1 = $vm.DNS1
            $DNS2 = $vm.DNS2
            
            # Gets the name of the Network Adapter
            $Network = Invoke-VMScript -VM $VMName -ScriptType Powershell -ScriptText "(gwmi Win32_NetworkAdapter -filter ‘netconnectionid is not null’).netconnectionid" -GuestCredential $GC
            $NetworkName = $Network.ScriptOutput
            $NetworkName = $NetworkName.Trim()
            
            # Starts the setup of the IP, Subnetmask, Gateway and DNS
            Write-Host "Setting IP address for $VMname…" -ForegroundColor Yellow
            $netsh = "c:\windows\system32\netsh.exe interface ip set address ""$NetworkName"" static $IP $SNM $GW"
            $netsh2 = "c:\windows\system32\netsh.exe interface ip set dnsservers ""$NetworkName"" static $DNS1"
            $netsh3 = "c:\windows\system32\netsh.exe interface ip add dnsservers ""$NetworkName"" $DNS2"
            Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh
            Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh2
            Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh3
            Write-Host "Setting IP address completed." -ForegroundColor Green
            }
            

            -Stuart

  4. Thanks for helping with this. Unfortunately I get the below message now after the script is run.

    ScriptOutput
    —————————————————————————————————————————————————————
    | The syntax supplied for this command is not valid. Check help for the correct syntax.
    |
    | Usage: set address [name=]
    | [[source=]dhcp|static]
    | [[address=][/] [[mask=]]
    | [[gateway=]|none [gwmetric=]]
    | [[type=]unicast|anycast]
    | [[subinterface=]]
    | [[store=]active|persistent]
    |
    | Parameters:
    |
    | Tag Value
    | name – Interface name or index.
    | source – One of the following values:
    | dhcp: Enables DHCP for configuring IP addresses for
    | the specified interface.
    | static: Disables DHCP for configuring IP addresses for
    | the specified interface. This value must be
    | specified if an address or a gateway is being
    | configured.
    | address – IPv4 address to add or modify, optionally followed by
    | the subnet prefix length.
    | mask – The IP subnet mask for the specified IP address.
    | gateway – One of the following values:
    | : A specific default gateway for the
    | static IP address you are setting.
    | none: No default gateways are set. This is the default.
    | gwmetric – The metric for the default gateway. This field should
    | be set only if gateway is specified.
    | type – One of the following values:
    | unicast: Marks the address as a unicast address.
    | This is the default.
    | anycast: Marks the address as an anycast address.
    | subinterface – LUID of the subinterface on which the default gateway
    | exists. This parameter is only needed on interfaces
    | with multiple subinterfaces.
    | store – One of the following values:
    | active: Set only lasts until next boot.
    | persistent: Set is persistent. This is the default.
    |
    | Remarks: Used to enable or disable DHCP for IP address configuration.
    | Also removes any previous static IP addresses and default gateways,
    | and can add a new static IP address and default gateway.
    |
    | Examples:
    |
    | set address name=”Wired Ethernet Connection” source=dhcp
    | set address “Wired Ethernet Connection” static 10.0.0.9 255.0.0.0 10.0.0.1 1
    |
    |
    —————————————————————————————————————————————————————
    ScriptOutput
    —————————————————————————————————————————————————————
    | The syntax supplied for this command is not valid. Check help for the correct syntax.
    |
    | Usage: set dnsservers [name=] [source=]dhcp|static
    | [[address=]|none]
    | [[register=]none|primary|both]
    | [[validate=]yes|no]
    |
    | Parameters:
    |
    | Tag Value
    | name – The name or index of the interface.
    | source – One of the following values:
    | dhcp: Sets DHCP as the source for configuring DNS
    | servers for the specific interface.
    | static: Sets the source for configuring DNS servers
    | to local static configuration.
    | address – One of the following values:
    | : An IP address for a DNS server.
    | none: Clears the list of DNS servers.
    | register – One of the following values:
    | none: Disables Dynamic DNS registration.
    | primary: Register under the primary DNS suffix only.
    | both: Register under both the primary DNS suffix, as
    | well as under the connection-specific suffix.
    | validate – Specifies whether validation of the DNS server setting
    | will be performed. The value is yes by default.
    |
    | Remarks: Sets DNS server configuration to either DHCP or static mode. Only
    | when source is ‘static’, is the ‘addr’ option also available for
    | configuring a static list of DNS server IP addresses for the
    | specified interface. If Validate switch is yes, then
    | the newly set DNS server is validated.
    |
    | Examples:
    |
    | set dnsservers name=”Wired Ethernet Connection” source=dhcp
    | set dnsservers “Wired Ethernet Connection” static 10.0.0.1 primary
    |
    |
    —————————————————————————————————————————————————————
    ScriptOutput
    —————————————————————————————————————————————————————
    | The syntax supplied for this command is not valid. Check help for the correct syntax.
    |
    | Usage: add dnsservers [name=] [address=]
    | [[index=]] [[validate=]yes|no]
    |
    |
    | Parameters:
    |
    | Tag Value
    | name – The name or index of the interface where DNS servers
    | are added.
    | address – The IP address for the DNS server you are adding.
    | index – Specifies the index (preference) for the specified
    | DNS server address.
    | validate – Specifies whether validation of the DNS server setting
    | will be performed. The value is yes by default.
    |
    | Remarks: Adds a new DNS server IP address to the statically-configured list.
    | By default, the DNS server is added to the end of the list. If an
    | index is specified, the DNS server will be placed in that position
    | in the list, with other servers being moved down to make room.
    | If DNS servers were previously obtained through DHCP, the new
    | address will replace the old list. If Validate switch is yes, then
    | the newly added DNS server is validated.
    |
    | Examples:
    |
    | add dnsservers “Wired Ethernet Connection” 10.0.0.1
    | add dnsservers “Wired Ethernet Connection” 10.0.0.3 index=2
    |
    |
    —————————————————————————————————————————————————————
    Setting IP address completed.

  5. If I replace the $NetworkName with the actual LAN connection name it seems to work. So the problem seems to be when reading the name of the LAN connection before applying the IP. I will look into this, maybe I can remove that part from the script and populate the CSV with the LAN name and create a new variable. I will try and use a seperate powershell script to perhaps set a generic LAN connection name on all servers before running the powercli script.

  6. Hi Stuart,

    I’m setting up VMs with two network cards. Is there a fast and easy way to include this in the scripts? I’m not guessing so at first blush.

    1. Tyler,
      There is a way to configure a VM with two NICs. Either use the current script on this page and modify it to take the second NIC or use OSCustomizationSpec with OSCustomizationNicMapping. I have a post about this, http://notesofascripter.com/2017/02/07/building-vms-using-oscustomizationspec/, and it is fairly simple to do. Below is an example of how it should look for configuring multiple IPs on a single VM.

      Get-OSCustomizationNicMapping -OSCustomizationSpec $OSCustomization | Set-OSCustomizationNicMapping -Position 1 -IpMode UseStaticIP -IpAddress $IP_NetworkAdapter1 -SubnetMask $SubnetMask_NetworkAdapter1 -DefaultGateway $Gateway_NetworkAdapter1 -Dns $DNS1_NetworkAdapter1,$DNS2_NetworkAdapter1 -Confirm:$false -ErrorAction Stop
      New-OSCustomizationNicMapping -Position 2 -IpMode UseStaticIP -IpAddress $IP_NetworkAdapter2 -SubnetMask $SubnetMask_NetworkAdapter2 -DefaultGateway $Gateway_NetworkAdapter2 -Dns $DNS1_NetworkAdapter2,$DNS2_NetworkAdapter2 -OSCustomizationSpec $OSCustomization -Confirm:$false -ErrorAction Stop
      

Leave a Reply

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.