Powershell GUID menu Help

Hello All,

i tried to create a dynamic menu based on a Linux output.

To create the menu works fine but i cant change the click-action per button

[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$WindowTitle = '*********'
$Title = '********'
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")

$Password = *******
$User = ******
$ComputerName = ********
$Command = "SSH Command"


$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)

$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials -AcceptKey

$Results = Invoke-SSHCommand -SessionId 0 -Command "arkmanager list-instances | cut -f1 -d ':' --output-delimiter=' ' " 
echo $Results|select -ExpandProperty Output | Format-List  | Out-File -FilePath c:\test\Process.txt



# Main menu
$MenuBox = New-Object System.Windows.Forms.Form
$MenuBox.Size = New-Object System.Drawing.Size @(650,450)
$MenuBox.Text = $WindowTitle
$MenuBox.StartPosition = 'CenterScreen'
$MenuBox.MaximizeBox = $False
$MenuBox.AutoSize = $False
$MenuBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$MenuBox.Icon = $Icon
$MenuBox.BackgroundImage = $Image
$MenuBox.BackgroundImageLayout = 'None' # None, Tile, Center, Stretch, Zoom

# Exit Button
$ExitButton = New-Object System.Windows.Forms.Button
$ExitButton.Location = New-Object System.Drawing.Size(540,370)
$ExitButton.Size = New-Object System.Drawing.Size(75,23)
$ExitButton.Text = 'Exit'
$ExitButton.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$ExitButton.Add_Click({
    $MenuBox.Close()
})

# Main menu Header Text
$MenuHeader = New-Object System.Windows.Forms.Label
$MenuHeader.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,1)
$MenuHeader.Location = New-Object System.Drawing.Size(118,20)
$MenuHeader.Size = New-Object System.Drawing.Size(380,40) 
$MenuHeader.Text = $Title
$MenuHeader.BackColor = 'Transparent'
$MenuHeader.TextAlign = [System.Drawing.ContentAlignment]::TopCenter
$MenuBox.Controls.Add($MenuHeader)

# Main menu
$BoxLabel = New-Object System.Windows.Forms.Label
$BoxLabel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",10,0,3,1)
$BoxLabel.Location = New-Object System.Drawing.Size(10,60) 
$BoxLabel.Size = New-Object System.Drawing.Size(680,20) 
$BoxLabel.Text = 'Select the category:'
$BoxLabel.BackColor = 'Transparent'
$MenuBox.Controls.Add($BoxLabel)

# AD menu
$ADBox = New-Object System.Windows.Forms.Form
$ADBox.Size = New-Object System.Drawing.Size @(650,450)
$ADBox.Text = $WindowTitle
$ADBox.StartPosition = 'CenterParent'
$ADBox.AutoSize = $False
$ADBox.MaximizeBox = $False
$ADBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$ADBox.Icon = $Icon
$ADBox.BackgroundImage = $Image

# AD menu
$Backup = New-Object System.Windows.Forms.Form
$Backup.Size = New-Object System.Drawing.Size @(650,450)
$Backup.Text = $WindowTitle
$Backup.StartPosition = 'CenterParent'
$Backup.AutoSize = $False
$Backup.MaximizeBox = $False
$Backup.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Backup.Icon = $Icon
$Backup.BackgroundImage = $Image


# install menu
$install = New-Object System.Windows.Forms.Form
$install.Size = New-Object System.Drawing.Size @(650,450)
$install.Text = $WindowTitle
$install.StartPosition = 'CenterParent'
$install.AutoSize = $False
$install.MaximizeBox = $False
$install.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$install.Icon = $Icon
$install.BackgroundImage = $Image
#######Restart Menü#########

# Main menu button 1



# Main menu button 2
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(62,100)
$Button2.Size = New-Object System.Drawing.Size(500,30)
$Button2.Add_Click({
    # Call Sub menu 1
    $MenuBox.Close()
    $MenuBox.Dispose()
    $ADBox.Topmost = $True
    $ADBox.Add_Shown({$ADBox.Activate()})
    [void] $ADBox.ShowDialog()
})
$Button2.Text = 'Server Restart'
$Button2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$MenuBox.Controls.Add($Button2)

$MenuBox.Controls.Add($ExitButton)




$form=New-Object System.Windows.Forms.Form
function function-to-run{
    $this.ForeColor=$this.Tag
    write-host $this.Tag -fore  $this.Tag -back white
}

$arguments = get-content c:\test\Process.txt
$i=0
foreach ($arg in $arguments){
    $b=New-Object System.Windows.Forms.Button
    $b.Name=$arg
     $b.location="62,$(40*++$I)"
     $b.Size = New-Object System.Drawing.Size(500,30)
     $b.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
     $b.Text="$arg"
     $b.Tag=$arg
     $b.Add_Click($Button_Click)
     $button_click = {write-host "hi $arg"}

    $form.controls.add($b)
    $ADBox.Controls.Add($b)
    $ADBox.Controls.Add($ExitButton1)

    }
# Show Menu
$MenuBox.Topmost = $True
$MenuBox.Add_Shown({$MenuBox.Activate()})
[void] $MenuBox.ShowDialog()

Hi,
You’re talking about this part ?

$arguments = get-content c:\test\Process.txt
$i=0
foreach ($arg in $arguments){
    $b=New-Object System.Windows.Forms.Button
    $b.Name=$arg
     $b.location="62,$(40*++$I)"
     $b.Size = New-Object System.Drawing.Size(500,30)
     $b.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
     $b.Text="$arg"
     $b.Tag=$arg
     $b.Add_Click($Button_Click)
     $button_click = {write-host "hi $arg"}

    $form.controls.add($b)
    $ADBox.Controls.Add($b)
    $ADBox.Controls.Add($ExitButton1)

    }

Could you please show content of c:\test\Process.txt ?

exactly. I modified it allready

$arguments = get-content c:\test\Process.txt
$i=0

foreach ($arg in $arguments){
    $b=New-Object System.Windows.Forms.Button
    $b.Name=$arg
    $b.location="62,$(40*++$I)"
    $b.Size = New-Object System.Drawing.Size(500,30)
    $b.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
    $b.Text=$arg
    $Button_Click1 = {write-host "Restart $arg"}
    $b.Add_Click($Button_Click1)
    $ADBox.Controls.Add($b)
    $ADBox.Controls.Add($ExitButton2)

    }

c:\test\Process.txt this is a temp file.
each time the script is used, the file is rewritten

  @theisland
  @extinction
  @fjordur
  @gen2
  @scorchedearth

No matter which button I press, I only get this output

Restart   @scorchedearth
Restart   @scorchedearth
Restart   @scorchedearth

You can use the GetNewClosure() method of the scriptblock.

$button_click = {write-host "hi $arg"}.GetNewClosure()
1 Like

Works for me thanks alot