VMware Cloud Community
livinma
Contributor
Contributor
Jump to solution

Creating multiple VMs with 2 NICs

Hey guys -

Been researching a few difference scripts but haven't found the one that will help me in my situation. 

I am looking for a script that will allow me to clone from template and apply networking settings to 2 nics.

1. Clone 20+ Vms from Template

2. Allow me to send it to a particular lun (using a few luns to provision to)

3  choose a single custom spec

4. Apply IP's to 2 nic cards (Primary & Backup).

Any help would be appreciated regards.

Thanks

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sorry for loosing this thread out of my sight.

The following is an attempt at fulfilling all the requirements you listed.

$numberOfVM = 20
$baseVMName = "VM"
$templateName = "Template"
$osCustName = "MyCust"
$IPBase1 = "192.168.1."
$IPBase2 = "192.168.2."
$IPMask = "255.255.255.0"
$IPGate1 = "192.168.1.254"
$IPGate2 = "192.168.2.254"
$IPDns1 = "192.168.1.100"
$IPDns2 = "192.168.2.100"

# Target resourcepool
$resPool = Get-ResourcePool -Name Resources

# Get the template
$template = Get-Template -Name $templateName

# Create a copy of the OS Customisation Spec
Try {
   
Get-OSCustomizationSpec -Name tempOSCust |
   
Remove-OSCustomizationSpec -Confirm:$false -ErrorAction Stop
}
Catch {}

$osCust = Get-OSCustomizationSpec -Name $osCustName |
   
New-OSCustomizationSpec -Name tempOSCust -Type NonPersistent

# Clone the number of requested VM
1..$numberOfVm | %{
   
# Find the datastore with the most free space
    $ds = Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending |
       
Select -First 1
   
   
# Update the OS Customisation Spec NIC parts with the IP addresses
    Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust |
   
Set-OSCustomizationNicMapping -Position 1 -IpMode UseStaticIP -IpAddress "$IPBase1$_" `
       
-SubnetMask $IPMask -DefaultGateway $IPGate1 -Dns $IPDns1 -ErrorAction SilentlyContinue | Out-Null

   
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust |
   
Set-OSCustomizationNicMapping -Position 2 -IpMode UseStaticIP -IpAddress "$IPBase2$_" `
       
-SubnetMask $IPMask -DefaultGateway $IPGate2 -Dns $IPDns2 -ErrorAction SilentlyContinue | Out-Null
   
   
# Create the new VM
    New-VM -Name "$baseVMName$_" -Template $template -OSCustomizationSpec $osCust `
       
-Datastore $ds -ResourcePool $resPool  | Out-Null
}

The script will create $numberOfVM clones of the template. The VMs will be created in the root of the cluster, and they will be named VM1, VM2....

The OSCustomizationSpec needs to exist, and needs to have 2 NICs.

The script looks for the datastore with most free space to create each VM.

The NIC IP addresses are consecutive, for example on NIC1 you will get 192.168.1.1, 192.168.1.2... and so on.

Let me know if you have any questions.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

17 Replies
LucD
Leadership
Leadership
Jump to solution

A question about point 2, how do you want to select the datastore ?

Based on the most free space ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
livinma
Contributor
Contributor
Jump to solution

Good point LucD-

I was actually adding 4x 2TB Luns so that is less of an issue. But it would be a cool add in but not necessary.

Thanks

0 Kudos
livinma
Contributor
Contributor
Jump to solution

Any suggestions ? @ one point I had a script that pretty much did all of the above except it didn't have the ability to add IPs to the second network interface. Unfortunately many company refreshes later my script repository is gone. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry for loosing this thread out of my sight.

The following is an attempt at fulfilling all the requirements you listed.

$numberOfVM = 20
$baseVMName = "VM"
$templateName = "Template"
$osCustName = "MyCust"
$IPBase1 = "192.168.1."
$IPBase2 = "192.168.2."
$IPMask = "255.255.255.0"
$IPGate1 = "192.168.1.254"
$IPGate2 = "192.168.2.254"
$IPDns1 = "192.168.1.100"
$IPDns2 = "192.168.2.100"

# Target resourcepool
$resPool = Get-ResourcePool -Name Resources

# Get the template
$template = Get-Template -Name $templateName

# Create a copy of the OS Customisation Spec
Try {
   
Get-OSCustomizationSpec -Name tempOSCust |
   
Remove-OSCustomizationSpec -Confirm:$false -ErrorAction Stop
}
Catch {}

$osCust = Get-OSCustomizationSpec -Name $osCustName |
   
New-OSCustomizationSpec -Name tempOSCust -Type NonPersistent

# Clone the number of requested VM
1..$numberOfVm | %{
   
# Find the datastore with the most free space
    $ds = Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending |
       
Select -First 1
   
   
# Update the OS Customisation Spec NIC parts with the IP addresses
    Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust |
   
Set-OSCustomizationNicMapping -Position 1 -IpMode UseStaticIP -IpAddress "$IPBase1$_" `
       
-SubnetMask $IPMask -DefaultGateway $IPGate1 -Dns $IPDns1 -ErrorAction SilentlyContinue | Out-Null

   
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust |
   
Set-OSCustomizationNicMapping -Position 2 -IpMode UseStaticIP -IpAddress "$IPBase2$_" `
       
-SubnetMask $IPMask -DefaultGateway $IPGate2 -Dns $IPDns2 -ErrorAction SilentlyContinue | Out-Null
   
   
# Create the new VM
    New-VM -Name "$baseVMName$_" -Template $template -OSCustomizationSpec $osCust `
       
-Datastore $ds -ResourcePool $resPool  | Out-Null
}

