2013-03-08 98 views
-1

語法: 新虛擬交換機[-VMHost] [-Name] [-NumPorts] [-nic]的String []]傳遞多個變量參數

我使用上面的cmdlet來在vSphere中添加一個新的vSwitch,我的知識缺乏的是如何使用代表四個NIC的四個複選框,並在選中時將其傳遞給-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 
    } 
} 
+0

在代碼'$ nic'一直沒有得到值.. – 2013-03-08 11:45:02

+0

忽略這一點,需要幫助將$ nic0 $ nic1 $ nic2 $ nic3移動到數組並傳遞給-Nic參數 – Shamza 2013-03-08 12:02:06

+0

嘗試將您的變量聲明爲$ global:nicX,並在您的第一個示例中調用。 – 2013-03-08 12:29:57

回答

0

就像CB表示,與全球$替換$ nic0:nic0,和$以$全球NIC1:NIC1等 然後測試

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 
    } 
} 
+0

獲取此'New-VirtualSwitch:無法驗證參數'Nic'上的參數。參數爲空,空或參數集合的元素包含空值。提供一個不包含任何空值的集合,然後再次嘗試 命令。' – Shamza 2013-03-08 15:33:18

+0

代碼更新,以檢查數組是否爲空 – Jackie 2013-03-10 07:01:30

+0

沒有運氣,沒有任何反應按鈕點擊所有 – Shamza 2013-03-10 17:45:27