-1

Syntax: New-VirtualSwitch [-VMHost] [-Name] [[-NumPorts] ] [[-Nic] String[]]

I am using the above cmdlet to add a new vSwitch in vSphere, where my knowledge lacks is how to use four checkbox that represent four NICs and when checked are passed to the -Nic parameter.

For instance the below wouldn't work

New-VirtualSwitch -VMHost $comboBox611.SelectedItem.toString() -Name $textBox611.Text -NumPorts 200 **-Nic $nic0,$nic1,$nic2,$nic3** -Mtu $textBox612.Text -Confirm

How do I pass the variables of each checkbox to a string array as the syntax shows it can be done?

$handler_linkLabel601_LinkClicked= 
{
if ($networkdataGridView.CurrentRow.Cells['VM Host'].Value.toString() -gt " ")
{
Add-Type -AssemblyName System.Windows.Forms
$form601 = New-Object Windows.Forms.Form
$form601.Size = New-Object Drawing.Size (250,270)
$form601.StartPosition = "CenterScreen"

$label611.Size = New-Object Drawing.Size (70,40)
$label611.Location = New-Object System.Drawing.Size (10,15)
$label611.Text = "vSwitch Name:"

$textBox611.Size = New-Object Drawing.Size (100,30)
$textBox611.Location = New-Object System.Drawing.Size (90,15)
$textBox611.Name = "vSwitch Name"

$label612.Size = New-Object Drawing.Size (50,20)
$label612.Location = New-Object System.Drawing.Size (10,55)
$label612.Text = "Host:"

$vmhostlist = Get-VMHost
    foreach ($vmhost in $vmhostlist)
    {
        $comboBox611.Items.add($vmhost.name.toString())
    }

$comboBox611.Size = New-Object Drawing.Size (100,20)
$comboBox611.Location = New-Object System.Drawing.Size (90,50)

$checkBox611.Size = New-Object Drawing.Size (20,20)
$checkBox611.Location = New-Object System.Drawing.Size (100,80)
# Add Click-Event
$checkBox611.Add_CheckStateChanged({
    If ($checkBox611.Checked) {
        $global:nic0 = "vmnic0"
    } Else {
         $global:nic0 = ""
    }
})
$checkBox612.Size = New-Object Drawing.Size (20,20)
$checkBox612.Location = New-Object System.Drawing.Size (170,80)
$checkBox611.Add_CheckStateChanged({
    If ($checkBox611.Checked) {
         $global:nic1 = "vmnic1"
    } Else {
         $global:nic1 = ""
    }
})
$checkBox613.Size = New-Object Drawing.Size (20,20)
$checkBox613.Location = New-Object System.Drawing.Size (100,100)
$checkBox611.Add_CheckStateChanged({
    If ($checkBox611.Checked) {
        $global:nic2 = "vmnic2"
    } Else {
         $global:nic2 = ""
    }
})
$checkBox614.Size = New-Object Drawing.Size (20,20)
$checkBox614.Location = New-Object System.Drawing.Size (170,100)
$checkBox611.Add_CheckStateChanged({
    If ($checkBox611.Checked) {
         $global:nic3 = "vmnic3"
    } Else {
         $global:nic3 = ""
    }
})
$label613.Size = New-Object Drawing.Size (80,20)
$label613.Location = New-Object System.Drawing.Size (10,140)
$label613.Text = "MTU Size:"

$textBox612.Size = New-Object Drawing.Size (100,20)
$textBox612.Location = New-Object System.Drawing.Size (90,140)
$textBox612.Name = "MTU"

$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object Drawing.Size (90,30)
$button.Location = New-Object System.Drawing.Size (70,200)
$selectedvmhost = ($comboBox611.SelectedItem.toString())
$button.add_click({test})
$button.Text = "Add New vSwitch"
$form601.Controls.Add($button)
$form601.Controls.Add($textBox611)
$form601.Controls.Add($textBox612)
$form601.Controls.Add($label611)
$form601.Controls.Add($label612)
$form601.Controls.Add($label613)
$form601.Controls.Add($comboBox611)
$form601.Controls.Add($checkBox611)
$form601.Controls.Add($checkBox612)
$form601.Controls.Add($checkBox613)
$form601.Controls.Add($checkBox614)
$form601.ShowDialog()
}
ELSE
{}
}
function test
{
$nic = @($global:nic0,$global:nic1,$global:nic2,$global:nic3 | ? {-not [string]::IsNullOrEmpty($_)})
 if ( $nic.count )
    {
        New-VirtualSwitch -VMHost $comboBox611.SelectedItem.toString() -Name $textBox611.Text -NumPorts 200 -Nic $nic -Mtu $textBox612.Text -Confirm
    }
}
Shamza
  • 13
  • 3
  • 8

1 Answers1

0

just as C.B. said replace $nic0 with $global:nic0, and $nic1 with $global:nic1 etc. then test is

function test
{
    $nic = @($global:nic0,$global:nic1,$global:nic2,$global:nic3 | ? {-not [string]::IsNullOrEmpty($_)})
    if ( $nic.count )
    {
        New-VirtualSwitch -VMHost $comboBox611.SelectedItem.toString() -Name $textBox611.Text -NumPorts 200 -Nic $nic -Mtu $textBox612.Text -Confirm
    }
}
Jackie
  • 2,476
  • 17
  • 20
  • Getting this 'New-VirtualSwitch : Cannot validate argument on parameter 'Nic'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again.' – Shamza Mar 08 '13 at 15:33
  • I do not find any problem, add `write-host $global:nic0` to test() to see what happened. – Jackie Mar 15 '13 at 12:58
  • It prints vmnic0 fine, but it only reading that variable by the looks of it. If I check another box the button does nothing, the command doesn't seem to run – Shamza Mar 21 '13 at 17:52