it-guy77
Goto Top

Erstellen eines GUI in Powershell und das nutzen vom Funktionen mit Usereingabe

Hallo zusammen,

folgendes Problem: Ich arbeite derzeit an einem GUI in Powershell für eine kleine Sammlung von Skripten um sie meinen Kollegen zugänglich zu machen.
Ein GUI mit Menüleiste habe ich. Die Probleme habe ich dabei die Funktionalität ins GUI zu bringen. In folgendem Button ist der Output NULL und ich verstehe nicht wieso...

$testButton.Add_Click({

        $computerName = $testFeld.Text    
    
        $users = Get-WmiObject -Class win32_process -ComputerName $computerName | Where-Object{ $_.Name -eq "explorer.exe" } | ForEach-Object{ ($_.GetOwner()).Domain + "\" + ($_.GetOwner()).User; }  
        
        Write-Host $users
    })

Hat jemand da eine Idee oder könnte mir erklären wie das zusammenhängt?

Hier einmal der gesamte Code:

# Install .Net Assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[Windows.Forms.Application]::EnableVisualStyles()

$font = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Regular)  

################################################################## Variables 
$mainForm         = New-Object System.Windows.Forms.Form
$menuMain         = New-Object System.Windows.Forms.MenuStrip
$menuFile         = New-Object System.Windows.Forms.ToolStripMenuItem
$menuPSTool1      = New-Object System.Windows.Forms.ToolStripMenuItem
$menuExit         = New-Object System.Windows.Forms.ToolStripMenuItem
$statusStrip      = New-Object System.Windows.Forms.StatusStrip
$statusLabel      = New-Object System.Windows.Forms.ToolStripStatusLabel

################################################################## Icons
$code = @"  
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace System
{
	public class IconExtractor
	{

	 public static Icon Extract(string file, int number, bool largeIcon)
	 {
	  IntPtr large;
	  IntPtr small;
	  ExtractIconEx(file, number, out large, out small, 1);
	  try
	  {
	   return Icon.FromHandle(largeIcon ? large : small);
	  }
	  catch
	  {
	   return null;
	  }

	 }
	 [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
	 private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);

	}
}
"@  
Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing

# Extract PowerShell Icon from PowerShell Exe
$iconPS   = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)

################################################################## Main Form Setup
$mainForm.Icon            = $iconPS
$mainForm.MainMenuStrip   = $menuMain
$mainForm.Height          = 600
$mainForm.Width           = 800
$mainForm.StartPosition   = "CenterScreen"  
$mainForm.TopMost         = $true
$mainForm.Text            = "Maske PowerBox"  
$mainForm.Font            = $font
$mainForm.Controls.Add($menuMain)

################################################################## Menue Parts

# Main Menu Bar
[void]$mainForm.Controls.Add($menuMain)

# Menu Options - File
$menuFile.Text = "&File"  
[void]$menuMain.Items.Add($menuFile)

# Menu Options - File / Exit
$menuPSTool1.Image        = [System.IconExtractor]::Extract("shell32.dll", 76, $true)  
$menuPSTool1.ShortcutKeys = "Control, Q"  
$menuPSTool1.Text = "Get-User"  
$menuPSTool1.Add_Click({get-LoggedInUserRemotePC})
[void]$menuFile.DropDownItems.Add($menuPSTool1)

# Menu Options - File / Exit
$menuExit.Image        = [System.IconExtractor]::Extract("shell32.dll", 10, $true)  
$menuExit.ShortcutKeys = "Control, X"  
$menuExit.Text = "&Exit"  
$menuExit.Add_Click({$mainForm.Close()})
[void]$menuFile.DropDownItems.Add($menuExit)

################################################################## Status Bar
# Status Bar & Label
[void]$statusStrip.Items.Add($statusLabel)
$statusLabel.AutoSize  = $true
$statusLabel.Text      = "Ready..."  
$mainForm.Controls.Add($statusStrip)

