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
von Kommandozeile oder aus einen Batch.
Das Skript testscript1.ps1 selber:
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
ich programmiere eine Powershell-GUI und momentan erscheint immer parallel zur GUI die Powershell-Aus-/Eingabebox (Konsolefenster), siehe u.g. Bild:
Skriptaufruf mit
1
powershell.exe -ExecutionPolicy Bypass -File "C:\powershell_tests\testscript1.ps1" -param1 "Parameter eins"
von Kommandozeile oder aus einen Batch.
Das Skript testscript1.ps1 selber:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
[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
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 540699
Url: https://administrator.de/forum/powershell-ist-bei-programmierung-einer-gui-die-darstellung-des-kommandofensters-unbedingt-notwendig-540699.html
Ausgedruckt am: 15.04.2025 um 21:04 Uhr
5 Kommentare
Neuester Kommentar
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 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
Moin,
ungetestet but worth a try
Parameter -NonInteractive anhängen.
https://docs.microsoft.com/de-de/powershell/module/Microsoft.PowerShell. ...
Grüße
ungetestet but worth a try
Parameter -NonInteractive anhängen.
https://docs.microsoft.com/de-de/powershell/module/Microsoft.PowerShell. ...
Grüße
Moin,
gallery.technet.microsoft.com/scriptcenter/PS2EXE-GUI-Convert-e7cb69d5
Gruß TK1987
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.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.
gallery.technet.microsoft.com/scriptcenter/PS2EXE-GUI-Convert-e7cb69d5
Gruß TK1987