135531
Mar 15, 2018, updated at 10:14:19 (UTC)
1587
5
0
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 :
Dies ist der ganze Code :
Es wäre Klasse wenn mir, da jemand von euch helfen kann..
LG Jannik
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
Please also mark the comments that contributed to the solution of the article
Content-Key: 368198
Url: https://administrator.de/contentid/368198
Printed on: April 25, 2024 at 16:04 o'clock
5 Comments
Latest comment
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 ...Ok, danke.
Hast du da vielleicht einen Lösungsvorschlag für mich ?
PowerShell GUI Skript ausführen durch "Start" Knopf
$dev = Get-PnpDevice -PresentOnly | ?{$_.FriendlyName -eq $device}
Grüße Uwe