The script will create $numberOfVM clones of the template. The VMs will be created in the root of the cluster, and they will be named VM1, VM2....

The OSCustomizationSpec needs to exist, and needs to have 2 NICs.

The script looks for the datastore with most free space to create each VM.

The NIC IP addresses are consecutive, for example on NIC1 you will get 192.168.1.1, 192.168.1.2... and so on.

Let me know if you have any questions.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

livinma
Contributor
Contributor
Jump to solution

Hey Luc -

No worries -- Your a busy man all day all night I can see by your posts. I truly appreciate this post. I will give this a shot tonight when I return home.

Regards & Thanks a bunch.

0 Kudos
livinma
Contributor
Contributor
Jump to solution

Hey Luc -

Tried it during the weekend with no luck and was MIA the last few days. I am receiving the below.

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:54 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:54 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:54 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:54 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

Set-OSCustomizationNicMapping : 11/6/2013 12:34:55 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:37 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 1 -IpMode UseStaticIP -IpAd

dress "$IPBase1$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

Set-OSCustomizationNicMapping : 11/6/2013 12:34:56 PMSet-OSCustomizationNic
Mapping    The parameter 'IpAddress' is invalid.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:41 char:34

+ Set-OSCustomizationNicMapping <<<<  -Position 2 -IpMode UseStaticIP -IpAd

