语法: New-VirtualSwitch [-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 :(得分:0)
正如C.B.所说用$ global替换$ nic0:nic0,用$ global替换$ 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
}
}