VMware Cloud Community
jmedd
Enthusiast
Enthusiast
Jump to solution

Add network cards to switch

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

  1. Add a Virtual Switch and Port Group

$vs = New-VirtualSwitch -VMHost $VMHost -Name vSwitch1

$vpg = New-VirtualPortGroup -VirtualSwitch $vs -Name 'Internal LAN'

  1. 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

Blog: http://jonathanmedd.net | Twitter: @jonathanmedd
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jmedd
Enthusiast
Enthusiast
Jump to solution

Brilliant, thank you very much!

Blog: http://jonathanmedd.net | Twitter: @jonathanmedd
Reply
0 Kudos