135531
Goto Top

PowerShell GUI Skript ausführen durch "Start" Knopf

Hi @all,

ich bin absoluter Neuling im Thema PowerShell. Ich versuche derzeit eine GUI für DDA (Discrete Device Assignment) zu bauen, bin auch schon soweit, dass das Skript die VMs und PCI-Geräte ausliest. Der nächste Schritt wäre nun das DDA Skript in das "GUI Skript" einzubauen, sprich wenn ich auf "Start" drücke soll das DDA skript ausgeführt werden und die vorher ausgewählte VM und das PCI-Gerät übernehmen. Leider habe ich nicht mal einen Ansatz wie das funktionieren könnte.

Ich bin natürlich auch offen für andere Lösungsvorschläge, hauptsache durch das "Start" drücken wird das PCI-Gerät in die VM übernommen (Discrete Device Assignment).

Mein Skript sieht derzeit 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.StartPosition = "CenterScreen"  
$objForm.Size = New-Object System.Drawing.Size(400,400)
$objForm.Text = "Discrete Device Assignment"  

#VM Namen eingeben

$objForm.Add_Load({
    $objComboBox1.Items.AddRange((Get-VM | select -Expand Name))
})
$objComboBox1.Add_SelectedIndexChanged({
    [System.Windows.Forms.MessageBox]::Show((Get-VM -Name $objComboBox1.SelectedItem | fl * | out-string))
})
$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)

$objComboBox1 = New-Object System.Windows.Forms.Combobox
$objComboBox1.Location = New-Object System.Drawing.Size(50,100)
$objComboBox1.Size = New-Object System.Drawing.Size(250,50)
$objComboBox1.Height = 70
$objForm.Controls.Add($objComboBox1)
$objForm.TopMost = $True


#PCI-Gerät Location Path angeben

$objForm.Add_Load({
    $objComboBox2.Items.AddRange((gwmi cim_controller | select -Expand Name))
})
$objComboBox2.Add_SelectedIndexChanged({
    [System.Windows.Forms.MessageBox]::Show((gwmi cim_controller | select -Expand Name))
})

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,150)
$objLabel.Size = New-Object System.Drawing.Size(300,50)
$objLabel.Text = "Wählen sie ein PCI-Gerät aus:"  
$objForm.Controls.Add($objLabel)

$objComboBox2 = New-Object System.Windows.Forms.Combobox
$objComboBox2.Location = New-Object System.Drawing.Size(50,200)
$objComboBox2.Size = New-Object System.Drawing.Size(300,50)
$objComboBox2.Height = 70
$objForm.Controls.Add($objComboBox2)
$objForm.TopMost = $True

#Abbrechen-Button

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(250,300)
$CancelButton.Size = New-Object System.Drawing.Size(75,25)
$CancelButton.Text = "Abbrechen"  
$CancelButton.Name = "Abbrechen"  
$CancelButton.DialogResult = "Cancel"  
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

#Start-Button

$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Size(50,300)
$StartButton.Size = New-Object System.Drawing.Size(75,25)
$StartButton.Text = "Start"  
$StartButton.Add_Click(
    {
        $Script:x = $objComboBox1.selectedItems
        $objComboBox1.Text = $Script:x | out-string
    })


$objForm.Controls.Add($StartButton)

#DDA Befehle ausführen
#$DDACommands = New-Object System.Windows.SystemCommands

[void] $objForm.ShowDialog()

Content-Key: 367763

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

Printed on: April 24, 2024 at 01:04 o'clock

Member: colinardo
Solution colinardo Mar 12, 2018, updated at Mar 15, 2018 at 10:28:45 (UTC)
Goto Top
Servus,
alles was du brauchst findest du hier:
Passing through devices to Hyper-V VMs by using discrete device assignment

Im Eventhandler des Buttons dürftest du also hiermit klar kommen:
$vm = Get-VM $objCombobox1.SelectedItem
$device = $objCombobox2.SelectedItem
$locationPath = Get-PnpDevice -PresentOnly | ?{$_.FriendlyName -eq $device} | Get-PnpDeviceProperty -KeyName DEVPKEY_Device_LocationPaths | select -Expand Data
if ($locationPath){
    Dismount-VMHostAssignableDevice -LocationPath $locationPath -Force -Verbose
    Add-VMAssignableDevice -VM $vm -LocationPath $locationPath -Verbose
}else{
    [System.Windows.Forms.MessageBox]::Show("Device not found.")  
}

Grüße Uwe
Mitglied: 135531
135531 Mar 13, 2018 at 15:20:45 (UTC)
Goto Top
Der Link war Goldwert, Danke dir !