I've been handed off a build environment, complete with scripts that are already written, that we use to build around 120 VM's to test some software.
I need to modify the build script so that when the VM is built, it's connected to the correct Network (NetworkName, Portgroup, Network Label, pick a name). Old scripts just put everything into the default network of VM Network, but now we want to test across multiple subnets.
It's a Vsphere cluster with 8 hosts. There are 128 Standard Port groups (VSS, Standard Switch)
We have a script that takes a CSV input (Previously only contained the VM's name, but I added a new column NetLabel
We build with a command like this:
$taskTab[(New-VM -VMHost (Get-VMHost $vhost) -Name $vm -Location $myFolder -Datastore (Get-Datastore $vhoststore) -Template $myTemplate -DiskStorageFormat Thin –OSCustomizationSpec (Get-OSCustomizationSpec $myOsCustomSpec) –RunAsync -Confirm:$False).Id] = $vm
My first go around was to try adding -NetworkName $NetLabel to the build command (No perceived errors shown)
Didn't work.
I modified the script to change the network after the VM was built (while it was still cloning)
$gVM = Get-VM $vm
$gVM | Get-NetworkAdapter | Set-NetworkAdapter -PortGroup $NetLabel -confirm:$false
That didn't work. (No errors shown)
Finally, I made a new script to change it and I would run it after the VM was built. (Essentially run the build script, without the build command) This threw errors of:
Set-NetworkAdapter : 2/28/2014 1:21:48 PM Set-NetworkAdapter Could no
t find VirtualPortGroupBase with name 'Network-01'.
At C:\Temp\VMSetup\powercli\PrimaryServerVMDeployment\UpdateNic.ps1:49 char:49
+ $gVM | Get-NetworkAdapter | Set-NetworkAdapter <<<< -PortGroup $NetL
abel -confirm:$false
+ CategoryInfo : ObjectNotFound: (Network-01:String) [Set-
NetworkAdapter], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNo
tFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetwo
rkAdapter
I'm a bit lost at this point. Network-01 exists.
As the error says, you are mixing 2 parametersets of the New-VM cmdlet.
You can't have Template and NetworkName together.
If you want to configure the network when you use the Template parameterset, you will have to do this via the OSCustomizationSpec parameter.
In the OSCustomizationSpec you can specify the network.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The value you pass to the Portgroup parameter should an the object that is returned by the Get-VirtualPortgroup cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So, I tried a couple of things.
I added the following:
$NetLabel = $vms[$vmi].NetLabel #Read from CSV File the Network Label
and in the build host I tried adding:
-NetworkName (Get-VirtualPortgroup -Name $NetLabel -VMHost $vhost)
I also attempted,
$NetLabel = Get-VirtualPortgroup -Name $vms[$vmi].NetLabel
and then call
-NetworkName $NetLabel.
The error I'm getting is:
New-VM : Parameter set cannot be resolved using the specified named parameters.
At C:\Temp\VMSetup\powercli\PiicVMDeploymet\DeployPiicVms.ps1:44 char:25
+ $taskTab[(New-VM <<<< -VMHost (Get-VMHost $vhost) -Name $vm -Locatio
n $myFolder -Datastore (Get-Datastore $vhoststore) -Template $myTemplate -DiskS
torageFormat Thin -NetworkName (Get-VirtualPortgroup -Name $NetLabel -VMHost $v
host) -OSCustomizationSpec (Get-OSCustomizationSpec $myOsCustomSpec) -RunAsync
-Confirm:$False).Id] = $vm
+ CategoryInfo : InvalidArgument: (:) [New-VM], ParameterBindingE
xception
+ FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCor
e.Cmdlets.Commands.NewVM
if I execute:
$NetLabel = $vms[$vmi].NetLabel
Get-VirtualPortgroup -Name $NetLabel
all by itself, i get back:
3KBed-Bedsides-02 key-vim.host.PortGroup-3KBe... 139
3KBed-Bedsides-02 key-vim.host.PortGroup-3KBe... 139
3KBed-Bedsides-02 key-vim.host.PortGroup-3KBe... 139
3KBed-Bedsides-02 key-vim.host.PortGroup-3KBe... 139
3KBed-Bedsides-02 key-vim.host.PortGroup-3KBe... 139
In case I misunderstood,
I also tried:
$NetLabel = Get-VirtualPortgroup -Name $vms[$vmi].NetLabel -VMHost $vhost
$gVM = Get-VM $vm
$gVM | Get-NetworkAdapter | Set-NetworkAdapter -PortGroup $NetLabel -confirm:$false
Which did not work either.
Probably silly to ask this, but I'll ask....
Did you specify the VirtualSwitch in which you are trying to get the Portgroup from? I remember having a hell of a time with dvSwitch portgroup retrieval without having to specify the VirtualSwitch in which I was trying to retrieve these from. Granted, dvSwitches probably react a little differently than VirtualSwitches, but might be worth a shot.
As the error says, you are mixing 2 parametersets of the New-VM cmdlet.
You can't have Template and NetworkName together.
If you want to configure the network when you use the Template parameterset, you will have to do this via the OSCustomizationSpec parameter.
In the OSCustomizationSpec you can specify the network.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference