herbstengel
Goto Top

Powershell: Ist bei Programmierung einer GUI die Darstellung des Kommandofensters unbedingt notwendig?

Hallo liebe Forumsmitglieder,

ich programmiere eine Powershell-GUI und momentan erscheint immer parallel zur GUI die Powershell-Aus-/Eingabebox (Konsolefenster), siehe u.g. Bild:


Skriptaufruf mit

powershell.exe -ExecutionPolicy Bypass -File "C:\powershell_tests\testscript1.ps1" -param1 "Parameter eins"  

von Kommandozeile oder aus einen Batch.

Das Skript testscript1.ps1 selber:

[CmdletBinding()]
Param 
(
	[Parameter(Mandatory=$true)][string]$param1
)

BEGIN {
    if([string]::IsNullOrEmpty($param1))
    {
        $param1 = ""  
    }

    
}

PROCESS {
    #nested methodes

    Function Main()
    {
        Param 
        (
	        [Parameter(Mandatory=$true)][string]$param1
        )

        #Read-host "in Function MAIN() drin"  

        #Write-Host $param1

        #Read-host "gleich ruft Function MAIN() die Function Second() auf"  

        return Second -param1 $param1
    }

    Function Second()
    {
        Param 
        (
	        [Parameter(Mandatory=$true)][string]$param1
        )



        #Read-host "jetzt in Function Second() drin"  

        #Write-Host $param1

        if( (get-host).Name -eq "ConsoleHost")  
        {
            #######################################
            #   Konsolenfenster: Groesse anpassen #
            #######################################
            SetConsoleWindow -Width 70 -Height 15
    
            #ToDo: Fenster c:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe schliessen
    
            ############################################
            #  Funktion: Windows-Form                  #
            ############################################
            GenerateForm -param1 $param1
     
    
        }


<#
        ...
        ...
#>


    #Read-host "gleich aus Function Second() und wegen dem retrun in Funktion MAIN aus MAIN() und damit ganz aus dem Skript drausen"  

    }

    function GenerateForm()
    {
      
        Param 
        (
	        [Parameter(Mandatory=$true)][string]$param1
        )

        #region Import the Assemblies
        [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
        [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
        #endregion
    
        #region Generated Form Objects
        $objForm = New-Object System.Windows.Forms.Form
        $objLabel1 = New-Object System.Windows.Forms.Label
        $objTestButton = New-Object System.Windows.Forms.Button
    
        $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
        

        $handler_objTestButton_Click= 
        {
            $objForm.Close()
        }

        # Formular Lade-Funktion  
        $OnLoadForm_StateCorrection=
        {
            #Correct the initial state of the form to prevent the .Net maximized form issue
            $objForm.WindowState = $InitialFormWindowState
        }
    
        #----------------------------------------------
        #region Generated Form Code
    
        #$objLabel1.Location = New-Object System.Drawing.Size(100,20)
        $objForm.ClientSize = New-Object System.Drawing.Size(600,300)
        $objForm.DataBindings.DefaultDataSourceUpdateMode = 0
        $objForm.Backcolor="white"  
        $objForm.BackgroundImageLayout = 2
        $objForm.StartPosition = "CenterScreen"  
        $objForm.Name = "Form1"  
        $objForm.Text =  "PowerShell Testprojekt"  
    
        #Statusleiste
        $objStatusLeiste=New-Object System.Windows.Forms.StatusBar
        $objStatusLeiste.Text="Auswahl: Noch nicht getroffen"  
        $objStatusLeiste.Name="StatusLeiste"  
        $objForm.Controls.Add($objStatusLeiste)
    
        #Label1
        $objLabel1.Location = New-Object System.Drawing.Size(30,20)
        $objLabel1.Size = New-Object System.Drawing.Size(250,20)
        $objLabel1.Text = $param1
        $objForm.Controls.Add($objLabel1)

         #OTest Button 
        $objTestButton.Anchor = 9
        $objTestButton.DataBindings.DefaultDataSourceUpdateMode = 0
        $objTestButton.Location = New-Object System.Drawing.Size(500,250) #340
        $objTestButton.Size = New-Object System.Drawing.Size(75,23) 
        $objTestButton.Name = "objTestButton"  
        $objTestButton.TabIndex = 1
        $objTestButton.Text = "Close Form"  
        $objTestButton.UseVisualStyleBackColor = $True
        $objTestButton.add_Click($handler_objTestButton_Click)
        $objForm.Controls.Add($objTestButton)
    
        #endregion Generated Form Code
    
 
    

        #Save the initial state of the form
        $InitialFormWindowState = $objForm.WindowState
        #Init the OnLoad event to correct the initial state of the form
        $objForm.add_Load($OnLoadForm_StateCorrection)
    
    
        #Show the Form
        $objForm.ShowDialog()| Out-Null
    
    
    } #End Function

    function SetConsoleWindow()
    {
 

        param(
               [int]$Width,
                [int]$Height
        )   


        $WindowSize = $Host.UI.RawUI.WindowSize
        $WindowSize.Width  = [Math]::Min($Width, $Host.UI.RawUI.BufferSize.Width)
        $WindowSize.Height = $Height

        try{
            $Host.UI.RawUI.WindowSize = $WindowSize
        }
        catch [System.Management.Automation.SetValueInvocationException] 
        {
            $Maxvalue = ($_.Exception.Message |Select-String "\d+").Matches.Value  
            $WindowSize.Height = $Maxvalue
            $Host.UI.RawUI.WindowSize = $WindowSize
        }
    } #End Function
    

    $result = Main -param1 $param1

}
END {
    #final doings like return a value
    return $result
}

Mit Funktion SetConsoleWindow() wird dieses Fenster von den Standardmaßen auf eine gewünschte Größe "zurechtgebogen".

Während der Entwicklung ist diese Powershell-Aus-/Eingabebox (Konsolefenster) ganz nützlich, z.B. für Anzeigen mit Write-Host oder Test-Eingaben mit Read-Host. Sobald die GUI fertig ist, möchte ich diese Powershell-Aus-/Eingabebox nicht mehr beim Skriptaufruf haben. Bitte um Lösungsansätze. Ich denke vor Aufruf der Funktion Generate Form oder zu Beginn der Funktion Generate Form muss man was tun.

Viele Grüsse, Roger
powershell1

Content-Key: 540699

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

Printed on: April 24, 2024 at 00:04 o'clock

Member: ITvortex
Solution ITvortex Jan 29, 2020 at 09:34:44 (UTC)
Goto Top
# Hide PowerShell Console
Add-Type -Name Window -Namespace Console -MemberDefinition '  
[DllImport("Kernel32.dll")]  
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]  
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'  
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)



