-1

構文:New-VirtualSwitch [-VMHost] [-Name] [[-NumPorts]] [[-Nic] String []]

上記のコマンドレットを使用して、vSphereに新しいvSwitchを追加しています。知識が不足しているのは、4つのNICを表す4つのチェックボックスを使用する方法であり、チェックすると-Nicパラメーターに渡されます。

たとえば、以下は機能しません

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

構文が示すように、各チェックボックスの変数を文字列配列に渡すにはどうすればよいですか?

$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
    }
}
4

1 に答える 1