VMware Cloud Community
derrick12
Contributor
Contributor

Set-VirtualSwitch parameter problem

Hi

I am having trouble understanding why something around the Set-VirtualSwitch does not work. Essentially what I am trying to do is correct a problem with my old deployment scripts which assign physical nics based on a cumulative assignment princple. In other words I assign physical nics to switches one at a time.

Now using Powershell v4.0.1 this behaviour has changed. In order to facilitate the removal of physical nics from switches the behaviour of the Set-Virtual Switch command now overwrites the existing config with the new one. In my case this means that only the last nic I have assigned stays configured, all others having been removed.

I understand the need for this but I am still stumped with what is more of a generic Powershell understanding issue than a PowerCLI issue.

As I read my config from from file and apply it to my hosts I build an array of objects that looks like this

[POSH] $pv

vSwitch                                           Nics
-------                                           ----
vSwitch0                                          {vmnic0, vmnic3}
vSwitch1                                          {vmnic1, vmnic2}

I then do a sneaky thing which I worked out all by myself (and thereby may be at the heart of my problems :

$vnics = ([string] $pv[1].Nics).Replace(" ", ",")

[POSH] $vnics

vmnic1,vmnic2

[POSH] $vnics.gettype()

IsPublic IsSerial Name                           BaseType
-------- -------- ----                           --------
True     True     String                         System.Object

Now I try to apply this string to the Set-VirtualSwith command :

[POSH] Set-VirtualSwitch -VirtualSwitch $sw -Nic $vnics
{color:red}Set-VirtualSwitch : 11/12/2009 3:10:22 p.m.    Set-VirtualSwitch    C002D1F8-8CB3-4DED-84B9-6FD777160FF9    
The object or item referred to could not be found.

At line:1 char:18
+ Set-VirtualSwitch <<<<  -VirtualSwitch $sw -Nic $vnics
    + CategoryInfo          : NotSpecified: (:) [Set-VirtualSwitch], ViError
    + FullyQualifiedErrorId : Client20_VirtualNetworkService_SetVirtualSwitch25_ViError,VMware.VimAutomation.Commands.SetVirtualSwitch{color}

When I try this using the values instead of the variable this works :

[POSH] Set-VirtualSwitch -VirtualSwitch $sw -Nic vmnic1,vmnic2

Name       Num Ports  Num Ports  Mtu   Key
                      Available
----       ---------  ---------- ---   ---
vSwitch1   128        125        1500  key-vim.host.VirtualSwitch-...

I am puzzled why this should work with the names directly and not a variable that seems in every way to be the same. Can someone possible help and shed some light on what I am missing?

Thanks

Derrick

0 Kudos
2 Replies
admin
Immortal
Immortal

Set-VirtualSwitch expects an array of strings for -Nic. If you enter X,Y on the command line, PowerShell casts this into an array of strings. If instead you enter "X,Y" (which is equivalent to what you do) PowerShell will interpret this as a single string of value X,Y.

Take advantage of the fact that the Nic property of the VirtualSwitch object is already an array. Try something like this. This adds vmnic3 to the already defined list.

Get-VirtualSwitch -name VM | Foreach { $_ | Set-VirtualSwitch -Nic ($_.Nic + "vmnic3") }

=====

Carter Shanklin

Read the PowerCLI Blog
[Follow me on Twitter|http://twitter.com/cshanklin]

0 Kudos
ggochkov
VMware Employee
VMware Employee

As Carter said the variable $vnics that you specified is a string while 'vmnic1, vmnic2' is converted to array of stringswhen powershell parses the arguments

and this is the reason the command with $vmnics does not work.

Just use:

&lt;code&gt;

$vnics = $pv[1].Nics

instead of

([string] $pv[1].Nics).Replace(" ", ",")

&lt;/code&gt;

0 Kudos