Tuesday, April 12, 2011

Deploying VMs with multiple interfaces through PowerCLI

The easiest way of deploying a large amount of VMs quickly is with a PowerCLI script. You write a script that modifies a guest customization, then you deploy from that customization. But how do you configure the IP addresses on the deployed VM?

Get-OSCustomizationNicMapping -spec myCustomization | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 192.168.0.100 -SubnetMask 255.255.255.0 -DefaultGateway 192.168.0.1 -Dns 192.168.0.150,192.168.0.151

Get-OSCustomizationNicMapping -spec myCustomization | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 192.168.0.100 -SubnetMask 255.255.255.0 -DefaultGateway 192.168.0.1 -Dns 192.168.0.150,192.168.0.151

Simple enough, but what is the server has multiple interfaces? Look at the output above. See the ‘Position’ column? ‘1’ means that the first network adapter is being modified. You can change different network adapters by modifying the Position attribute in the Get-OSCustomizationNicMapping command.

Get-OSCustomizationNicMapping -spec myCustomization | where { $_.Position -eq '1'} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $192.168.0.100 -SubnetMask 255.255.255.0 -DefaultGateway 192.168.0.1 -Dns 192.168.0.1,192.168.0.1

Get-OSCustomizationNicMapping -spec myCustomization | where { $_.Position -eq '1'} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $192.168.0.100 -SubnetMask 255.255.255.0 -DefaultGateway 192.168.0.1 -Dns 192.168.0.1,192.168.0.1

Notice how the output is the same. We can modify the second adapter by changing the Position attribute to 2.

Get-OSCustomizationNicMapping -spec myCustomization | where { $_.Position -eq '2'} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $192.168.0.100 -SubnetMask 255.255.255.0 -DefaultGateway 192.168.0.1 -Dns 192.168.0.1,192.168.0.1

Get-OSCustomizationNicMapping -spec myCustomization | where { $_.Position -eq '2'} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $192.168.0.100 -SubnetMask 255.255.255.0 -DefaultGateway 192.168.0.1 -Dns 192.168.0.1,192.168.0.1

All good. But, you should only have a single default gateway on a server. When we try to execute the command without the –DefaultGateway argument, we get an error message.

Set-OSCustomizationNicMapping : Missing an argument for parameter 'DefaultGateway'. Specify a parameter of type 'System.String' and try again.

Set-OSCustomizationNicMapping : Missing an argument for parameter 'DefaultGateway'. Specify a parameter of type 'System.String' and try again.
At line:1 char:204
+ Get-OSCustomizationNicMapping -spec myCustomization | where { $_.Position -eq '2'} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 192.168.200.100 -SubnetMask 255.255.255.0 -DefaultGateway <<<<  -Dns 192.168.0.150,192.168.0.151
+ CategoryInfo : InvalidArgument: (:) [Set-OSCustomizationNicMapping], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMapping

The Set-OSCustomizationNicMapping command has a bug that forces you to have a default gateway! Boo hiss! How do we get around this? Set the OSCustomizationNicMapping so both interfaces have default gateways. Then, use a RunOnce command to remove the default gateway you don’t want. In the Guest Customization settings, go to Run Once and add the command

netsh interface ip delete address “Local Area Connection” address=192.168.0.1 gateway=all

image

This command will delete the default gateway 192.168.0.1 on the interface “Local Area Connection” (the first network adapter). Obviously if you want to delete the default gateway on the second adapter, change this to “Local Area Connection 2”.

I hope this helps you. In the meantime, I will file a bug report with VMware and see if it’s fixed in VMware 5.

4 comments:

  1. Have you tested this with Windows 2003? We got to the same endpoint with 2008, but couldnt get it to work with 2003. Found this post when googling for an answer. The RunOnce stuff doesnt seem quite as stable with 2003.

    ReplyDelete
  2. You can also use something like this :
    $csmSpecMgr = Get-View 'CustomizationSpecManager'
    $spec = Get-OSCustomizationSpec $projectName
    $spec.ExtensionData.Spec.NicSettingMap[1].Adapter.Gateway = ""
    $csmSpecMgr.OverwriteCustomizationSpec($spec.ExtensionData)

    Assuming that the first adapter has the gateway and the second must be fixed

    ReplyDelete