Hello, I have been working on a powershell gui app that gets all the session for putty that I have saved in the registry and creates a menu for them. I am able to get the list and assign it to a list object, but also want a button menu that has a clickable button for each session. I am able to generate and name the buttons, but when I try to assign the click cmdlet to it, it assigns the same value to every button, which makes it so every button opens the same session, the last button created. All the variables within the cmdlet, except for $i, are passed into the code from a previous function. Code is below:
Function Menu_Select {
$objForm3 = New-Object System.Windows.Forms.Form
$objForm3.Text = "Operator Menu"
$objForm3.Size = New-Object System.Drawing.Size(600,1000)
$objForm3.StartPosition = "CenterScreen"
$w=10
$v=10
get-childitem -Path HKCU:\Software\SimonTatham\PuTTY\Sessions -Name | Foreach-object {$y += $_}
ForEach($i in $y){
$w+=30
if ($w -gt 800)
{
$v+=200
$w=10
}
$z = New-Object System.Windows.Forms.Button
$z.Location = New-Object System.Drawing.Size($v,$w)
$z.Size = New-Object System.Drawing.Size(175,23)
$z.Text = "$i"
$z.Add_Click({& 'C:\PuTTY\putty.exe' $ld $i $login $user $pw $pass})
$objForm3.Controls.Add($z)
}
$ReturnButton = New-Object System.Windows.Forms.Button
$ReturnButton.Location = New-Object System.Drawing.Size(150,920)
$ReturnButton.Size = New-Object System.Drawing.Size(200,23)
$ReturnButton.Text = "Return to Main"
$ReturnButton.Add_Click({$objForm3.Close()})
$objForm3.Controls.Add($ReturnButton)
$objForm3.Add_Shown({$objForm3.Activate()})
$objForm3.ShowDialog()
}