# Powershell GUID menu Help

**URL:** <https://forums.powershell.org/t/powershell-guid-menu-help/19648>\
**Category:** PowerShell Help\
**Created:** [June 20, 2022, 1:32pm UTC](https://forums.powershell.org/t/powershell-guid-menu-help/19648 "2022-06-20T13:32:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![granatelbart](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@granatelbart](https://forums.powershell.org/u/granatelbart)\
**Post date:** [June 20, 2022, 1:32pm UTC](https://forums.powershell.org/t/powershell-guid-menu-help/19648/1 "2022-06-20T13:32:11Z")

</div>

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

```auto
[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()

```

---

<div class="post-metadata">

**Author:** ![DejvDzi](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dejvdzi/32/2460_2.png) [@DejvDzi](https://forums.powershell.org/u/DejvDzi)\
**Post date:** [June 20, 2022, 8:01pm UTC](https://forums.powershell.org/t/powershell-guid-menu-help/19648/2 "2022-06-20T20:01:31Z")

</div>

Hi,  
You’re talking about this part ?

```auto
$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 ?

---

<div class="post-metadata">

**Author:** ![granatelbart](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@granatelbart](https://forums.powershell.org/u/granatelbart)\
**Post date:** [June 20, 2022, 8:14pm UTC](https://forums.powershell.org/t/powershell-guid-menu-help/19648/3 "2022-06-20T20:14:33Z")

</div>

exactly. I modified it allready

```auto
$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

```auto
  @theisland
  @extinction
  @fjordur
  @gen2
  @scorchedearth

```

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

```auto
Restart @scorchedearth
Restart @scorchedearth
Restart @scorchedearth

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [June 21, 2022, 6:21am UTC](https://forums.powershell.org/t/powershell-guid-menu-help/19648/4 "2022-06-21T06:21:46Z")

</div>

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

```auto
$button_click = {write-host "hi $arg"}.GetNewClosure()

```

---

<div class="post-metadata">

**Author:** ![granatelbart](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@granatelbart](https://forums.powershell.org/u/granatelbart)\
**Post date:** [June 21, 2022, 7:18am UTC](https://forums.powershell.org/t/powershell-guid-menu-help/19648/5 "2022-06-21T07:18:24Z")

</div>

Works for me thanks alot

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/powershell-guid-menu-help/19648/6 "2024-05-16T20:17:51Z")

</div>


