VMware Cloud Community
toobulkeh
Contributor
Contributor
Jump to solution

NetworkName variable under New-VM Cmdlet in PowerCLI

  Write-Host "Creating Clone VM: " $vm.vm_name
  $networks = @($vm.vn0_pg,$vm.vn1_pg)
  $tmpVM = New-VM -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $ds -Location $vm.folder -NetworkName $networks

For the above code, I'm trying to set the Network Adapters when cloning a VM. There are 2 adapters in this case. Here is the published document online:

http://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/New-VM.html

From that site:

NetworkNameString[]Specify the networks to which you want to connect the new virtual machine.falsefalse

which leads me to believe in PowerCLI it is an Array of Strings, but this throws the error:

Parameter set cannot be resolved using the specified named parameters.

At :line:78 char:17

+   $tmpVM = New-VM <<<<  -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $ds -Location $vm.folder -NetworkName $networks

Thoughts? Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, perhaps my explanantion wasn't too clear 🙂

This is your line

New-VM -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $ds -Location $vm.folder -NetworkName $networks

Let's take the parameters one by one and indicate to which parameterset they belong.

VMHost:               all parametersets (CloneVm,DefaultParameterSet,Template,RegisterVm)

VM:                      cloneVM

Name:                  all parametersets (CloneVm,DefaultParameterSet,Template,RegisterVm)

Datastore:            CloneVM,Template,DefaultParameterSet

Location:              all parametersets (CloneVm,DefaultParameterSet,Template,RegisterVm)

NetworkName:     DefaultParameterSet

You notice that the VM and the NetworkName parameter do not have a common parameterset.

That means you can't use these together.

Try leaving out the VM parameter, the command will work as well.

If you want to clone a VM, use the New-VM without the NetworkName parameter.

Once the new VM is created, use the Get- and Set-NetworkAdapter cmdlets to configure the networks for the NICs.


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

This is a parameterset problem.

The New-VM cmdlet has 4 of those.

The -VM parameter belongs to the CloneVM parameterset, while all the others belong to the DefaultParameterSet.

You can't mix parameters that are exclusive to a specific parameterset.

My PowerCLI cmdlet XRef – Another look post contains a script that allows you to produce a handy list of all cmdlets with all their parametersets.


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

0 Kudos
toobulkeh
Contributor
Contributor
Jump to solution

LucD,

I'm no powershell nor powercli guru, so I'm not sure what half those terms mean. But the command I mentioned above does work without the NetworkName command (including all those other commands) so I'm pretty sure mixing those parametersets works just fine. Again I may be wrong, but what you suggested doesn't appear to be happening.

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, perhaps my explanantion wasn't too clear 🙂

This is your line

New-VM -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $ds -Location $vm.folder -NetworkName $networks

Let's take the parameters one by one and indicate to which parameterset they belong.

VMHost:               all parametersets (CloneVm,DefaultParameterSet,Template,RegisterVm)

VM:                      cloneVM

Name:                  all parametersets (CloneVm,DefaultParameterSet,Template,RegisterVm)

Datastore:            CloneVM,Template,DefaultParameterSet

Location:              all parametersets (CloneVm,DefaultParameterSet,Template,RegisterVm)

NetworkName:     DefaultParameterSet

You notice that the VM and the NetworkName parameter do not have a common parameterset.

That means you can't use these together.

Try leaving out the VM parameter, the command will work as well.

If you want to clone a VM, use the New-VM without the NetworkName parameter.

Once the new VM is created, use the Get- and Set-NetworkAdapter cmdlets to configure the networks for the NICs.


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

0 Kudos
toobulkeh
Contributor
Contributor
Jump to solution

Ah that is much more clear! Thanks for identifying that difference. Where did you pull that table? I do not see it in the following documentation page:

http://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/New-VM.html

Unfortunately, I am alreadying cloning the VMs like so:


  $tmpVM = New-VM -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $ds -Location $vm.folder
  write-host "Created clone VM: " $vm.vm_name
  $nics = Get-NetworkAdapter -VM $tmpVM
  if($vm.vn0_pg -ne "") {
      Set-NetworkAdapter $nics[0] -NetworkName $vm.vn0_pg -Confirm:$false
  }
  if($vm.vn1_pg -ne "") {
      Set-NetworkAdapter $nics[1] -NetworkName $vm.vn1_pg -Confirm:$false
  }
  Write-Host "Changed vNIC Portgroups"

I was just looking for a method to speed up the clone time as I'm generating a few thousand of these VMs Smiley Happy

You might also understand the problem I was having with Datastore cloning... to save some internet space, I'll just ask here:

I have one vCenter server with multiple Datacenters. Is there a way to change the "scope" of the following command:

New-VM -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $vm.datastore -Location $vm.folder

I ask because I have two different datastores (LUNs/paths) with the same name, but in separate Datacenters. This causes the above command to retreive multiple "values" for the Datastore variable and report an error like:

3/28/2011 12:29:00 PM    New-VM        The specified parameter 'Datastore' expects a single value, but your name criteria 't01-vms' corresponds to multiple values.   
At :line:75 char:17
+   $tmpVM = New-VM <<<<  -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $vm.datastore -Location $vm.folder

I feel that changing the scope of the New-VM command, if at all possible, to the Datacenter of my choosing would be much faster than doing the following:

Write-Host "Getting Datastore: " $vm.datastore
   $ds = Get-Datastore -Name $vm.datastore -Datacenter $dc
   Write-Host "Creating Clone VM: " $vm.vm_name
   $networks = @($vm.vn0_pg,$vm.vn1_pg)
   $tmpVM = New-VM -VMHost $vm.esx -VM $sv -Name $vm.vm_name -Datastore $ds -Location $vm.folder

Thanks for the help!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I don't think you can scope the New-VM cmdlet to choose a specific datastore if you pass multiple.

The best solution, afaik, is what you already have

Get-Datastore -Name $vm.datastore -Datacenter $dc

The best solution is of course not to use datastores with the same name on the same vCenter.

The table I used is created by the script in my blogpost I mentioned earlier.


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