http://blog.dbsnet.fr/hide-powershell-console-from-a-gui

LG
Member: Bitboy
Bitboy Jan 29, 2020 at 09:36:26 (UTC)
Goto Top
Moin,

ungetestet but worth a try
Parameter -NonInteractive anhängen.
https://docs.microsoft.com/de-de/powershell/module/Microsoft.PowerShell. ...

Grüße
Member: mayho33
mayho33 Jan 29, 2020 updated at 11:23:39 (UTC)
Goto Top
Hi!

Du kannst dein Command in der CMD zusätzlich mit dem Parameter absetzen:

-windowstyle Hidden

powershell.exe -ExecutionPolicy Bypass -windowstyle Hidden -File "C:\powershell_tests\testscript1.ps1" -param1 "Parameter eins"  

Der Effekt ist, dass die CMD ausgeblendet wird.

Grüße!
Member: Herbstengel
Herbstengel Jan 29, 2020 at 11:51:57 (UTC)
Goto Top
Hallo,

Parameter -NonInteractive bzw. -windowstyle Hidden in den cmd-Aufruf einbauen brachte leider nicht den gewünschten Effekt.
Es wird zwar beides Mal zwar die Konsole geschlossen aber halt auch die GUI. Der Tipp von ITvortex traf ins Schwarze.
Einfach den Codeblock am Anfang der Funktion GenerateForm() eingebau, und nur die GUI bleibt stehen face-smile. Vielen Dank!!!!!


function GenerateForm()
    {
      
        Param 
        (
	        [Parameter(Mandatory=$true)][string]$param1
        )

        # Hide PowerShell Console (http:{{comment_single_line_double_slash:0}}
        Add-Type -Name Window -Namespace Console -MemberDefinition '  
        [DllImport("Kernel32.dll")]  
        public static extern IntPtr GetConsoleWindow();
        [DllImport("user32.dll")]  
        public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'  
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)

        #region Import the Assemblies
        [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
        [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
        #endregion
    
        #region Generated Form Objects
        $objForm = New-Object System.Windows.Forms.Form
        ...
        ...
Member: TK1987
TK1987 Jan 29, 2020 at 11:54:39 (UTC)
Goto Top
Moin,

Zitat von @Herbstengel:
Während der Entwicklung ist diese Powershell-Aus-/Eingabebox (Konsolefenster) ganz nützlich, z.B. für Anzeigen mit Write-Host oder Test-Eingaben mit Read-Host. Sobald die GUI fertig ist, möchte ich diese Powershell-Aus-/Eingabebox nicht mehr beim Skriptaufruf haben. Bitte um Lösungsansätze.
Eine weitere Möglichkeit wäre, aus dem Script eine Exe zu machen.
gallery.technet.microsoft.com/scriptcenter/PS2EXE-GUI-Convert-e7cb69d5

Gruß TK1987