135531
Goto Top

PowerShell GUI, Cmdlet in ComboBox ausführen

Hi @all,

ich bin absoluter Neuling wenn es um die nutzung von PowerShell geht. Dennoch versuche ich im Moment eine PowerShell GUI zu bauen um das Discrete Device Assignment (DDA) zu vereinfachen.
Mein Problem ist, dass ich keine Ahnung habe wie ich ein Befehl (Cmdlet) in einer ComboBox ausführe, sprich --> klick auf ComboBox Ausgabe des Befehls erscheint zur Auswahl.
Genauer gesagt will ich es so haben, dass ich in der ComboBox alle VMs des Lokalen Servers (PCs) zur Auswahl habe.
Also soll der Befehl Get-VM ausgeführt werden und die Ausgabe in der ComboBox zur Auswahl stehen.

Aber wie funktioniert das ? :/ Kann mir da jemand helfen ?

Mein PowerShell skript sieht aktuell wie folgt aus :

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Backcolor="White"
$objForm.BackgroundImageLayout = 2
$objForm.BackgroundImage =[System.Drawing.Image]::FromFile('C:\Users\jannikr\Pictures\Microsoft-Logo.jpg')
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(650,650)
$objForm.Text = "Discrete Device Assignment"

#VM Namen eingeben

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,50)
$objLabel.Size = New-Object System.Drawing.Size(250,50)
$objLabel.Text = "Bitte wählen sie eine VM aus:"
$objForm.Controls.Add($objLabel)

$objComboBox = New-Object System.Windows.Forms.Combobox
$objComboBox.Location = New-Object System.Drawing.Size(50,100)
$objComboBox.Size = New-Object System.Drawing.Size(250,50)
[void] $objComboBox.Items.Add("VM-Name")
$objComboBox.Height = 70
$objForm.Controls.Add($objComboBox)
$objForm.TopMost = $True
$objComboBox.Add_SelectedIndexChanged({ })

function Get-VM ()
{
if ($objComboBox.SelectedItem -eq "VM-Name")
Get-VM
}


Wie Ihr sehen könnt habe ich nichtmal einen Ansatzpunkt um mein Vorhaben in die Realität umzusetzen... face-sad

Ich nehme jedes Ratschlag/Tipp an den ich bekommen kann!!! :D

Content-Key: 365754

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

Ausgedruckt am: 19.03.2024 um 04:03 Uhr

Mitglied: 135333
135333 22.02.2018 aktualisiert um 15:20:35 Uhr
Goto Top
$objComboBox.Add_SelectedIndexChanged({ 
    [System.Windows.Forms.MessageBox]::Show((Get-VM -Name $objComboBox.SelectedItem  | fl * | out-string))
})
Gruß Snap
Mitglied: 135531
135531 22.02.2018 um 15:21:04 Uhr
Goto Top
Hi Snap,

was genau soll ich denn nun mit diesem Befehl machen ? Es funktioniert leider nicht...

Ich habe es jetzt so gemacht (siehe letzte Zeilen) :

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Backcolor="White"
$objForm.BackgroundImageLayout = 2
$objForm.BackgroundImage =[System.Drawing.Image]::FromFile('C:\Users\jannikr\Pictures\Microsoft-Logo.jpg')
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(650,650)
$objForm.Text = "Discrete Device Assignment"

#VM Namen eingeben

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,50)
$objLabel.Size = New-Object System.Drawing.Size(250,50)
$objLabel.Text = "Bitte wählen sie eine VM aus:"
$objForm.Controls.Add($objLabel)

$objComboBox = New-Object System.Windows.Forms.Combobox
$objComboBox.Location = New-Object System.Drawing.Size(50,100)
$objComboBox.Size = New-Object System.Drawing.Size(250,50)
$objComboBox.Height = 70
$objForm.Controls.Add($objComboBox)
$objForm.TopMost = $True
$objComboBox.Add_SelectedIndexChanged({
Get-VM -Name $objComboBox.SelectedItem
})
Mitglied: 135333
135333 22.02.2018 aktualisiert um 15:25:05 Uhr
Goto Top
s.o. Hatte die MessageBox vergessen °|°
Die Combobox musst du ja vorher erst mal füllen ...Z.B. im Load-Event der Form.
$objForm.add_Load({
   $objComboBox.Items.AddRange((get-vm | select -Expand Name))
})
Mitglied: 135531
135531 22.02.2018 um 15:27:52 Uhr
Goto Top
Okay Danke.

Aber wie fülle ich die ComboBox ? Bzw. was ist das Load-Event der Form ?

P.S: Wie gesagt, ich bin blutiger Anfänger...
Mitglied: 135333
135333 22.02.2018 aktualisiert um 15:33:16 Uhr
Goto Top
Zitat von @135531:
Aber wie fülle ich die ComboBox ? Bzw. was ist das Load-Event der Form ?
Steht im letzten Post.
Mitglied: 135531
135531 22.02.2018 um 15:37:30 Uhr
Goto Top
geht !

Danke dir Snap ! face-smile