I haven't found anything on the Internet for this one.
I have a script that clones a VM from a template and successfully sets various hardware, but it fails setting the network/vlan.
The VLAN does exist and I can set it through the v4.0 U1 Virtual Center client.
Also, it works against a v3.5 U5 Virtual Center client and hosts. (It's the environment where I developed the script.)
Running just " Get-NetworkAdapter -VM vmname" works.
I find it interesting that get-networkadapter works alone but when combined with set-network adapter it reports and error.
Any help is much appreciated.
===========================================================================
Z:\ops\posh\VMware> Get-NetworkAdapter -VM vmname | Set-NetworkAdapter -NetworkName vlanname -Confirm:$false
Get-NetworkAdapter : 4/6/2010 5:00:23 PM Get-NetworkAdapter 237ED357-9DEC-4886-A3EB-D6C9BCF3A1F2 Error occured while executing cmdlet: Get-NetworkAdapter. Check inner exception for more details.
At line:1 char:19
+ Get-NetworkAdapter <<<< -VM vmname | Set-NetworkAdapter -NetworkName vlanname -Confirm:$false
+ CategoryInfo : NotSpecified: ( , VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.Commands.GetNetworkAdapter
Set-NetworkAdapter : 4/6/2010 5:00:23 PM Set-NetworkAdapter The network "vlanname" doesn't exist on the host.
At line:1 char:59
+ Get-NetworkAdapter -VM vmname | Set-NetworkAdapter <<<< -NetworkName vlanname -Confirm:$false
+ CategoryInfo : ResourceUnavailable: (vlanname:String) , ViError
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_TryGetHostNetworkByName_NonexistentNetwork,VMware.VimAutomation.Commands.SetNetworkAdapter
===========================================================================
Hi,
These errors are coming because of invalid parameter NetworkName in Set-NetworkAdapter. If you provide a valid network name available on host, it will work.
For example:
$n = Get-NetworkAdapter -VM vm |Select-Object NetworkName Set-NetworkAdapter -NetworkAdapter (Get-NetworkAdapter -VM vm) -NetworkName $n.NetworkName -Confirm:$false
Hope this helps.
The portgroup you specified in vlanname apparently isn't defined on the host where your VM is created.
Try this to check if the portgroup exists
$vmName = <vm-name> $vlanName = <vlan-name> $portgroups = @() $vmImpl = Get-VM $vmName Get-VMHost -Name $vmImpl.Host.Name | Get-VirtualPortGroup | %{$portgroups += $_.Name} if($portgroups -notcontains $vlanName){ Write-Host "The portgroup" $vlanName "isn't defined on" $vmImpl.Host.Name Write-Host "Valid portgroups are" $portgroups } else{ Write-Host "The portgroup" $vlanname "exists on" $vmImpl.Host.Name }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I realized the problem and forgot to mention the key point in my original post. The guest is on a dvSwitch, and dvSwitches aren't supported by PowerCLI yet. After I started to suspect that this morning I found a post from who else but Luc pointing that out and with a solution. I'll be putting those functions into my script.
http://www.lucd.info/2010/03/04/dvswitch-scripting-part-8-get-and-set-network-adapters/
Thank you both for responding.
Hi,
In PowerCLI 4.1 we introduced partially vDS support that will cover your use case.
Now you can move the VM in to vDS by just passing vDS portgroup name as NetworkName in the Set-NetworkAdapter cmdlet:
Get-VM vm | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName "vdPortGroup"
You can learn more from the vDS in PowerCLI 4.1 article.
Regards,
Yasen Kalchev
PowerCLI Dev Team