Powershell Probleme mit initialer Start-Wert-Befüllung einer DropDownList
Hallo liebe Community,
ich schreibe hier das erste Mal und hoffe auf eure guten Tipps.
Aktuell schreibe ich ein großes Skript, welches die Umzüge von Userhomes von FileServer A nach B automatisiert. Zur Auswahl des Ziel-Servers verwende ich eine ComboBox, Style DropDownList.
Ich möchte nun folgendes realisieren:
Die DropDownList soll initial den ersten Wert des Arrays anzeigen, sodass keine $NULL-Übergabe entstehen kann. Zur besseren Übersicht habe ich die Zeilen durchnummeriert. in den Zeilen 21 & 22 wollte ich den Start-Wert setzen. Dies funktioniert so allerdings leider nicht. Könntet ihr mir hier weiterhelfen? Des Weiteren suche ich nach einem Equivalent zu "Me.Close()", um das Fenster mit einem implementierten "Cancel" - Button zu schließen und das gesamte Skript zu beenden.
Für eine Hilfe wäre ich euch sehr dankbar!
Viele Grüße
Daniel
1 function Server-Auswahl
2 {
3 [array]$global:ServerList=Get-Content -Path D:\Scripts\AD\User\PS_UHomeCopy\Tools\FSList.csv | Where-Object {$_ -ne ""}
4 function Return-DropDown
5 {
6 $global:newServer = $DropDown.SelectedItem.ToString()
7 $Form.Close()
8 }
9
10 $Form = New-Object System.Windows.Forms.Form
11 $Form.width = 300
12 $Form.height = 150
13 $Form.Text = ”DropDown”
14 $Form.StartPosition = "CenterScreen"
15 $Form.KeyPreview = $False
16
17 $DropDown = new-object System.Windows.Forms.ComboBox
18 $DropDown.DropDownStyle = "DropDownList"
19 $DropDown.Location = new-object System.Drawing.Size(100,10)
20 $DropDown.Size = new-object System.Drawing.Size(130,30)
21 [System.Int32]$int = 2
22 $DropDown.SelectedIndex = $int
23
24
25 ForEach ($Item in $global:ServerList)
26 {
27 $a = $DropDown.Items.Add($Item)
28 }
29
.
.
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,30)
$DropDownLabel.Text = "Bitte Ziel-Server auswählen:"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "OK"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
[void]$Form.ShowDialog()
}
Server-Auswahl
ich schreibe hier das erste Mal und hoffe auf eure guten Tipps.
Aktuell schreibe ich ein großes Skript, welches die Umzüge von Userhomes von FileServer A nach B automatisiert. Zur Auswahl des Ziel-Servers verwende ich eine ComboBox, Style DropDownList.
Ich möchte nun folgendes realisieren:
Die DropDownList soll initial den ersten Wert des Arrays anzeigen, sodass keine $NULL-Übergabe entstehen kann. Zur besseren Übersicht habe ich die Zeilen durchnummeriert. in den Zeilen 21 & 22 wollte ich den Start-Wert setzen. Dies funktioniert so allerdings leider nicht. Könntet ihr mir hier weiterhelfen? Des Weiteren suche ich nach einem Equivalent zu "Me.Close()", um das Fenster mit einem implementierten "Cancel" - Button zu schließen und das gesamte Skript zu beenden.
Für eine Hilfe wäre ich euch sehr dankbar!
Viele Grüße
Daniel
1 function Server-Auswahl
2 {
3 [array]$global:ServerList=Get-Content -Path D:\Scripts\AD\User\PS_UHomeCopy\Tools\FSList.csv | Where-Object {$_ -ne ""}
4 function Return-DropDown
5 {
6 $global:newServer = $DropDown.SelectedItem.ToString()
7 $Form.Close()
8 }
9
10 $Form = New-Object System.Windows.Forms.Form
11 $Form.width = 300
12 $Form.height = 150
13 $Form.Text = ”DropDown”
14 $Form.StartPosition = "CenterScreen"
15 $Form.KeyPreview = $False
16
17 $DropDown = new-object System.Windows.Forms.ComboBox
18 $DropDown.DropDownStyle = "DropDownList"
19 $DropDown.Location = new-object System.Drawing.Size(100,10)
20 $DropDown.Size = new-object System.Drawing.Size(130,30)
21 [System.Int32]$int = 2
22 $DropDown.SelectedIndex = $int
23
24
25 ForEach ($Item in $global:ServerList)
26 {
27 $a = $DropDown.Items.Add($Item)
28 }
29
.
.
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,30)
$DropDownLabel.Text = "Bitte Ziel-Server auswählen:"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "OK"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
[void]$Form.ShowDialog()
}
Server-Auswahl
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 241377
Url: https://administrator.de/forum/powershell-probleme-mit-initialer-start-wert-befuellung-einer-dropdownlist-241377.html
Ausgedruckt am: 25.04.2025 um 13:04 Uhr
5 Kommentare
Neuester Kommentar
Hallo dschauerte, Willkommen auf Adminstrator.de!
Du versuchst den Index des DropDown-Feldes zu setzen bevor du es überhaupt mit Inhalt gefüllt hast, also mach dies danach (Zeile 22 nach Zeile 29 verschieben).
Um deine Form zu schließen schreibst du einfach
Grüße Uwe
p.s. die Nummerierung kannst du dir sparen wenn du Code-Tags für deinen Quellcode nutzt:
Du versuchst den Index des DropDown-Feldes zu setzen bevor du es überhaupt mit Inhalt gefüllt hast, also mach dies danach (Zeile 22 nach Zeile 29 verschieben).
Um deine Form zu schließen schreibst du einfach
$Form.Close()
p.s. die Nummerierung kannst du dir sparen wenn du Code-Tags für deinen Quellcode nutzt:
<code> Quellcode </code>
, ansonsten kommen uns hier auch eventuell wichtige Sonderzeichen abhanden.
Hierfür musst du aus der Funktion einen Wert zurückgeben anhand dessen du dann in einem If() entscheidest ob das Script beendet wird oder nicht:
Beispiel-Form
Hier läuft das Script nur weiter wenn auf das rote X der Form geklickt wurde. Wenn auf den Exit-Button geklickt wurde nicht.
Beispiel-Form
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
$script:quit = $false
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button2 = New-Object System.Windows.Forms.Button
$button1 = New-Object System.Windows.Forms.Button
$DropDown = New-Object System.Windows.Forms.ComboBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
$handler_button2_Click=
{
$form1.Close()
$script:quit = $true
}
$handler_button1_Click=
{
$DropDown.Items.Clear()
1..10 | %{$DropDown.Items.Add($_)}
$DropDown.SelectedIndex = 2
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 103
$System_Drawing_Size.Width = 210
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Testform"
$button2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 102
$System_Drawing_Point.Y = 55
$button2.Location = $System_Drawing_Point
$button2.Name = "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$button2.Size = $System_Drawing_Size
$button2.TabIndex = 2
$button2.Text = "exit"
$button2.UseVisualStyleBackColor = $True
$button2.add_Click($handler_button2_Click)
$form1.Controls.Add($button2)
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 55
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 1
$button1.Text = "set index"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($handler_button1_Click)
$form1.Controls.Add($button1)
$DropDown.DataBindings.DefaultDataSourceUpdateMode = 0
$DropDown.DropDownStyle = 2
$DropDown.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$DropDown.Location = $System_Drawing_Point
$DropDown.Name = "DropDown"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 165
$DropDown.Size = $System_Drawing_Size
$DropDown.TabIndex = 0
$form1.Controls.Add($DropDown)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
# gebe den Wert aus der Function zurück
return $script:quit
} #End Function
#Call the Function
$result = GenerateForm
# wenn $true zurückgegeben wird beende Script
if($result){
exit
}
# Script wird weiter ausgeführt wenn die Form $false zurückgibt
write-host "Script continues here ..."