我有一个小型GUI,可以连接到列表中的vCenter。表单以我想要的顺序打开,但我不能在它们之间传递值。我已经尝试了很多回复电话,我不知道还能做什么。
这是代码。基本上,第一个表单打开并询问您想要的vCenter。然后提示您进行身份验证(这一切都非常有效)。但问题出现在脚本完成之后。我没有得到任何返回值。
function Connect-Vc ($vcArg)
{
$Form2 = $null
$selectedVC = $vcArg.selecteditem
$viconnection = $null
if ($global:defaultviserver -ne $null)
{
Disconnect-viserver * -confirm:$false
}
$clusterselection = $null
$DropDownTo = $null
$credential = Get-Credential -Message "Please Enter Elevated Credentials To Access $selectedVC. You can enter assuming you're an administrator on $selectedVC."
$username = $credential.Username
$password = $credential.GetNetworkCredential().Password
$viconnection = connect-viserver $selectedVC -User $username -Password $password
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form2 = New-Object System.Windows.Forms.Form
$Form2.AutoSize = $True
$Form2.AutoSizeMode = "GrowAndShrink"
$Form2.width = 600
$Form2.height = 500
$Form2.Text = ”Vcenter Selection”
$Form2.StartPosition = "CenterScreen"
#First item selection*******************
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(140,10)
$DropDownLabel1.size = new-object System.Drawing.Size(130,30)
$DropDownLabel1.Text = "$selectedVC"
$Form2.Controls.Add($DropDownLabel1)
$DropDownLabel2 = new-object System.Windows.Forms.Label
$DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel2.size = new-object System.Drawing.Size(100,40)
$DropDownLabel2.Text = "Connected to Vcenter"
$Form2.Controls.Add($DropDownLabel2)
#Second item selection*******************
[array]$clusterselection = get-cluster
$DropDownTo = New-Object System.Windows.Forms.ComboBox
$DropDownTo.Location = New-Object System.Drawing.Size(140,50)
$DropDownTo.Size = New-Object System.Drawing.Size(130,30)
$DropdownTo.DropDownStyle = 2
ForEach ($Item2 in $clusterselection)
{
[void] $DropDownTo.Items.Add($Item2)
}
$Form2.Controls.Add($DropDownTo)
$DropDownLabel3 = new-object System.Windows.Forms.Label
$DropDownLabel3.Location = new-object System.Drawing.Size(10,50)
$DropDownLabel3.size = new-object System.Drawing.Size(100,40)
$DropDownLabel3.Text = "Select Cluster Location"
$Form2.Controls.Add($DropDownLabel3)
#Third item selection*******************
$DropDownType = New-Object System.Windows.Forms.ComboBox
$DropDownType.Location = New-Object System.Drawing.Size(140,90)
$DropDownType.Size = New-Object System.Drawing.Size(130,30)
$DropDownType.DropDownStyle = 2
ForEach ($Item3 in $typeOfServer)
{
[void] $DropDownType.Items.Add($Item3)
}
$Form2.Controls.Add($DropDownType)
$DropDownLabel4 = new-object System.Windows.Forms.Label
$DropDownLabel4.Location = new-object System.Drawing.Size(10,90)
$DropDownLabel4.size = new-object System.Drawing.Size(100,40)
$DropDownLabel4.Text = "Select Type of Server"
$Form2.Controls.Add($DropDownLabel4)
#Fourth item Input*******************
$InputName = New-Object System.Windows.Forms.Textbox
$InputName.Location = New-Object System.Drawing.Size(140,130)
$InputName.Size = New-Object System.Drawing.Size(130,30)
$InputName.MaxLength = 15
$Form2.Controls.Add($InputName)
$inputLabel = new-object System.Windows.Forms.Label
$inputLabel.Location = new-object System.Drawing.Size(10,130)
$inputLabel.size = new-object System.Drawing.Size(100,40)
$inputLabel.Text = "Enter Name of Server"
$Form2.Controls.Add($inputLabel)
#Fifth item selection*******************
$dropDownMem = New-Object System.Windows.Forms.Combobox
$dropDownMem.Location = New-Object System.Drawing.Size(140,170)
$dropDownMem.Size = New-Object System.Drawing.Size(130,30)
$dropDownMem.DropDownStyle = 2
ForEach ($Item5 in $mem)
{
[void] $dropDownMem.Items.Add($Item5)
}
$Form2.Controls.Add($dropDownMem)
$DropDownLabel5 = new-object System.Windows.Forms.Label
$DropDownLabel5.Location = new-object System.Drawing.Size(10,170)
$DropDownLabel5.size = new-object System.Drawing.Size(100,40)
$DropDownLabel5.Text = "Select Memory Quantity in GB"
$Form2.Controls.Add($DropDownLabel5)
#Sixth item selection*******************
$dropDownvCpu = New-Object System.Windows.Forms.Combobox
$dropDownvCpu.Location = New-Object System.Drawing.Size(140,210)
$dropDownvCpu.Size = New-Object System.Drawing.Size(130,30)
$dropDownvCpu.DropDownStyle = 2
ForEach ($Item6 in $vCpu)
{
[void] $dropDownvCpu.Items.Add($Item6)
}
$Form2.Controls.Add($dropDownvCpu)
$DropDownLabel6 = new-object System.Windows.Forms.Label
$DropDownLabel6.Location = new-object System.Drawing.Size(10,210)
$DropDownLabel6.size = new-object System.Drawing.Size(100,40)
$DropDownLabel6.Text = "Select Virtual CPU's"
$Form2.Controls.Add($DropDownLabel6)
#Seventh item selection*******************
$dropDownOS = New-Object System.Windows.Forms.Combobox
$dropDownOS.Location = New-Object System.Drawing.Size(140,250)
$dropDownOS.Size = New-Object System.Drawing.Size(130,30)
$dropDownOS.DropDownStyle = 2
ForEach ($Item7 in $operatingSystem)
{
[void] $dropDownOS.Items.Add($Item7)
}
$Form2.Controls.Add($dropDownOS)
$DropDownLabel7 = new-object System.Windows.Forms.Label
$DropDownLabel7.Location = new-object System.Drawing.Size(10,250)
$DropDownLabel7.size = new-object System.Drawing.Size(100,40)
$DropDownLabel7.Text = "Select Operating System"
$Form2.Controls.Add($DropDownLabel7)
#Defines buttons for click to accept*********************
#$Button1 = new-object System.Windows.Forms.Button
#$Button1.Location = new-object System.Drawing.Size(10,300)
#$Button1.Size = new-object System.Drawing.Size(100,20)
#$Button1.Text = "Provision Vm"
#$Button1.Add_Click({Provision-VM})
#$Button1.Add_Click({Get-Account $oRphanedAcct})
#Defines buttons for click to accept*********************
$Button2 = new-object System.Windows.Forms.Button
$Button2.Location = new-object System.Drawing.Size(10,330)
$Button2.Size = new-object System.Drawing.Size(100,20)
$Button2.Text = "Provision Vm"
$Button2.Add_Click({Provision-VM -arg1 "$choice4" -arg2 "$choice5" -arg3 "$Choice3"})
#Defines buttons for click to accept*********************
$Button3 = new-object System.Windows.Forms.Button
$Button3.Location = new-object System.Drawing.Size(140,330)
$Button3.Size = new-object System.Drawing.Size(100,20)
$Button3.Text = "Close Form"
#$Button3.Add_Click({Return-Drop})
$Button3.Add_Click({$Form2.Dispose()})
#Defines buttons for click to accept*********************
$Button4 = new-object System.Windows.Forms.Button
$Button4.Location = new-object System.Drawing.Size(275,330)
$Button4.Size = new-object System.Drawing.Size(100,20)
$Button4.Text = "Ok Button"
$Button4.Dialogresult = [System.Windows.Forms.DialogResult]::OK
$Form2.AcceptButton = $Button4
$Form2.Controls.Add($Button2)
$Form2.Controls.Add($Button3)
$Form2.Controls.Add($Button4)
$Form2.Topmost = $True
$Form2.ShowDialog()
$Form2.refresh()
return $DropdownTo.SelectedItem
return $DropDownType.SelectedItem.ToString()
return $InputName.SelectedItem.ToString()
return $DropDownMem.SelectedItem.ToString()
return $DropDownVcpu.SelectedItem.ToString()
return $DropDownOS.SelectedItem.ToString()
return $Button3
return $Button4
}
#Main function definition********************
function Menu
{
$clusterselection = $null
$global:defaultviserver = $null
[array]$VCItems = "VCSelection","VCSelection2","VCSelection3"
[array]$typeOfServer = "Vanilla", "IIS", "SQL", "SQL_Cluster_Node"
[array]$operatingSystem = "Windows2012", "Windows2016"
[array]$mem = "2","4","6","8","10","12","32","64"
[array]$vCpu = "2","4","6","8","10","12"
#Function for Closing Form Upon no input
function Return-Drop
{
If (($dropDownVC.SelectedItem -ne $null) -or ($DropDownTO.SelectedItem -ne $null) -or ($DropDownType.SelectedItem -ne $null))
{
$Form.Close()
}
}
#Function for connecting to Vcenter
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.AutoSize = $True
$Form.AutoSizeMode = "GrowAndShrink"
$Form.width = 600
$Form.height = 500
$Form.Text = ”Vcenter Selection”
$Form.StartPosition = "CenterScreen"
#First item selection*******************
$DropDownVC = New-Object System.Windows.Forms.ComboBox
$DropDownVC.Location = New-Object System.Drawing.Size(140,10)
$DropDownVC.Size = New-Object System.Drawing.Size(130,30)
$DropDownVC.DropDownStyle = 2
$DropDownVC.TabIndex = 0
ForEach ($Item1 in $VCItems)
{
[void] $DropDownVC.Items.Add($Item1)
}
$DropDownVC.SelectedItem = $DropDownVC.Items[0]
$Form.Controls.Add($DropDownVC)
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel1.size = new-object System.Drawing.Size(100,40)
$DropDownLabel1.Text = "Select Vcenter to connect to"
$Form.Controls.Add($DropDownLabel1)
#Defines buttons for click to connect to VC*********************
$Button5 = new-object System.Windows.Forms.Button
$Button5.Location = new-object System.Drawing.Size(290,10)
$Button5.Size = new-object System.Drawing.Size(100,20)
$Button5.Text = "Connect"
$Button5.Add_Click({$form.visible = $false;$t = Connect-Vc -vcArg $DropDownVC})
$Form.Controls.Add($Button5)
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Choice1 = $DropDownVC.SelectedItem.ToString()
enter code here
return $Choice1
}