I'm creating a script to create the networking for a new host.
$NewHost = Read-Host "Please enter the full DNS name of the new VMware host"
$VMHost = Get-VMHost -Name $NewHost
Add a Virtual Switch and Port Group
$vs = New-VirtualSwitch -VMHost $VMHost -Name vSwitch1
$vpg = New-VirtualPortGroup -VirtualSwitch $vs -Name 'Internal LAN'
Add vmnic2 and vmnic3 to the Switch
$net = Get-VMHostNetwork -VMHost $VMHost
$NICs = $net.physicalnic
foreach ($NIC in $NICs){
switch -wildcard ($NIC.DeviceName) {
"vmnic0" {"vmnic0.";break}
"vmnic1" {"vmnic1.";break}
"vmnic2" {Set-VirtualSwitch -VirtualSwitch $vs -Nic $NIC;break}
"vmnic3" {Set-VirtualSwitch -VirtualSwitch $vs -Nic $NIC;break}
default {"The NIC could not be determined."}
}
}
Creating the switch and port groups work fine, but when I try to add vmnic2 and 3 to the switch I get:
Set-VirtualSwitch : 13/03/2009 09:17:16 Set-VirtualSwitch 7D1D0E94-9F79-47AA-A289-C8FB3D3D823D The object or item referred to could not be found
Can anyone see what I need to change? Thanks
The should be $NIC.devicename instead of $NIC.
$NewHost = Read-Host "Please enter the full DNS name of the new VMware host" $VMHost = Get-VMHost -Name $NewHost $vs = New-VirtualSwitch -VMHost $VMHost -Name vSwitch1 $vpg = New-VirtualPortGroup -VirtualSwitch $vs -Name 'Internal LAN' $net = Get-VMHostNetwork -VMHost $VMHost $NICs = $net.physicalnic foreach ($NIC in $NICs){ switch -wildcard ($NIC.DeviceName) { "vmnic0" {"vmnic0.";break} "vmnic1" {"vmnic1.";break} "vmnic2" {Set-VirtualSwitch -VirtualSwitch $vs -Nic $NIC.DeviceName;break} "vmnic3" {Set-VirtualSwitch -VirtualSwitch $vs -Nic $NIC.DeviceName;break} default {"The NIC could not be determined."} } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The should be $NIC.devicename instead of $NIC.
$NewHost = Read-Host "Please enter the full DNS name of the new VMware host" $VMHost = Get-VMHost -Name $NewHost $vs = New-VirtualSwitch -VMHost $VMHost -Name vSwitch1 $vpg = New-VirtualPortGroup -VirtualSwitch $vs -Name 'Internal LAN' $net = Get-VMHostNetwork -VMHost $VMHost $NICs = $net.physicalnic foreach ($NIC in $NICs){ switch -wildcard ($NIC.DeviceName) { "vmnic0" {"vmnic0.";break} "vmnic1" {"vmnic1.";break} "vmnic2" {Set-VirtualSwitch -VirtualSwitch $vs -Nic $NIC.DeviceName;break} "vmnic3" {Set-VirtualSwitch -VirtualSwitch $vs -Nic $NIC.DeviceName;break} default {"The NIC could not be determined."} } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Brilliant, thank you very much!