I'm new to PowerShell and now I got to forms.
And I have a problem that I cannot solve in any way.
I made a form with buttons, textboxes and labels.
I have set a fixed size for each item.
Form opens normally on most PCs
But on some, it opens like this:
And I don't understand how to fix it at all. I spent a whole day on this.
I have already used
$objForm.AutoScaleMode = 'dpi'
$objForm.AutoScaleDimensions = '6, 13'
But that doesn't help.
Please, tell me how to make the form look normal for everyone.
Thanks!
UPD: I made sure that this effect is achieved by changing the scale to 125%
But I never found a solution.
P.S.
Sorry for bad English :-)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Computer Connect"
$objForm.Size = New-Object System.Drawing.Size(280,350)
$objForm.StartPosition = "CenterScreen"
$objForm.BackColor = "Lightblue"
$objForm.KeyPreview = $True
$objForm.Topmost = $True
$objForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$objForm.Add_Shown({$objForm.Activate()})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Down")
{
$objListbox.SelectedIndex = $objListbox.SelectedIndex + 1
}
})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Up")
{
$objListbox.SelectedIndex = $objListbox.SelectedIndex - 1
}})
#BUTTON DW ADM
$HistoryRange = New-Object System.Windows.Forms.Listbox
$RCButton = New-Object System.Windows.Forms.Button
$RCButton.Location = New-Object System.Drawing.Size(10,160)
$RCButton.Size = New-Object System.Drawing.Size(75,23)
$RCButton.Text = "DW ADM"
$objForm.Controls.Add($RCButton)
#BUTTON DameWare
$DWButton = New-Object System.Windows.Forms.Button
$DWButton.Location = New-Object System.Drawing.Size(100,160)
$DWButton.Size = New-Object System.Drawing.Size(75,23)
$DWButton.Text = "DameWare"
$objForm.Controls.Add($DWButton)
##-------VDI BUTTON-
$VDIButton = New-Object System.Windows.Forms.Button
$VDIButton.Location = New-Object System.Drawing.Size(190,240)
$VDIButton.Size = New-Object System.Drawing.Size(75,23)
$VDIButton.Text = "VDI"
$objForm.Controls.Add($VDIButton)
#-----------------------------------------------------------------------
$VDIInput = New-Object System.Windows.Forms.TextBox
$VDIInput.Location = New-Object System.Drawing.Size(10,295)
$VDIInput.Size = New-Object System.Drawing.Size(255)
$objForm.Controls.Add($VDIInput)
#-----------------------------------------------------------------------
#BUTTON LOCK.RU
$InfoButton = New-Object System.Windows.Forms.Button
$InfoButton.Location = New-Object System.Drawing.Size(190,160)
$InfoButton.Size = New-Object System.Drawing.Size(75,23)
$InfoButton.Text = "lock.ru"
$objForm.Controls.Add($InfoButton)
##-------HELP BUTTON-
$HELPButton = New-Object System.Windows.Forms.Button
$HELPButton.Location = New-Object System.Drawing.Size(115, 190)
$HELPButton.Size = New-Object System.Drawing.Size(75, 23)
$HELPButton.Text = "HELP"
$objForm.Controls.Add($HELPButton)
#LOGGED ON
$Info = New-Object System.Windows.Forms.Button
$Info.Location = New-Object System.Drawing.Size(190,215)
$Info.Size = New-Object System.Drawing.Size(75,23)
$Info.Text = "Logged On machines"
$objForm.Controls.Add($Info)
#PING LABEL
$IPlabel1 = New-Object System.Windows.Forms.Label
$IPlabel1.Location = New-Object System.Drawing.Size(100,193)
$IPlabel1.Size = New-Object System.Drawing.Size(80,15)
$IPlabel1.Text = ""
$objForm.Controls.Add($IPlabel1)
$IPLabel = New-Object System.Windows.Forms.Label
$IPLabel.Location = New-Object System.Drawing.Size(10,193)
$IPLabel.Size = New-Object System.Drawing.Size(80,15)
$IPLabel.Text = "IP-адрес:"
$objForm.Controls.Add($IPLabel)
$MACLabel = New-Object System.Windows.Forms.Label
$MACLabel.Location = New-Object System.Drawing.Size(10,215)
$MACLabel.Size = New-Object System.Drawing.Size(80,15)
$MACLabel.Text = "MAC-адрес:"
$objForm.Controls.Add($MACLabel)
$MACaddress = New-Object System.Windows.Forms.label
$MACaddress.Location = New-Object System.Drawing.Size(100,215)
$MACaddress.Size = New-Object System.Drawing.Size(80,15)
$objForm.Controls.Add($MACaddress)
$pingLabel = New-Object System.Windows.Forms.Label
$PingLabel.Location = New-Object System.Drawing.Size(10,233)
$PingLabel.Size = New-Object System.Drawing.Size(170,60)
$PingLabel.Text = "Ping"
$PingLabel.AutoSize = $True
$objForm.Controls.Add($PingLabel)
#Button to UNLOCK
$UnlockButton = New-Object System.Windows.Forms.Button
$UnlockButton.Location = New-Object System.Drawing.Size(190,190)
$UnlockButton.Size = New-Object System.Drawing.Size(75,23)
$UnlockButton.Text = "UNLOCK"
$objForm.Controls.Add($UnlockButton)
#LABEL ПОИСК
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,18)
$objLabel.Size = New-Object System.Drawing.Size(40,20)
$objLabel.Text = "Поиск:"
$objForm.Controls.Add($objLabel)
#BUTTON Clear
$ClearButton = New-Object System.Windows.Forms.Button
$ClearButton.Location = New-Object System.Drawing.Size(172,12)
$ClearButton.Size = New-Object System.Drawing.Size(40,23)
$ClearButton.Text = "Clear"
$objForm.Controls.Add($ClearButton)
#BUTTON HISTORY
$HistoryButton = New-Object System.Windows.Forms.Button
$HistoryButton.Location = New-Object System.Drawing.Size(214,12)
$HistoryButton.Size = New-Object System.Drawing.Size(50,23)
$HistoryButton.Text = "History"
$objForm.Controls.Add($HistoryButton)
#TEXT BOX
$objInput = New-Object System.Windows.Forms.TextBox
$objInput.Location = New-Object System.Drawing.Size(50,15)
$objInput.Size = New-Object System.Drawing.Size(115)
$objForm.Controls.Add($objInput)
#LIST-BOX
$objListbox = New-Object System.Windows.Forms.Listbox
$objListbox.Location = New-Object System.Drawing.Size(10,43)
$objListbox.Size = New-Object System.Drawing.Size(253,20)
$objListbox.Height = 120
$objForm.Controls.Add($objListbox)
[void] $objForm.ShowDialog()
I ended up finding a solution. It's not as elegant as I expected, but it works.
I had to get AppliedDPI from the registry, calculate how many times the set scale is larger than the standard one, and simply increase all form elements as many times as needed.
it doesn't work if the user changes the scale while working.
But this is unlikely and my goal was to provide a normal view for users with large scales of 125%, 150%, etc.
#scaling
##################################################
$DPISetting = (Get-ItemProperty 'HKCU:\Control Panel\Desktop\WindowMetrics' -Name AppliedDPI).AppliedDPI
$dpiKoef = $DPISetting / 96
if ($dpiKoef -gt 1){
$objForm.AutoSize = $true
$objFormWidth = ($objForm.ClientSize).Width * $dpiKoef
$objFormHeight = ($objForm.ClientSize).Height * $dpiKoef
$objForm.ClientSize = New-Object System.Drawing.Size($objFormWidth,$objFormHeight)
$RCButtonWidth = ($RCButton.ClientSize).Width * $dpiKoef
$RCButtonHeight = ($RCButton.ClientSize).Height * $dpiKoef
$RCButton.ClientSize = New-Object System.Drawing.Size($RCButtonWidth,$RCButtonHeight)
$DWButtonWidth = ($DWButton.ClientSize).Width * $dpiKoef
$DWButtonHeight = ($DWButton.ClientSize).Height * $dpiKoef
$DWButton.ClientSize = New-Object System.Drawing.Size($DWButtonWidth,$DWButtonHeight)
$VDIButtonWidth = ($VDIButton.ClientSize).Width * $dpiKoef
$VDIButtonHeight = ($VDIButton.ClientSize).Height * $dpiKoef
$VDIButton.ClientSize = New-Object System.Drawing.Size($VDIButtonWidth,$VDIButtonHeight)
$VDIInputWidth = ($VDIInput.ClientSize).Width * $dpiKoef
$VDIInputHeight = ($VDIInput.ClientSize).Height * $dpiKoef
$VDIInput.ClientSize = New-Object System.Drawing.Size($VDIInputWidth,$VDIInputHeight)
$InfoButtonWidth = ($InfoButton.ClientSize).Width * $dpiKoef
$InfoButtonHeight = ($InfoButton.ClientSize).Height * $dpiKoef
$InfoButton.ClientSize = New-Object System.Drawing.Size($InfoButtonWidth,$InfoButtonHeight)
$HELPButtonWidth = ($HELPButton.ClientSize).Width * $dpiKoef
$HELPButtonHeight = ($HELPButton.ClientSize).Height * $dpiKoef
$HELPButton.ClientSize = New-Object System.Drawing.Size($HELPButtonWidth,$HELPButtonHeight)
$InfoWidth = ($Info.ClientSize).Width * $dpiKoef
$InfoHeight = ($Info.ClientSize).Height * $dpiKoef
$Info.ClientSize = New-Object System.Drawing.Size($InfoWidth,$InfoHeight)
$IPlabel1Width = ($IPlabel1.ClientSize).Width * $dpiKoef
$IPlabel1Height = ($IPlabel1.ClientSize).Height * $dpiKoef
$IPlabel1.ClientSize = New-Object System.Drawing.Size($IPlabel1Width,$IPlabel1Height)
$IPLabelWidth = ($IPLabel.ClientSize).Width * $dpiKoef
$IPLabelHeight = ($IPLabel.ClientSize).Height * $dpiKoef
$IPLabel.ClientSize = New-Object System.Drawing.Size($IPLabelWidth,$IPLabelHeight)
$MACaddressWidth = ($MACaddress.ClientSize).Width * $dpiKoef
$MACaddressHeight = ($MACaddress.ClientSize).Height * $dpiKoef
$MACaddress.ClientSize = New-Object System.Drawing.Size($MACaddressWidth,$MACaddressHeight)
$MACLabelWidth = ($MACLabel.ClientSize).Width * $dpiKoef
$MACLabelHeight = ($MACLabel.ClientSize).Height * $dpiKoef
$MACLabel.ClientSize = New-Object System.Drawing.Size($MACLabelWidth,$MACLabelHeight)
$pingLabelWidth = ($pingLabel.ClientSize).Width * $dpiKoef
$pingLabelHeight = ($pingLabel.ClientSize).Height * $dpiKoef
$pingLabel.ClientSize = New-Object System.Drawing.Size($pingLabelWidth,$pingLabelHeight)
$UnlockButtonWidth = ($UnlockButton.ClientSize).Width * $dpiKoef
$UnlockButtonHeight = ($UnlockButton.ClientSize).Height * $dpiKoef
$UnlockButton.ClientSize = New-Object System.Drawing.Size($UnlockButtonWidth,$UnlockButtonHeight)
$objLabelWidth = ($objLabel.ClientSize).Width * $dpiKoef
$objLabelHeight = ($objLabel.ClientSize).Height * $dpiKoef
$objLabel.ClientSize = New-Object System.Drawing.Size($objLabelWidth,$objLabelHeight)
$ClearButtonWidth = ($ClearButton.ClientSize).Width * $dpiKoef
$ClearButtonHeight = ($ClearButton.ClientSize).Height * $dpiKoef
$ClearButton.ClientSize = New-Object System.Drawing.Size($ClearButtonWidth,$ClearButtonHeight)
$HistoryButtonWidth = ($HistoryButton.ClientSize).Width * $dpiKoef
$HistoryButtonHeight = ($HistoryButton.ClientSize).Height * $dpiKoef
$HistoryButton.ClientSize = New-Object System.Drawing.Size($HistoryButtonWidth,$HistoryButtonHeight)
$objInputWidth = ($objInput.Size).Width * $dpiKoef
$objInputHeight = ($objInput.Size).Height * $dpiKoef
$objInput.ClientSize = New-Object System.Drawing.Size($objInputWidth,$objInputHeight)
$objListboxWidth = ($objListbox.ClientSize).Width * $dpiKoef
$objListboxHeight = ($objListbox.ClientSize).Height * $dpiKoef
$objListbox.ClientSize = New-Object System.Drawing.Size($objListboxWidth,$objListboxHeight)
$RCButtonX = ($RCButton.Location).X * $dpiKoef
$RCButtonY = ($RCButton.Location).Y * $dpiKoef
$RCButton.Location = New-Object System.Drawing.Size($RCButtonX,$RCButtonY)
$DWButtonX = ($DWButton.Location).X * $dpiKoef
$DWButtonY = ($DWButton.Location).Y * $dpiKoef
$DWButton.Location = New-Object System.Drawing.Size($DWButtonX,$DWButtonY)
$VDIButtonX = ($VDIButton.Location).X * $dpiKoef
$VDIButtonY = ($VDIButton.Location).Y * $dpiKoef
$VDIButton.Location = New-Object System.Drawing.Size($VDIButtonX,$VDIButtonY)
$VDIInputX = ($VDIInput.Location).X * $dpiKoef
$VDIInputY = ($VDIInput.Location).Y * $dpiKoef
$VDIInput.Location = New-Object System.Drawing.Size($VDIInputX,$VDIInputY)
$InfoButtonX = ($InfoButton.Location).X * $dpiKoef
$InfoButtonY = ($InfoButton.Location).Y * $dpiKoef
$InfoButton.Location = New-Object System.Drawing.Size($InfoButtonX,$InfoButtonY)
$HELPButtonX = ($HELPButton.Location).X * $dpiKoef
$HELPButtonY = ($HELPButton.Location).Y * $dpiKoef
$HELPButton.Location = New-Object System.Drawing.Size($HELPButtonX,$HELPButtonY)
$InfoX = ($Info.Location).X * $dpiKoef
$InfoY = ($Info.Location).Y * $dpiKoef
$Info.Location = New-Object System.Drawing.Size($InfoX,$InfoY)
$IPlabel1X = ($IPlabel1.Location).X * $dpiKoef
$IPlabel1Y = ($IPlabel1.Location).Y * $dpiKoef
$IPlabel1.Location = New-Object System.Drawing.Size($IPlabel1X,$IPlabel1Y)
$IPLabelX = ($IPLabel.Location).X * $dpiKoef
$IPLabelY = ($IPLabel.Location).Y * $dpiKoef
$IPLabel.Location = New-Object System.Drawing.Size($IPLabelX,$IPLabelY)
$MACaddressX = ($MACaddress.Location).X * $dpiKoef
$MACaddressY = ($MACaddress.Location).Y * $dpiKoef
$MACaddress.Location = New-Object System.Drawing.Size($MACaddressX,$MACaddressY)
$MACLabelX = ($MACLabel.Location).X * $dpiKoef
$MACLabelY = ($MACLabel.Location).Y * $dpiKoef
$MACLabel.Location = New-Object System.Drawing.Size($MACLabelX,$MACLabelY)
$pingLabelX = ($pingLabel.Location).X * $dpiKoef
$pingLabelY = ($pingLabel.Location).Y * $dpiKoef
$pingLabel.Location = New-Object System.Drawing.Size($pingLabelX,$pingLabelY)
$UnlockButtonX = ($UnlockButton.Location).X * $dpiKoef
$UnlockButtonY = ($UnlockButton.Location).Y * $dpiKoef
$UnlockButton.Location = New-Object System.Drawing.Size($UnlockButtonX,$UnlockButtonY)
$objLabelX = ($objLabel.Location).X * $dpiKoef
$objLabelY = ($objLabel.Location).Y * $dpiKoef
$objLabel.Location = New-Object System.Drawing.Size($objLabelX,$objLabelY)
$ClearButtonX = ($ClearButton.Location).X * $dpiKoef
$ClearButtonY = ($ClearButton.Location).Y * $dpiKoef
$ClearButton.Location = New-Object System.Drawing.Size($ClearButtonX,$ClearButtonY)
$HistoryButtonX = ($HistoryButton.Location).X * $dpiKoef
$HistoryButtonY = ($HistoryButton.Location).Y * $dpiKoef
$HistoryButton.Location = New-Object System.Drawing.Size($HistoryButtonX,$HistoryButtonY)
$objInputX = ($objInput.Location).X * $dpiKoef
$objInputY = ($objInput.Location).Y * $dpiKoef
$objInput.Location = New-Object System.Drawing.Size($objInputX,$objInputY)
$objListboxX = ($objListbox.Location).X * $dpiKoef
$objListboxY = ($objListbox.Location).Y * $dpiKoef
$objListbox.Location = New-Object System.Drawing.Size($objListboxX,$objListboxY)
}
##################################################
Related
trying to create simple win form in PowerShell. there will be some automatically calculated checkbox and i'm struggling with sizes - seems that 'AutoSize' is a bullsh*t and values returned are from nowhere. can anyone please help and suggest how you create dynamic positioning? based on which values?
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$nrOfNIC = 2
$nrOfDisks = 3
$nrOfPIP = 1
$vShift = 20
$allChkb = 1
$chkForm = New-Object system.Windows.Forms.Form
$chkForm.text = "Remove Resources"
$chkForm.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$chkForm.AutoSize = $true
$chkForm.StartPosition = 'CenterScreen'
$chkForm.FormBorderStyle = 'Fixed3D'
$chkForm.Icon = [System.Drawing.SystemIcons]::Question
$chkForm.Topmost = $true
$chkForm.MaximizeBox = $false
$chkVMBox = new-object System.Windows.Forms.GroupBox
#$chkVMBox.MinimumSize = New-Object System.Drawing.Size(180,100)
$chkVMBox.AutoSize = $true
$chkVMBox.Location = New-Object System.Drawing.Point(10,10)
$chkVMBox.Text = 'VM resources'
#$chkVMBox.Anchor = 'left,top'
#$lastControl = $chkVMBox
if($nrOfDisks -gt 0) {
$chkVMDisks = new-object System.Windows.Forms.GroupBox
#$chkVMDisks.MinimumSize = New-Object System.Drawing.Size(180,20)
$chkVMDisks.Location = New-Object System.Drawing.Point(10,20)
$chkVMDisks.Text = 'DISKs'
#$chkVMDisks.Anchor = 'left,top'
for($disk=0;$disk -lt $nrOfDisks;$disk++) {
$chkbDisk = New-Object System.Windows.Forms.Checkbox
$chkbDisk.Location = New-Object System.Drawing.Point(10, ($vShift+($disk*$vShift)) )
#$chkbDisk.Anchor = 'left,top'
$chkbDisk.AutoSize = $true
$chkbDisk.Text = "disk $disk"
$chkbDisk.TabIndex = $allChkb++
$chkVMDisks.Controls.Add($chkbDisk)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMDisks)
#$lastControl=$chkVMDisks
}
if($nrOfNIC -gt 0) {
#$vLocation = $lastControl.Bottom+$shift
$vLocation = $chkVMDisks.Bottom+$shift
$chkVMNICs = new-object System.Windows.Forms.GroupBox
#$chkVMNICs.MinimumSize = New-Object System.Drawing.Size(180,20)
$chkVMNICs.AutoSize = $true
$chkVMNICs.Location = New-Object System.Drawing.Point(10,$vLocation)
$chkVMNICs.Text = 'NICs'
#$chkVMNICs.Anchor = 'left,top'
for($nic = 0;$nic -lt $nrOfNIC; $nic++) {
$chkbNIC = New-Object System.Windows.Forms.Checkbox
$chkbNIC.Location = New-Object System.Drawing.Point(10, ($vShift+($nic*$vShift)) )
#$chkbNIC.Anchor = 'left,top'
$chkbNIC.AutoSize = $true
$chkbNIC.Text = "nic $nic"
$chkbNIC.TabIndex = $allChkb++
$chkVMNICs.Controls.Add($chkbNIC)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMNICs)
#$lastControl = $chkVMNICs
}
#$vLocation = $lastControl.Bottom+$shift
$vLocation = $chkVMNICs.Bottom+$shift
$chkbVMdiag = New-Object System.Windows.Forms.Checkbox
$chkbVMdiag.Location = New-Object System.Drawing.Point(10,$vLocation)
#$chkbVMdiag.Anchor = 'left,top'
$chkbVMdiag.AutoSize = $true
$chkbVMdiag.Text = "Boot Diagnostics"
$chkbVMdiag.TabIndex = $allChkb++
$chkVMBox.Controls.Add($chkbVMdiag)
$vLocation = $chkVMBox.bottom + 40
$btOK = New-Object System.Windows.Forms.Button
$btOK.Location = New-Object System.Drawing.Size(15,$vLocation)
$btOK.Size = New-Object System.Drawing.Size(70,20)
$btOK.Text = "OK"
$btOK.DialogResult = [System.Windows.Forms.DialogResult]::OK
#$btOK.Anchor = 'left,bottom'
$btCancel = New-Object System.Windows.Forms.Button
$btCancel.Location = New-Object System.Drawing.Size(95,$vLocation)
$btCancel.Size = New-Object System.Drawing.Size(70,20)
$btCancel.Text = "Cancel"
$btCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
#$btCancel.Anchor = 'right,bottom'
$chkForm.AcceptButton = $btOK
$chkForm.CancelButton = $btCancel
$chkForm.Controls.AddRange(#($chkVMBox, $btOK, $btCancel))
[void]$chkForm.ShowDialog()
effect:
effect of code
there is some big padding on the right of the main form
in check box list - there is additional space in first, double the space in second control
there is some padding under last chkbox
chkVMBox (main box) returns 'size = 100' 'bottom = 110'and buttons which uses this value are actually under other controls and invisible.
seems that ALL VALUES returned with 'autosize' are bullsh*t. so how do you create dynamic layout, not knowing sizes/number of elements upfront?
there is some big padding on the right of the main form
in check box list - there is additional space in first, double the space in second control
there is some padding under last chkbox
Set .AutoSizeMode = 'GrowAndShrink' as well as .MinimumSize properties along with the .AutoSize.
buttons … are actually under other controls and invisible
Button positions are derived from $chkVMBox.bottom; use $chkForm.Controls.Add($chkVMBox) before computing vertical location for buttons and remove $chkVMBox from $chkForm.Controls.AddRange(…)
to create dynamic layout, not knowing sizes/number of elements upfront:
I'd try calculating horizontal and vertical positions and (minimal) sizes of controls relative to an invariant; for instance, those are computed from $chkForm.Font in the following code (aspiration apparently inconsistent:), see the $hShift and $vShift variables, their values and usage.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Remove-Variable chk* -ErrorAction SilentlyContinue
$nrOfNIC = 2
$nrOfDisks = 3
$nrOfPIP = 1
$allChkb = 1
$chkForm = New-Object System.Windows.Forms.Form
$chkForm.text = "Remove Resources"
$chkForm.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$hShift = $chkForm.Font.SizeInPoints
$vShift = $chkForm.Font.Height * 1.5 ### arbitrary ad hoc coefficient
$chkForm.StartPosition = 'CenterScreen'
$chkForm.FormBorderStyle = 'Fixed3D'
$chkForm.Icon = [System.Drawing.SystemIcons]::Question
$chkForm.Topmost = $true
$chkForm.AutoSize = $true
$chkForm.AutoSizeMode = 'GrowAndShrink'
$chkForm.MinimumSize = [System.Drawing.Size]::new(100,100)
# $chkForm.AutoScaleMode = 'Font'
$chkForm.MaximizeBox = $false
$chkForm.MinimizeBox = $false
$chkVMBox = new-object System.Windows.Forms.GroupBox
$chkVMBox.AutoSizeMode = 'GrowAndShrink'
$chkVMBox.AutoSize = $true
$chkVMBox.Location = New-Object System.Drawing.Point(10,10)
$chkVMBox.Text = 'VM resources'
if($nrOfDisks -gt 0) {
$chkVMDisks = new-object System.Windows.Forms.GroupBox
$chkVMDisks.Location = New-Object System.Drawing.Point($hShift,( $vShift+10))
$chkVMDisks.Text = 'DISKs'
$chkVMDisks.AutoSize = $true
$chkVMDisks.MinimumSize = [System.Drawing.Size]::new(($chkVMBox.Size.Width - $hShift),$vShift)
$chkVMDisks.AutoSizeMode = 'GrowAndShrink'
for($disk=0;$disk -lt $nrOfDisks;$disk++) {
$chkbDisk = New-Object System.Windows.Forms.Checkbox
$chkbDisk.Location = New-Object System.Drawing.Point($hShift, ($vShift*($disk+1)))
$chkbDisk.AutoSize = $true
$chkbDisk.Text = "disk $disk"
$chkbDisk.TabIndex = $allChkb++
$chkVMDisks.Controls.Add($chkbDisk)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMDisks)
#$lastControl=$chkVMDisks
}
if($nrOfNIC -gt 0) {
#$vLocation = $lastControl.Bottom+$hShift
$vLocation = $chkVMDisks.Bottom + $vShift
$chkVMNICs = new-object System.Windows.Forms.GroupBox
$chkVMNICs.AutoSize = $true
$chkVMNICs.MinimumSize = [System.Drawing.Size]::new(($chkVMBox.Size.Width - $hShift),$vShift)
$chkVMNICs.AutoSizeMode = 'GrowAndShrink'
$chkVMNICs.Location = New-Object System.Drawing.Point($hShift,$vLocation)
$chkVMNICs.Text = 'NICs'
for($nic = 0;$nic -lt $nrOfNIC; $nic++) {
$chkbNIC = New-Object System.Windows.Forms.Checkbox
$chkbNIC.Location = New-Object System.Drawing.Point($hShift, ($vShift*($nic+1)) )
# $chkbNIC.Anchor = 'left,top'
$chkbNIC.AutoSize = $true
$chkbNIC.Text = "nic $nic"
$chkbNIC.TabIndex = $allChkb++
$chkVMNICs.Controls.Add($chkbNIC)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMNICs)
#$lastControl = $chkVMNICs
}
#$vLocation = $lastControl.Bottom+$hShift
$vLocation = $chkVMNICs.Bottom + $vShift
$chkbVMdiag = New-Object System.Windows.Forms.Checkbox
$chkbVMdiag.Location = New-Object System.Drawing.Point($hShift,$vLocation)
$chkbVMdiag.AutoSize = $true
$chkbVMdiag.Text = "Boot Diagnostics"
$chkbVMdiag.TabIndex = $allChkb++
$chkVMBox.Controls.Add($chkbVMdiag)
$chkForm.Controls.Add($chkVMBox)
$vLocation = $chkVMBox.bottom + 20
$btOK = New-Object System.Windows.Forms.Button
$btOK.Location = New-Object System.Drawing.Point(15,$vLocation)
# $btOK.Size = New-Object System.Drawing.Size(70,20)
$btOK.AutoSize = $true
$btOK.Text = "OK"
$btOK.DialogResult = [System.Windows.Forms.DialogResult]::OK
$btCancel = New-Object System.Windows.Forms.Button
$btCancel.Location = New-Object System.Drawing.Point(95,$vLocation)
# $btCancel.Size = New-Object System.Drawing.Size(70,20)
$btCancel.AutoSize = $true
$btCancel.Text = "Cancel"
$btCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$chkForm.AcceptButton = $btOK
$chkForm.CancelButton = $btCancel
$chkForm.Controls.AddRange(#($btOK, $btCancel))
[void]$chkForm.ShowDialog()
$chkForm.Dispose()
I have an issue with my PS script including a listview.
I need to check the content of 6 directorys and show the content in 6 different columns :
The issue is that i can't get a correct visual, they are all in the same column
Here is my code :
$Form = New-Object Windows.Forms.Form
$Form.Text = "New Test"
$Form.Width = 1550
$Form.Height = 800
$Listview = New-Object System.Windows.Forms.ListView
$Listview.Location = New-Object System.Drawing.Size(15,10)
$Listview.Size = New-Object System.Drawing.Size(550,10)
$Listview.AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent)
$Listview.View = "Details"
$Listview.FullRowSelect = $true
$Listview.GridLines = $true
$Listview.Height = 650
$Listview.Width =1500
$Listview.AllowColumnReorder = $true
$Listview.Sorting = [System.Windows.Forms.SortOrder]::None
#[void]$Listview.Columns.Add('Null',0)
[void]$Listview.Columns.Add('Scripts',150)
[void]$Listview.Columns.Add('Applications',150)
[void]$Listview.Columns.Add('Systems',500)
[void]$Listview.Columns.Add('Databases',100)
[void]$Listview.Columns.Add('Datas',150)
[void]$Listview.Columns.Add('Backups',500)
$oButton = New-Object Windows.Forms.Button
$oButton.Text = "List"
$oButton.Top = 700
$oButton.Left = 350
$oButton.Width = 150
$oButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
$a = (Get-ChildItem "C:\Scripts\").Name
$b = (Get-ChildItem "C:\Applications\").Name
$c = (Get-ChildItem "C:\Systems\").Name
$d = (Get-ChildItem "C:\Databases\").Name
$e = (Get-ChildItem "C:\Datas\").Name
$f = (Get-ChildItem "C:\Backups\").Name
$Entry = New-Object System.Windows.Forms.ListViewItem($_.Null)
foreach($mp in $a){
$Entry1 = New-Object System.Windows.Forms.ListViewItem($_.Scripts)
$Listview.Items.Add($mp)
}
foreach($mp1 in $b){
$Entry2 = New-Object System.Windows.Forms.ListViewItem($_.Applications)
$Listview.Items.Add($Entry2).SubItems.Add($mp1)
}
foreach($mp2 in $c){
$Entry3 = New-Object System.Windows.Forms.ListViewItem($_.Systems)
$Listview.Items.Add($Entry3).SubItems.Add($mp2)
}
foreach($mp3 in $d){
$Entry4 = New-Object System.Windows.Forms.ListViewItem($_.Databases)
$Listview.Items.Add($Entry4).SubItems.Add($mp3)
}
foreach($mp4 in $e){
$Entry5 = New-Object System.Windows.Forms.ListViewItem($_.Datas)
$Listview.Items.Add($Entry5).SubItems.Add($mp4)
}
foreach($mp5 in $f){
$Entry6 = New-Object System.Windows.Forms.ListViewItem($_.Backup)
$Listview.Items.Add($Entry6).SubItems.Add($mp5)
}
$Form.Add_Shown({$Form.Activate()})
$Form.controls.add($oButton)
$Form.controls.add($Listview)
$Form.ShowDialog()
I tried a lot of change but im still blocked
Thanks in advance for you help
Regards
Should be pretty straight forward but cant work out the logic. Below is the code I currently have which sort of works but the only problem is if more than one checkboxes are checked and you uncheck one the button disables, I need the button to remain enabled as long as there is one or more checkbox checked. I've also tried various convoluted If and Elseif statements but nothing I've tried changes this behaviour.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '170,191'
$Form.text = "Scheduler"
$Form.TopMost = $false
$MonCheckBox = New-Object system.Windows.Forms.CheckBox
$MonCheckBox.text = "Monday"
$MonCheckBox.AutoSize = $false
$MonCheckBox.width = 95
$MonCheckBox.height = 20
$MonCheckBox.location = New-Object System.Drawing.Point(30,30)
$MonCheckBox.Font = 'Microsoft Sans Serif,10'
$TueCheckBox = New-Object system.Windows.Forms.CheckBox
$TueCheckBox.text = "Tuesday"
$TueCheckBox.AutoSize = $false
$TueCheckBox.width = 95
$TueCheckBox.height = 20
$TueCheckBox.location = New-Object System.Drawing.Point(30,50)
$TueCheckBox.Font = 'Microsoft Sans Serif,10'
$WedCheckBox = New-Object system.Windows.Forms.CheckBox
$WedCheckBox.text = "Wednesday"
$WedCheckBox.AutoSize = $false
$WedCheckBox.width = 95
$WedCheckBox.height = 20
$WedCheckBox.location = New-Object System.Drawing.Point(30,70)
$WedCheckBox.Font = 'Microsoft Sans Serif,10'
$ThuCheckBox = New-Object system.Windows.Forms.CheckBox
$ThuCheckBox.text = "Thursday"
$ThuCheckBox.AutoSize = $false
$ThuCheckBox.width = 95
$ThuCheckBox.height = 20
$ThuCheckBox.location = New-Object System.Drawing.Point(30,90)
$ThuCheckBox.Font = 'Microsoft Sans Serif,10'
$FriCheckBox = New-Object system.Windows.Forms.CheckBox
$FriCheckBox.text = "Friday"
$FriCheckBox.AutoSize = $false
$FriCheckBox.width = 95
$FriCheckBox.height = 20
$FriCheckBox.location = New-Object System.Drawing.Point(30,110)
$FriCheckBox.Font = 'Microsoft Sans Serif,10'
$SchedButton = New-Object system.Windows.Forms.Button
$SchedButton.text = "Schedule"
$SchedButton.width = 60
$SchedButton.height = 30
$SchedButton.location = New-Object System.Drawing.Point(30,134)
$SchedButton.Font = 'Microsoft Sans Serif,10'
$SchedButton.Autosize = $true
$SchedButton.Enabled = $false
$Form.controls.AddRange(#($MonCheckBox,$TueCheckBox,$WedCheckBox,$ThuCheckBox,$FriCheckBox,$SchedButton))
$MonCheckBox.add_CheckedChanged({$SchedButton.Enabled = $MonCheckBox.Checked})
$TueCheckBox.add_CheckedChanged({$SchedButton.Enabled = $TueCheckBox.Checked})
$WedCheckBox.add_CheckedChanged({$SchedButton.Enabled = $WedCheckBox.Checked})
$ThuCheckBox.add_CheckedChanged({$SchedButton.Enabled = $ThuCheckBox.Checked})
$FriCheckBox.add_CheckedChanged({$SchedButton.Enabled = $FriCheckBox.Checked})
[void]$Form.ShowDialog()
I would do something like this:
$Form.controls.AddRange(#($MonCheckBox, $TueCheckBox, $WedCheckBox, $ThuCheckBox, $FriCheckBox, $SchedButton))
Function Test-AnyButtonChecked {
if (
$MonCheckBox.Checked -or
$TueCheckBox.Checked -or
$WedCheckBox.Checked -or
$ThuCheckBox.Checked -or
$FriCheckBox.Checked
) {
$SchedButton.Enabled = $true
}
else {
$SchedButton.Enabled = $false
}
}
$MonCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$TueCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$WedCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$ThuCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$FriCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
[void]$Form.ShowDialog()
I'm trying to create a gui with comboboxes and textboxes. If the user select a item from the Combobox, the related values from an array should appears in textboxes. Can someone give me a clue? I already tried to enumerat thru the arrays and select the key/value pair with -eq, but unfortunately I was not able to solve it this way.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Arrays
$CB_NDL_Array = #{
City1 = #{
Street = "Test1"
Postalcode = "5435"
}
City2 = #{
Street="Test2"
Postalcode="23423"
}
City3 = #{
Street="Test3"
Postalcode="234"
}}
$UserManager.controls.AddRange(#($Vorname,$Nachname,$TB_Givenname,$TB_Surname,$Anmeldename,$TB_SamAccountName,$Passwort,$TB_Password,$Niederlassung,$CB_NDL,$Street,$TB_Streetaddress,$City,$TB_City,$PLZ,$TB_Postalcode,$Buero,$TB_Office,$Abteilung,$TB_Department,$Position,$CB_Title,$Firma,$CB_Company,$Manager,$TB_Manager,$Telefon,$TB_OfficePhone,$Mobile,$TB_Mobile,$UserInfo,$Exchange,$RB_exc_yes,$RB_exc_no,$Email_address,$TB_email_address,$CB_Database,$Database,$B_Create))
$CB_NDL = New-Object system.Windows.Forms.ComboBox
$CB_NDL.text = ""
$CB_NDL.width = 100
$CB_NDL.height = 20
$CB_NDL.location = New-Object System.Drawing.Point(110,180)
$CB_NDL.Font = 'Microsoft Sans Serif,10'
$CB_NDL.SelectedItem
$Street = New-Object system.Windows.Forms.Label
$Street.text = "Strasse"
$Street.AutoSize = $true
$Street.width = 25
$Street.height = 10
$Street.location = New-Object System.Drawing.Point(15,210)
$Street.Font = 'Microsoft Sans Serif,10'
$TB_Streetaddress = New-Object system.Windows.Forms.TextBox
$TB_Streetaddress.multiline = $false
$TB_Streetaddress.width = 100
$TB_Streetaddress.height = 20
$TB_Streetaddress.location = New-Object System.Drawing.Point(110,210)
$TB_Streetaddress.Font = 'Microsoft Sans Serif,10'
$City = New-Object system.Windows.Forms.Label
$City.text = "Ort"
$City.AutoSize = $true
$City.width = 25
$City.height = 10
$City.location = New-Object System.Drawing.Point(15,240)
$City.Font = 'Microsoft Sans Serif,10'
$TB_City = New-Object system.Windows.Forms.TextBox
$TB_City.multiline = $false
$TB_City.width = 100
$TB_City.height = 20
$TB_City.location = New-Object System.Drawing.Point(110,240)
$TB_City.Font = 'Microsoft Sans Serif,10'
$PLZ = New-Object system.Windows.Forms.Label
$PLZ.text = "PLZ"
$PLZ.AutoSize = $true
$PLZ.width = 25
$PLZ.height = 10
$PLZ.location = New-Object System.Drawing.Point(15,270)
$PLZ.Font = 'Microsoft Sans Serif,10'
$TB_Postalcode = New-Object system.Windows.Forms.TextBox
$TB_Postalcode.multiline = $false
$TB_Postalcode.width = 100
$TB_Postalcode.height = 20
$TB_Postalcode.location = New-Object System.Drawing.Point(110,270)
$TB_Postalcode.Font = 'Microsoft Sans Serif,10'
$CB_NDL.Add_SelectedIndexChanged({
$TB_City.text = $CB_NDL.SelectedItem
$TB_Postalcode.text = $CB_NDL_Array.values | Where-Object{$CB_NDL.SelectedItem} | Where CB_NDL_Array.$_.key -eq "Postalcode" })
foreach ($NDL in $CB_NDL_Array.keys){
$CB_NDL.items.AddRange("$NDL")
}
foreach ($Title in $CB_Title_Array){
$CB_Title.items.add("$Title")
}
foreach ($Company in $CB_Company_Array){
$CB_Company.items.add("$Company")
}
[void]$UserManager.ShowDialog()
$SelectedItem=$CB_NDL.SelectedItem
$TB_Postalcode.text = $CB_NDL_Array.$SelectedItem.Postalcode
I have a small GUI that will connect to a vCenter in a list. The forms open in the order I want, but I can't carry values between them. I've tried so many return calls I don't know what else to do.
Here is the code. Basically, the first form opens and asks for which vCenter you want. Then prompts you to authenticate (this all works wonderfully). But the problem comes in after the script is done. I don't get any return values.
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
}