135531
Goto Top

PowerShell Skript Befehle richtig zuweisen(definieren)

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. Wenn ich die VM und das PCI-Gerät auswähle werden die Befehle an der VM richtig ausgeführt (VM herunterfahren usw.).
Die Befehle an dem PCI-Gerät jedoch funktionieren nicht vernünftig. Das Ziel ist das, in der Combobox ausgewählte, PCI-Gerät zu deaktivieren. Allerdings versucht das Skript Alle PCI-Geräte zu deaktivieren. Leider habe ich zu wenig Erfahrung um den Fehler selbst zu finden. Google brachte die erhoffte Erkenntnis leider auch nicht...

In diesen Zeilen muss der Fehler liegen :
#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(
    {
        $vm = $objComboBox1.SelectedItem
        $device = $objComboBox2.SelectedItem
        $dev = (Get-PnpDevice -PresentOnly).Where{ $_.InstanceId }
        $LocationPath = Get-PnpDevice -presentOnly | ?{$_.Name -eq $device} | Get-PnpDeviceProperty -KeyName DEVPKEY_Device_LocationPaths | select -Expand Data
        $vmName = Get-VM | ?{$_.Name -eq $vm}
        if ($vmName){
            Stop-VM -Name $vm -TurnOff -Force
            Set-VM -Name $vm -AutomaticStopAction TurnOff
            Set-VM -GuestControlledCacheTypes $true -Name $vm
            Set-VM -LowMemoryMappedIoSpace 3Gb -Name $vm
            Set-VM -HighMemoryMappedIoSpace 33280Mb -Name $vm
        }else{ 
        if ($LocationPath){
            Disable-PnpDevice -InstanceId $dev.InstanceId -Confirm:$false
            #Dismount-VMHostAssignableDevice -Force -LocationPath $LocationPath
            #Add-VMAssignableDevice -LocationPath $LocationPath
        }else{
            [System.Windows.Forms.MessageBox]::Show("Device not found.")  
    }}})

$objForm.Controls.Add($StartButton)

Dies ist der ganze Code :
[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))
})

$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))
})

$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(250,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()
    [System.Windows.Forms.MessageBox]::Show(("Der Vorgang wurde abgebrochen"))  
})
$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(
    {
        $vm = $objComboBox1.SelectedItem
        $device = $objComboBox2.SelectedItem
        $dev = (Get-PnpDevice -PresentOnly).Where{ $_.InstanceId }
        $LocationPath = Get-PnpDevice -presentOnly | ?{$_.Name -eq $device} | Get-PnpDeviceProperty -KeyName DEVPKEY_Device_LocationPaths | select -Expand Data
        $vmName = Get-VM | ?{$_.Name -eq $vm}
        if ($vmName){
            Stop-VM -Name $vm -TurnOff -Force
            Set-VM -Name $vm -AutomaticStopAction TurnOff
            Set-VM -GuestControlledCacheTypes $true -Name $vm
            Set-VM -LowMemoryMappedIoSpace 3Gb -Name $vm
            Set-VM -HighMemoryMappedIoSpace 33280Mb -Name $vm
        }else{ 
        if ($LocationPath){
            Disable-PnpDevice -InstanceId $dev.InstanceId -Confirm:$false
            #Dismount-VMHostAssignableDevice -Force -LocationPath $LocationPath
            #Add-VMAssignableDevice -LocationPath $LocationPath
        }else{
            [System.Windows.Forms.MessageBox]::Show("Device not found.")  
    }}})

$objForm.Controls.Add($StartButton)

# KeyDown Event (Enter and Escape)

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$objForm.DialogResult = "OK";$objForm.Close()} })  
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")  
{
$objForm.DialogResult = "Cancel";$objForm.Close()}  
    [System.Windows.Forms.MessageBox]::Show(("Der Vorgang wurde abgebrochen"))  
})


[void] $objForm.ShowDialog()

Es wäre Klasse wenn mir, da jemand von euch helfen kann..

LG Jannik

Content-Key: 368198

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

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

Member: SlainteMhath
SlainteMhath Mar 15, 2018 at 10:31:42 (UTC)
Goto Top
Moin,

sieht für mich so aus, also würdest du in Zeile 11 ($dev= ...) nicht auf das ausgewählte Gerät ($device) abfragen, sondern nur auf die InstanceID (bzw. deren vorhandensein)

Später schaltest du dann alles $dev ab.

lg,
Slainte
Mitglied: 135531
135531 Mar 15, 2018 at 10:37:07 (UTC)
Goto Top
Ok, danke.

Hast du da vielleicht einen Lösungsvorschlag für mich ?
Member: colinardo
Solution colinardo Mar 15, 2018 updated at 10:42:37 (UTC)
Goto Top
Zitat von @135531:

Ok, danke.

Hast du da vielleicht einen Lösungsvorschlag für mich ?
Öhm, einfach mal in meinen letzten Kommentar schauen, wozu schreibe ich das eigentlich ...
PowerShell GUI Skript ausführen durch "Start" Knopf
$dev = Get-PnpDevice -PresentOnly | ?{$_.FriendlyName -eq $device}
Du würfelst oben alles durcheinander face-smile. Copy n' Paste führt dich nicht ans Ziel..., verstehe was du schreibst dann wirst du auch Erfolg haben.

Grüße Uwe
Mitglied: 135531
135531 Mar 15, 2018 at 11:02:22 (UTC)
Goto Top
Hallo Uwe,

ich bemühe mich "zu verstehen was ich schreibe". Nur leider funktioniert das nicht immer so wie gewollt...

Was genau würfel ich denn "Oben" durcheinander ?

LG
Jannik
Member: colinardo
Solution colinardo Mar 15, 2018 at 11:09:44 (UTC)
Goto Top
Zitat von @135531:
Was genau würfel ich denn "Oben" durcheinander ?
Wenn du dir meine und deine Zeilen mal genau ansiehst verstehst du was und warum.
Das sind reine Flüchtigkeitsfehler.