it-guy77
Goto Top

Probleme beim aufrufen von Funktionen Powershell GUI

Hallo zusammen,

ich habe folgendes Problem bei meinem Skript: Ich habe derzeit unter File 2 Funktionen eingebunden. Beides funktioniert unabhängig voneinander einwandfrei. Wenn ich jedoch die eine Funktion aufgerufen und ausgeführt habe und dann die andere Funktion ausführen möchte, hat er noch die Funktionen geladen bzw. auf der Form gezeichnet. Ich vermute das ich irgendwie die Funktionen vorher löschen muss bevor er die neue auf die Form zeichnen kann. Anscheinend wird diese nicht einfach überschrieben.

Kann mir jemand aufzeigen wie man das macht oder einen Denkanstoß geben?

Grüße vom IT-Guy77

Hier einmal der gesamte Code:

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

################################################################## Variables 
###Main Window###
$mainForm         = New-Object System.Windows.Forms.Form

###Menu bar###
$menuMain         = New-Object System.Windows.Forms.MenuStrip

###Menu###
$menuFile         = New-Object System.Windows.Forms.ToolStripMenuItem
$menuPrograms     = New-Object System.Windows.Forms.ToolStripMenuItem

###Items of $menuFile###
$menuPSTool1      = New-Object System.Windows.Forms.ToolStripMenuItem
$menuPSTool2      = New-Object System.Windows.Forms.ToolStripMenuItem
$menuExit         = New-Object System.Windows.Forms.ToolStripMenuItem

###Items of $menuPrograms###
$menuPSTool3      = New-Object System.Windows.Forms.ToolStripMenuItem

###Tool1###
$tool1TextBox      = New-Object System.Windows.Forms.TextBox
$tool1Label        = New-Object System.Windows.Forms.Label
$tool1Button       = New-Object System.Windows.Forms.Button

###Tool2###
$tool2TextBox      = New-Object System.Windows.Forms.TextBox
$tool2Label        = New-Object System.Windows.Forms.Label
$tool2Button       = New-Object System.Windows.Forms.Button

###Status###
$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)

# Set font style and size for entire gui
$font = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Regular)  

################################################################## 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)

################################################################## Menu

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

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

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

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

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

# Menu Options - File / update-GlobalAddressBook
$menuPSTool3.Image = [System.IconExtractor]::Extract("shell32.dll", 13, $true)  
$menuPSTool3.ShortcutKeys = "Control, E"  
$menuPSTool3.Text = "Update-GlobalAddressBook"  
$menuPSTool3.Add_Click({update-GlobalAddressBook})
[void]$menuPrograms.DropDownItems.Add($menuPSTool3)

# 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
[void]$statusStrip.Items.Add($statusLabel)
$statusLabel.AutoSize  = $true
$statusLabel.Text      = "Ready..."  
$mainForm.Controls.Add($statusStrip)

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

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

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

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

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

        $computerName = $tool1TextBox.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($tool1Button)
}# get-LoggedInUserRemotePC

function get-FreeSpaceOfRemotePC {

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

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

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

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

        $computerName = $tool2TextBox.Text    
    
        $freeSpace = gwmi Win32_LogicalDisk -ComputerName $computerName | ForEach {$_.name, ($_.freespace / 1GB)}
        
        Write-Host $freeSpace

        $statusLabel.Text = "Ready..."  
    })
    [void]$mainForm.Controls.Add($tool2Button) 
}# get-FreeSpaceOfRemotePC

function update-GlobalAddressBook {

    #Globles Adressbuch aktualisieren um Änderungen direkt anzuzeigen
    $statusLabel.Text = "Updating Global Adressbook...";Start-Sleep 1  
    get-addresslist | update-addresslist
    $statusLabel.Text = "Updating...please wait 3...";Start-Sleep 1  
    get-globaladdresslist | update-globaladdresslist
    $statusLabel.Text = "Updating...please wait 2...";Start-Sleep 1  
    get-offlineaddressbook | update-offlineaddressbook
    $statusLabel.Text = "Updating...please wait 1...";Start-Sleep 1  
    $statusLabel.Text = "Ready..."  
}

# Show Main Form
[void] $mainForm.ShowDialog()

Content-Key: 340651

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

Printed on: April 18, 2024 at 08:04 o'clock

Mitglied: 133417
Solution 133417 Jun 14, 2017 updated at 15:01:09 (UTC)
Goto Top
Du hast erneut ein Scope-Problem denn deine Functions werden in einem eigenen "Scriptblock" ausgeführt, wenn du darin Variablen veränderst bekommt das deine Oberfläche nicht mit da die Variablen in einem anderen Scope definiert wurden.

Du solltest dir also dringend about_scopes
durchlesen, das ist essentiell für die Entwicklung mit Powershell und ist Pflichtlecktüre für jeden Entwickler! Lese es so oft durch bis du es verstanden hast, dann verstehst du auch dein Problem in deinem Skript.

Und Controls die man nicht mehr sehen will blendet man entweder mit einem Gruppen-Control aus oder man arbeitet mit einem Tab-Control mit mehreren Seiten welches man je nach Context aktiviert.
Das Erzeugen der Controls in einer Funktion halte ich für sehr ineffektiv. Lieber gleich auf einm Tab-Control alle Seiten fertig anlegen und dann über das Menü die jeweilgen Seiten (de)aktivieren.

Gruß
Member: IT-Guy77
IT-Guy77 Mar 13, 2018 at 07:19:04 (UTC)
Goto Top
Hat etwas länger gedauert. Aber das war es. Denke ich komme damit jetzt klar. Vielen Dank.

Grüße vom IT-Guy77