Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Trouble with Increasing a Variable Name in a Foreach Loop

$
0
0

Hi Everybody,

The Powershell Script/Programm im writing is for the purpose of deleting false Snapshots from hyper-v virtual machines.

At first everything looked good so far but know i think im running against a wall, i just cant seem to get it to work.

I want to create a Button (with winforms) for each value in my variable trough  a foreach Loop. My Problem is that i cant get the variable Name in the foreach Loop to increase or Change.

I tried several Things but None of them will work.

the Problem is that it Needs to be a different variable Name after each Loop or the script will only Show the last button of the Loop and not all of them.

$objForm = New-Object System.Windows.Forms.Form
$objForm.Size = New-Object System.Drawing.Size(700,900)

$objForm.KeyPreview = $true
#Stellt die Form so ein das Tastenanschläge erkannt werden

$objForm.Add_KeyDown({if ($_.Keycode -eq "Enter")
    {$x=$objTextbox.Text;$objForm.Close()}})
#Enter Tasten kann anstatt des OK Knopfes gedrückt werden

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})
#Escape Taste kann anstatt des abbrechen Knopfes gedrückt werden

#$OKButton = New-Object System.Windows.Forms.Button
#$OKButton.Location = New-Object System.Drawing.Size(200,20)
#$OKButton.Size = New-Object System.Drawing.Size(75,23)
#$OKButton.Text = "OK"
#$OKButton.Add_Click({$x=$objTextbox.Text;$objform.Close()})
#$objForm.Controls.Add($OKButton)

#Erstellt den OK Knopf für die Anwendung

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(290,20)
$CancelButton.Size = New-Object System.Drawing.Size(100,23)
$CancelButton.Text = "Abbrechen"
$CancelButton.Add_Click({$objform.Close()})
$objForm.Controls.Add($CancelButton)

#Erstellt den Abbrechen Knopf für die Anwendung

$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(10,20)
$objLabel0.Size = New-Object System.Drawing.Size(280,20)
$objLabel0.Text = "Hyper-V Snapshot Remover"
$objForm.Controls.Add($objLabel0)

#Erstellt das Label für die Anwendung

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,60)
$objLabel1.Size = New-Object System.Drawing.Size(500,20)
$objLabel1.Text = "Von welcher Virtuellen Maschine wollen Sie Snapshots entfernen?"
$objForm.Controls.Add($objLabel1)

#Erstellt die Anwendungsfrage

$VMs = Get-VM | Select-Object -exp Name

Foreach($VM in $VMs) {


$List = New-Object System.Windows.Forms.Button
$List.Location = New-Object System.Drawing.Size(10,100)
$List.Size = New-Object System.Drawing.Size(500,50)
$List.Text = "$VM"
$List.Add_Click({$objform.Close()})
$objForm.Controls.Add($List)

}

$objForm.Add_Shown({$objForm.Activate()})

[void] $objForm.ShowDialog()

I Hope you can help me, i just dont seem to get it to work. I would be very happy over any advice you could give me.

thx

  

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>