Granatelbart 0 Geschrieben 20. Juni 2022 Melden Geschrieben 20. Juni 2022 Guten Tag, ich versuche aktuell ein dynamisches Menü aus einer TXT zu erstellen. Das Menü baut sich soweit korrekt zusammen jedoch passen sich die Click-Button nicht an. [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 "****** 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() Zitieren
BOfH_666 585 Geschrieben 20. Juni 2022 Melden Geschrieben 20. Juni 2022 (bearbeitet) Max, willkommen im MCSEboard. In Zukunft Board-Regel Nr:19 bitte beachten! https://www.mcseboard.de/terms/ https://forums.powershell.org/t/powershell-guid-menu-help/19648 bearbeitet 20. Juni 2022 von BOfH_666 Zitieren
MurdocX 976 Geschrieben 20. Juni 2022 Melden Geschrieben 20. Juni 2022 Hallo, kurz vorweg mit einfachen Handgriffen ist Dir nicht geholfen. Es sind einfach zu viele Fehler im Code. Daraus schlussfolgere ich, dass du vermutlich noch viel lernen wirst. Am 20.6.2022 um 16:48 schrieb Granatelbart: $Title = '********' <- JA $Password = ******* <- NEIN Mehr Setze Strings immer in ' oder ". Am 20.6.2022 um 16:48 schrieb Granatelbart: $SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials -AcceptKey $Command = "SSH Command" Mehr .. sind Variablen die NIE genutzt werden. Am 20.6.2022 um 16:48 schrieb Granatelbart: $b.Add_Click($Button_Click) $button_click = {write-host "hi $arg"} Mehr Hier wird eine Variable ausgelesen, bevor sie überhaupt existiert. Am 20.6.2022 um 16:48 schrieb Granatelbart: $ADBox.Controls.Add($ExitButton1) Mehr .. das Gleiche hier. Es gibt kein $ExitButton1 Am 20.6.2022 um 16:48 schrieb Granatelbart: $install.BackgroundImage = $Image Mehr ... und hier.. Am 20.6.2022 um 16:48 schrieb Granatelbart: function function-to-run{ $this.ForeColor=$this.Tag write-host $this.Tag -fore $this.Tag -back white } Mehr ... auch diese Funktion wird definiert, aber niemals aufgerufen. Wenn ich Dir einen Tipp geben darf. Versuche es mal mit Visual Studio Code und den PowerShell Erweiterungen. Die werden Dir deine Fehler gleich zeigen und helfen dir mit Intellisense. Unterhalb möchte ich Dir zeigen, wie ein Script mit einer Funktion für Buttons aussehen kann. Fast Identisch kann das auch für deine Labels gebaut werden. Versuche Dich einfach mal. #requires -Version 3.0 [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') [void] [System.Windows.Forms.Application]::EnableVisualStyles() # # Functions # function CreateButton { [OutputType([System.Windows.Forms.Button])] param ( [Parameter(Mandatory)] [System.Drawing.Size] $Size, [Parameter(Mandatory)] [System.Drawing.Size] $Location, [Parameter(Mandatory)] [string] $Text, [Parameter(Mandatory)] [scriptblock] $ClickEvent ) $myButton = New-Object -TypeName System.Windows.Forms.Button $myButton.Location = $Location $myButton.Size = $Size $myButton.Text = $Text $myButton.Font = New-Object -TypeName System.Drawing.Font -ArgumentList ('Microsoft Sans Serif', 11, 0, 3, 1) $myButton.Add_Click($ClickEvent) return $myButton } Zitieren
Granatelbart 0 Geschrieben 20. Juni 2022 Autor Melden Geschrieben 20. Juni 2022 (bearbeitet) ja ich muss das Skript noch etwas aufräumen ist mir bewusst (mit unter weil ich am testen bin) Diese genannten "Fehler" sind aber auch nicht mein derzeitiges Problem Setze Strings immer in ' oder ". <<<< Das weiß ich wohl ist halt beim ersetzen durch * entfernt worden [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') [void] [System.Windows.Forms.Application]::EnableVisualStyles() $WindowTitle = 'Certified-Bots Arkmanager' $Title = 'Certified-Bots Arkmanager' $Password = "++++++++++" $User = "+++++++++" $ComputerName = "+++++++++" $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.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() }) # Exit Button $ExitButton1 = New-Object System.Windows.Forms.Button $ExitButton1.Location = New-Object System.Drawing.Size(540,370) $ExitButton1.Size = New-Object System.Drawing.Size(75,23) $ExitButton1.Text = 'Exit' $ExitButton1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1) $ExitButton1.Add_Click({ $Backup.Close() }) # Exit Button $ExitButton2 = New-Object System.Windows.Forms.Button $ExitButton2.Location = New-Object System.Drawing.Size(540,370) $ExitButton2.Size = New-Object System.Drawing.Size(75,23) $ExitButton2.Text = 'Exit' $ExitButton2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1) $ExitButton2.Add_Click({ $ADBox.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 # Backup 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 #######Restart Menü######### $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($ExitButton1) $form=New-Object System.Windows.Forms.Form $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) } #############BACKUP########### # Main menu button 2 $Button1 = New-Object System.Windows.Forms.Button $Button1.Location = New-Object System.Drawing.Size(62,140) $Button1.Size = New-Object System.Drawing.Size(500,30) $Button1.Add_Click({ # Call Sub menu 1 $MenuBox.Close() $MenuBox.Dispose() $Backup.Topmost = $True $Backup.Add_Shown({$Backup.Activate()}) [void] $Backup.ShowDialog() }) $Button1.Text = 'Server Backup' $Button1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1) $MenuBox.Controls.Add($Button1) $MenuBox.Controls.Add($ExitButton) $form=New-Object System.Windows.Forms.Form $arguments1 = get-content c:\test\Process.txt $i=0 foreach ($arg1 in $arguments1){ $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=$arg1 $b.Tag=$arg1 $button_click2 = {write-host "Backup $arg1"} $b.Add_Click($Button_Click2) $form.controls.add($b) $Backup.Controls.Add($b) $Backup.Controls.Add($ExitButton1) $Backup.Controls.Add($BackButton) } # Show Menu $MenuBox.Topmost = $True $MenuBox.Add_Shown({$MenuBox.Activate()}) [void] $MenuBox.ShowDialog() Am 20.6.2022 um 19:47 schrieb Granatelbart: 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) } Mehr Mein Problem liegt eher in diesem Bereich. Ich generiere immer nur das Gleiche Event für die unterschiedlichen Button bearbeitet 20. Juni 2022 von Granatelbart Zitieren
Empfohlene Beiträge
Schreibe einen Kommentar
Du kannst jetzt antworten und Dich später registrieren. Falls Du bereits ein Mitglied bist, logge Dich jetzt ein.