marabunta
Goto Top

Powershell Funktion mit mehreren Parametern (Code Optimierung)

Hallo,

für eine GUI habe ich mehrere Checkboxen und die sollen etwas ausführen.

#Mehrfach die Funktion aufrufen
Function StatusCheckbox ($Status)
{
if (checkbox12=checked)
{
Write-Host "Option 12 gewaehlt und wird ausgefuehrt."  
DoOption12
}
else{}
usw.
}
StatusCheckbox -Status $CheckBox12_Check
StatusCheckbox -Status $CheckBox11_Check
StatusCheckbox -Status $CheckBox10_Check

oder

#Lange Liste mit Parametern
Function StatusCheckbox ($CheckBox12_Check, $CheckBox11_Check, $CheckBox10_Check)
if($checkbox12.CheckState -eq "checked")  
Write-Host "Option 12 gewaehlt und wird ausgefuehrt."  
DoOption12
}
else{}
usw.
}

oder geht das noch eleganter?
Danke

Content-Key: 271004

Url: https://administrator.de/contentid/271004

Printed on: April 25, 2024 at 14:04 o'clock

Member: colinardo
Solution colinardo May 04, 2015 updated at 12:06:34 (UTC)
Goto Top
Moin Marabunta,

Beispielform s. weiter unten (eine von diversen Möglichkeiten face-wink):

Hiermit werden die Checkboxen in einer Schleife durchlaufen und geprüft ob sie ausgewählt sind. Wenn ja wird eine Funktion mit der Nummer als Parameter aufgerufen in welcher dann je nach Nummer eine Aktion ausgeführt wird:

Der essentielle Abschnitt des weiter unten stehenden kompletten Beispiels ist folgender
$handler_btnOK_Click= 
{
    # Fünf Checkboxen werden durchlaufen
    1..5 | %{
        # ist die Checkbox aktiviert führe Funktion mit Nummer der Checkbox als Parameter aus
        if ($form1.Controls["checkbox$_"].checked){  
            do-action $_
        }
    }
}

# Funktion die die Aktionen ausführt
function do-action($number){
    switch($number){
        1 {msg "Action 1 fired"}  
        2 {msg "Action 2 fired"}  
        3 {msg "Action 3 fired"}  
        4 {msg "Action 4 fired"}  
        5 {msg "Action 5 fired"}  
    }
}

# Hilfsfunktion für eine Messagebox (für Tippfaule ;-))
function msg([string]$message){
    [System.Windows.Forms.MessageBox]::Show($message)
}
back-to-topFunktionsfähiges Beispiel:
#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$btnOK = New-Object System.Windows.Forms.Button
$checkBox5 = New-Object System.Windows.Forms.CheckBox
$checkBox4 = New-Object System.Windows.Forms.CheckBox
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox1 = New-Object System.Windows.Forms.CheckBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
# Event Script Blocks
#----------------------------------------------

$handler_btnOK_Click= 
{
    1..5 | %{
        if ($form1.Controls["checkbox$_"].checked){  
            do-action $_
        }
    }
}
function do-action($number){
    switch($number){
        1 {msg "Action 1 fired"}  
        2 {msg "Action 2 fired"}  
        3 {msg "Action 3 fired"}  
        4 {msg "Action 4 fired"}  
        5 {msg "Action 5 fired"}  
    }
}
function msg([string]$message){
    [System.Windows.Forms.MessageBox]::Show($message)
}

$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 = 208
$System_Drawing_Size.Width = 157
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"  
$form1.Text = "Demo"  


$btnOK.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 173
$btnOK.Location = $System_Drawing_Point
$btnOK.Name = "btnOK"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 133
$btnOK.Size = $System_Drawing_Size
$btnOK.TabIndex = 5
$btnOK.Text = "Execute"  
$btnOK.UseVisualStyleBackColor = $True
$btnOK.add_Click($handler_btnOK_Click)

$form1.Controls.Add($btnOK)


$checkBox5.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 132
$checkBox5.Location = $System_Drawing_Point
$checkBox5.Name = "checkBox5"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$checkBox5.Size = $System_Drawing_Size
$checkBox5.TabIndex = 4
$checkBox5.Text = "checkBox5"  
$checkBox5.UseVisualStyleBackColor = $True

$form1.Controls.Add($checkBox5)


$checkBox4.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 102
$checkBox4.Location = $System_Drawing_Point
$checkBox4.Name = "checkBox4"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$checkBox4.Size = $System_Drawing_Size
$checkBox4.TabIndex = 3
$checkBox4.Text = "checkBox4"  
$checkBox4.UseVisualStyleBackColor = $True

$form1.Controls.Add($checkBox4)


$checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 72
$checkBox3.Location = $System_Drawing_Point
$checkBox3.Name = "checkBox3"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$checkBox3.Size = $System_Drawing_Size
$checkBox3.TabIndex = 2
$checkBox3.Text = "checkBox3"  
$checkBox3.UseVisualStyleBackColor = $True

$form1.Controls.Add($checkBox3)


$checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 42
$checkBox2.Location = $System_Drawing_Point
$checkBox2.Name = "checkBox2"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$checkBox2.Size = $System_Drawing_Size
$checkBox2.TabIndex = 1
$checkBox2.Text = "checkBox2"  
$checkBox2.UseVisualStyleBackColor = $True

$form1.Controls.Add($checkBox2)


$checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$checkBox1.Location = $System_Drawing_Point
$checkBox1.Name = "checkBox1"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$checkBox1.Size = $System_Drawing_Size
$checkBox1.TabIndex = 0
$checkBox1.Text = "checkBox1"  
$checkBox1.UseVisualStyleBackColor = $True

$form1.Controls.Add($checkBox1)

#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

} #End Function

#Call the Function
GenerateForm

Du könntest natürlich die Objekte der Checkboxen in ein Array packen und das an eine Funktion übergeben, und dann dieses Array in der Funktion in einer Schleife abarbeiten. Je nachdem was du genau machen willst käme das eventuell auch in Frage (Falls nicht immer alle Checkboxen geprüft werden müssen).

Grüße Uwe