################################################################## functions
function get-LoggedInUserRemotePC {

    $statusLabel.Text = "Wait for input..."  

    $testLabel  = New-Object System.Windows.Forms.Label
    $testFeld   = New-Object System.Windows.Forms.TextBox
    $testButton = New-Object System.Windows.Forms.Button

    $testLabel.Text = "Computername eingeben:"  
    $testLabel.Location = New-Object System.Drawing.Size(40,44)
    $testLabel.Size = New-Object System.Drawing.Size(200,20)
    [void]$mainForm.Controls.Add($testLabel)

    $testFeld.Text = "BlaBlaBla!"  
    $testFeld.Location = New-Object System.Drawing.Size(40,80)
    $testFeld.Size = New-Object System.Drawing.Size(200,20)
    $testFeld.Multiline = $true
    [void]$mainForm.Controls.Add($testFeld)

    $testButton.Text = "Get-User"  
    $testButton.Location = New-Object System.Drawing.Size(240,40)
    $testButton.Autosize = $true
    $testButton.Add_Click({

        $computerName = $testFeld.Text    
    
        $users = Get-WmiObject -Class win32_process -ComputerName $computerName | Where-Object{ $_.Name -eq "explorer.exe" } | ForEach-Object{ ($_.GetOwner()).Domain + "\" + ($_.GetOwner()).User; }  
        
        Write-Host $users
    })
    [void]$mainForm.Controls.Add($testButton) 

    $statusLabel.Text = "Ready..."  
}
# Show Main Form
[void] $mainForm.ShowDialog()

Content-Key: 340513

Url: https://administrator.de/contentid/340513

Printed on: April 23, 2024 at 23:04 o'clock

Member: SeriousEE
Solution SeriousEE Jun 13, 2017 updated at 12:25:47 (UTC)
Goto Top
Hi,

Das hat mit den Scopes zu tun Sapien

Lösungsmöglichkeit wäre das testFeld nicht in der Funktion zu erstellen, sondern 1x im "Main" Bereich:

...
$testFeld   = New-Object System.Windows.Forms.TextBox

function get-LoggedInUserRemotePC {

    $statusLabel.Text = "Wait for input..."  

    $testLabel  = New-Object System.Windows.Forms.Label

    $testButton = New-Object System.Windows.Forms.Button

    $testLabel.Text = "Computername eingeben:"  
    $testLabel.Location = New-Object System.Drawing.Size(40,44)
    $testLabel.Size = New-Object System.Drawing.Size(200,20)
    [void]$mainForm.Controls.Add($testLabel)

    $testFeld.Text = "BlaBlaBla!"  
    $testFeld.Location = New-Object System.Drawing.Size(40,80)
    $testFeld.Size = New-Object System.Drawing.Size(200,20)
    $testFeld.Multiline = $true

    $testButton.Text = "Get-User"  
    $testButton.Location = New-Object System.Drawing.Size(240,40)
    $testButton.Autosize = $true
    $testButton.Add_Click({
	    $computerName = $testFeld.Text
        $users = Get-WmiObject -Class win32_process -ComputerName $computerName | Where-Object{ $_.Name -eq "explorer.exe" } | ForEach-Object{ ($_.GetOwner()).Domain + "\" + ($_.GetOwner()).User; }        
        Write-Host $users
    })
    [void]$mainForm.Controls.Add($testButton) 
    [void]$mainForm.Controls.Add($testFeld)
    $statusLabel.Text = "Ready..."  
}
# Show Main Form
[void] $mainForm.ShowDialog()
Member: IT-Guy77
IT-Guy77 Jun 13, 2017 at 12:44:53 (UTC)
Goto Top
Danke für den Denkanstoß. Interessanter Artikel und das hat mein Problem auch gelöst. Vielen vielen Dank für die Hilfe. =)

Grüße vom IT-Guy77