dress "$IPBase2$_" `

+ CategoryInfo      : InvalidArgument: (:) [Set-OSCustomizationNicMapp

   ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_Inva

   lidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMa

  pping

New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViC

ore.Types.V1.Inventory.VIContainer' required by parameter 'ResourcePool'. Speci

fied method is not supported.

At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\server

deploy.ps1:46 char:37

+     -Datastore $ds -ResourcePool <<<<  $resPool  | Out-Null
+ CategoryInfo      : InvalidArgument: (:) [New-VM], ParameterBindingE

   xception

+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewVM

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts>error.jpg

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks like "Get-ResourcePool -Name Resources" is returning more than one object.

This happens when there is more than 1 cluster in your vCenter.

Make that more specific, for example

Get-Cluster -Name MyCluster | Get-ResourcePool -Name Resources

The 2nd type of error seems to be caused by an IP address that the cmdlet considers to be invalid.

What did you assign to the variable $IPBase1 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
livinma
Contributor
Contributor
Jump to solution

You are right in regards to the resourcepool. I adjusted it to target 1 cluster. I have gotten as far as getting the server to deploy but having an issue with sysprep. Continuing to troubleshoot now.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What exactly are the sysprep problems you are seeing ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
livinma
Contributor
Contributor
Jump to solution

Hey Luc  -

When I created this template I created it off a production based template with all tools already ran and configured. Which also included the changing of accounts locally. I believe the error I am receiving  is because of a password between the custom spec and the template are different. I am making changing now to test this.

sysprep.jpg

0 Kudos
livinma
Contributor
Contributor
Jump to solution

This is a slight modification to the above script you provided by my colleague. We are deploying but even after adjusting the template and the custom spec password to no avail. I am now removing the automatically login to identify if this is causing the error.

$ipend = 140

$baseVMName = "VMSERVER01"

$templateName = "NEWTEMPLATE"

$osCustName = "CUSTOMSPEC2K8R2"

$IPBase1 = "192.168.104."

$IPBase2 = "192.168.115."

$IPMask = "255.255.255.0"

$IPGate1 = "192.168.104.1"

#$IPGate2 = "1.1.1.1"

$IPDns1 = @("192.168.242.241","192.168.242.242")

#$IPDns2 = @("0.0.0.0","0.0.0.0")

$myds = @("VMW-3PAR1-TTC-LUN-7917-DATA","VMW-3PAR1-TTC-LUN-7918-DATA","VMW-3PAR1-TTC-LUN-7919-DATA","VMW-3PAR1-TTC-LUN-7920-DATA","VMW-3PAR1-TTC-LUN-7923-DATA","VMW-3PAR1-TTC-LUN-7924-DATA","VMW-3PAR1-TTC-LUN-7925-DATA","VMW-3PAR1-TTC-LUN-7926-DATA")

# Target resourcepool

$resPool = Get-Cluster -Name "CLUSTER 01"

# Get the template

$template = Get-Template -Name $templateName

# Create a copy of the OS Customisation Spec

Try {

    Get-OSCustomizationSpec -Name tempOSCust | Remove-OSCustomizationSpec -Confirm:$false -ErrorAction Stop

}

Catch {}

$osCust = Get-OSCustomizationSpec -Name $osCustName | New-OSCustomizationSpec -Name tempOSCust -Type NonPersistent

# On Each Datastore ...

foreach ($ds in $myds)

{

    $dsobj = Get-Datastore -Name $ds

    # Build 5 VMs ...

    1..5 | %{

        Write-Host $ds - $baseVMName$ipend

      

        # Update the OS Customisation Spec NIC parts with the IP addresses

        Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | `

            Set-OSCustomizationNicMapping -Position 1 -IpMode UseStaticIP -IpAddress "$IPBase1$ipend" `

            -SubnetMask $IPMask -DefaultGateway $IPGate1 -Dns $IPDns1 -ErrorAction SilentlyContinue | Out-Null

          

        Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | `

           Set-OSCustomizationNicMapping -Position 2 -IpMode UseStaticIP -IpAddress "$IPBase2$ipend" `

            -SubnetMask $IPMask -DefaultGateway $IPGate2 -Dns $IPDns2 -ErrorAction SilentlyContinue | Out-Null

  

        Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | `

            Set-OSCustomizationNicMapping -Position 2 -IpMode UseDHCP `

            -ErrorAction SilentlyContinue | Out-Null  

  

          

        # Create the new VM

        New-VM -Name "$baseVMName$ipend" -Template $template -OSCustomizationSpec $osCust -Datastore $dsobj -ResourcePool $resPool | Out-Null

      

        # Power it on

        Start-VM "$baseVMName$ipend" | Out-Null

       

        $i++

    }

}

sysprep2.jpg

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is it possible to attach the unattend.xml file that was generated ?

Make sure to remove any sensitive information first (password, windows key...)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
livinma
Contributor
Contributor
Jump to solution

okay working on it now..

Umm unfortunately for some reason the copy between my service console and desktop isn't working. I enabled the policy in the advanced vm settings. I will have to figure out another method of getting to you. Probably mount the vmdk file has a disk drive.

0 Kudos
livinma
Contributor
Contributor
Jump to solution

Ok Luc -

I tried every which way from Sunday to try and extract this file but no luck. I ended up doing snapshots. I hope this can help.

custom01.jpg

custom02.jpg

custom03.jpg

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm confused by the AutoLogon part, according to the value of PlainText, the password should be in plain text (doh).

But the value I see in Value seems to be an encrypted one.

Can you try the deployment without the Autologon part ?

And see if you still get the error in that case ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

livinma
Contributor
Contributor
Jump to solution

LUC -

I also did find that look weird so after you acknowledge it we did more research and found the below article.

https://communities.vmware.com/thread/297748

Thanks for pointing us down the right rabbit hole on this one. We were able to deploy without incident. Now going to try to do the 40 vms and get back to you in a moment.

0 Kudos
livinma
Contributor
Contributor
Jump to solution

Seems like when the credentials were passed during the customspec clone/copy (what we gathered from the link above) they were passed improperly. So we made some alterations to the code. I am adding the completed product. Big thanks to you Luc for helping us work thru this.

$ipend = 140

$baseVMName = "ServerName"

$templateName = "Template"

$osCustName = "Customization Spec"

$IPBase1 = "192.168.1."

$IPBase2 = "192.168.2."

$IPMask = "255.255.255.0"

$IPGate1 = "192.168.1.1"

$IPGate2 = "1.1.1.1"

$IPDns1 = @("192.168.1.99","192.168.1.100")

$IPDns2 = @("0.0.0.0","0.0.0.0")

$myds = @("DATASTORE1","DATASTORE2","DATASTORE3","DATASTORE4")

# Target resourcepool

$resPool = Get-Cluster -Name "LUCDISTHEBEST"

# Get the template

$template = Get-Template -Name $templateName

$csmSpecMgr = Get-View 'CustomizationSpecManager'

# On Each Datastore ...

foreach ($ds in $myds)

{

    $dsobj = Get-Datastore -Name $ds

    # Build 5 VMs ...

    1..5 | %{

        Write-Host $ds - $baseVMName$ipend

     

        if($csmSpecMgr.DoesCustomizationSpecExist("$baseVMName$ipend"))

            {

            Get-OSCustomizationSpec -Name $baseVMName$ipend | Remove-OSCustomizationSpec -Confirm:$false -ErrorAction Stop

            }

     

        $csmSpecMgr.DuplicateCustomizationSpec("$osCustName","$baseVMName$ipend")

        $osCust = (Get-OSCustomizationSpec -Name "$baseVMName$ipend" -Type Persistent)

             

        # Update the OS Customisation Spec NIC parts with the IP addresses

        $osCust | Get-OSCustomizationNicMapping | where {$_.Position -eq 1} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IPBase1$ipend -SubnetMask $IPMask -DefaultGateway $IPGate1 -Dns $IPDns1

        $osCust | Get-OSCustomizationNicMapping | where {$_.Position -eq 2} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IPBase2$ipend -SubnetMask $IPMask -DefaultGateway $IPGate2 -Dns $IPDns2

     

     

        $osCust = (Get-OSCustomizationSpec -Name "$baseVMName$ipend" -Type Persistent)

        $osCust.ExtensionData.Spec.NicSettingMap[1].Adapter.dnsServerList = $null

        $osCust.ExtensionData.Spec.NicSettingMap[1].Adapter.Gateway = $null

     

        $csmSpecMgr.OverwriteCustomizationSpec($osCust.ExtensionData)

     

        # Create the new VM

        New-VM -Name "$baseVMName$ipend" -Template $template -OSCustomizationSpec $osCust -Datastore $dsobj -ResourcePool $resPool

     

        Remove-OSCustomizationSpec -CustomizationSpec $osCust -Confirm:$false

     

        # Power it on

        Start-VM "$baseVMName$ipend"

      

        $ipend++

    }

}

0 Kudos