-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-gui-app.ps1
More file actions
451 lines (392 loc) · 17.1 KB
/
Copy pathtest-gui-app.ps1
File metadata and controls
451 lines (392 loc) · 17.1 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# Test GUI Application - PowerShell Script with File and Image Display
# This script demonstrates reading a text file and displaying an image in a WPF GUI
# Files needed: test.txt, image.png
#Requires -Version 3.0
# Add required assemblies for WPF
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
Add-Type -AssemblyName System.Drawing
# Set error action preference
$ErrorActionPreference = "Stop"
# Initialize logging
$global:AppLogPath = Join-Path ([Environment]::GetFolderPath('UserProfile')) "Downloads\TestGUI-Runtime-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
$global:AppLogMessages = @()
function Write-AppLog {
param(
[string]$Message,
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
$logEntry = "[$timestamp] [$Level] $Message"
$global:AppLogMessages += $logEntry
Write-Host $logEntry -ForegroundColor $(if($Level -eq "ERROR"){"Red"}elseif($Level -eq "WARN"){"Yellow"}else{"Cyan"})
}
function Save-AppLog {
try {
$global:AppLogMessages | Out-File -FilePath $global:AppLogPath -Encoding UTF8
Write-Host "Application log saved to: $global:AppLogPath" -ForegroundColor Green
} catch {
Write-Host "Failed to save application log: $_" -ForegroundColor Red
}
}
Write-AppLog "=== Test GUI Application Started ==="
Write-AppLog "Log file: $global:AppLogPath"
Write-AppLog "PowerShell Version: $($PSVersionTable.PSVersion)"
# Log all environment variables related to resources
Write-AppLog "Environment Variables:"
Write-AppLog " PS_RESOURCE_DIR = $($env:PS_RESOURCE_DIR)"
Write-AppLog " TEMP = $($env:TEMP)"
Write-AppLog " TMP = $($env:TMP)"
# List all files in resource directory if it exists
if ($env:PS_RESOURCE_DIR -and (Test-Path $env:PS_RESOURCE_DIR)) {
Write-AppLog "Files in PS_RESOURCE_DIR:"
try {
$files = Get-ChildItem -Path $env:PS_RESOURCE_DIR -File
foreach ($file in $files) {
Write-AppLog " - $($file.Name) ($($file.Length) bytes)"
}
Write-AppLog "Total files found: $($files.Count)"
} catch {
Write-AppLog " ERROR listing files: $($_.Exception.Message)" "ERROR"
}
} else {
Write-AppLog "PS_RESOURCE_DIR not set or directory does not exist" "WARN"
}
# Function to get resource file path (will work with embedded resources)
function Get-ResourcePath {
param([string]$FileName)
Write-AppLog "Searching for file: $FileName"
# FIRST: Check if running from executable (environment variable set by C# runtime)
if ($env:PS_RESOURCE_DIR) {
$resourcePath = Join-Path $env:PS_RESOURCE_DIR $FileName
Write-AppLog " Checking resource dir: $resourcePath"
if (Test-Path $resourcePath) {
Write-AppLog " SUCCESS: Found in resource directory!" "SUCCESS"
return $resourcePath
} else {
Write-AppLog " Not found in resource dir" "WARN"
}
} else {
Write-AppLog " PS_RESOURCE_DIR not set" "WARN"
}
# Try current script directory
$scriptDir = if ($MyInvocation.ScriptName) { Split-Path -Parent $MyInvocation.ScriptName } else { $PSScriptRoot }
if (-not $scriptDir) { $scriptDir = Get-Location }
$localPath = Join-Path $scriptDir $FileName
Write-AppLog " Checking script dir: $localPath"
if (Test-Path $localPath) {
Write-AppLog " SUCCESS: Found in script directory!"
return $localPath
}
# Try working directory
$workingPath = Join-Path (Get-Location) $FileName
Write-AppLog " Checking working dir: $workingPath"
if (Test-Path $workingPath) {
Write-AppLog " SUCCESS: Found in working directory!"
return $workingPath
}
# Try temp directory (legacy check)
$tempPath = Join-Path $env:TEMP $FileName
Write-AppLog " Checking temp dir: $tempPath"
if (Test-Path $tempPath) {
Write-AppLog " SUCCESS: Found in temp directory!"
return $tempPath
}
Write-AppLog " ERROR: File not found in any location!" "ERROR"
return $null
}
# XAML definition for the GUI
$xaml = @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test GUI Application - PowerShell Executor Builder Demo"
Height="600" Width="800"
WindowStartupLocation="CenterScreen"
Background="#FFF8F9FA">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="10"/>
<Setter Property="Padding" Value="15,8"/>
<Setter Property="Background" Value="#FF007BFF"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Margin" Value="10"/>
<Setter Property="Padding" Value="10"/>
<Setter Property="BorderBrush" Value="#FFCED4DA"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FontFamily" Value="Consolas"/>
<Setter Property="FontSize" Value="11"/>
</Style>
<Style TargetType="Label">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Margin" Value="10,10,10,5"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Header -->
<Border Grid.Row="0" Background="#FF343A40" Padding="20">
<StackPanel>
<TextBlock Text="PowerShell Executor Builder - Test Application"
FontSize="20" FontWeight="Bold"
Foreground="White" HorizontalAlignment="Center"/>
<TextBlock Text="Demonstrating text file reading and image display with embedded resources"
FontSize="12" Foreground="#FFE9ECEF"
HorizontalAlignment="Center" Margin="0,5,0,0"/>
</StackPanel>
</Border>
<!-- Text File Content -->
<GroupBox Grid.Row="1" Header="Text File Content (test.txt)" Margin="15">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Name="btnLoadText" Content="Load Text File"/>
<Button Name="btnClearText" Content="Clear"/>
<TextBlock Name="txtFileStatus" Text="Click 'Load Text File' to read test.txt"
VerticalAlignment="Center" Margin="15,0,0,0" FontStyle="Italic"/>
</StackPanel>
<TextBox Name="txtFileContent" Grid.Row="1"
IsReadOnly="True" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
Background="#FFF8F8F8"/>
</Grid>
</GroupBox>
<!-- Image Display -->
<GroupBox Grid.Row="2" Header="Image Display (image.png)" Margin="15">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Name="btnLoadImage" Content="Load Image"/>
<Button Name="btnClearImage" Content="Clear Image"/>
<TextBlock Name="txtImageStatus" Text="Click 'Load Image' to display image.png"
VerticalAlignment="Center" Margin="15,0,0,0" FontStyle="Italic"/>
</StackPanel>
<Border Grid.Row="1" BorderBrush="#FFCED4DA" BorderThickness="1" Margin="10">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Image Name="imgDisplay" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</ScrollViewer>
</Border>
</Grid>
</GroupBox>
<!-- Action Buttons -->
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center" Margin="20">
<Button Name="btnLoadBoth" Content="Load Both Files" Width="150"/>
<Button Name="btnAbout" Content="About" Width="100"/>
<Button Name="btnExit" Content="Exit" Width="100"/>
</StackPanel>
</Grid>
</Window>
'@
try {
Write-Host "Loading Test GUI Application..." -ForegroundColor Green
# Load XAML
$reader = [System.Xml.XmlReader]::Create([System.IO.StringReader]$xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$reader.Close()
Write-Host "XAML loaded successfully!" -ForegroundColor Green
} catch {
Write-Host "Error loading XAML: $($_.Exception.Message)" -ForegroundColor Red
[System.Windows.MessageBox]::Show("Error loading GUI: $($_.Exception.Message)", "Error", "OK", "Error")
exit 1
}
# Set window icon (if Set-AppIcon function is available from PSEBuilder)
if (Get-Command Set-AppIcon -ErrorAction SilentlyContinue) {
try {
Set-AppIcon -Window $window
Write-AppLog "Window icon set using Set-AppIcon function" "INFO"
} catch {
Write-AppLog "Could not set window icon: $($_.Exception.Message)" "WARN"
}
} else {
Write-AppLog "Set-AppIcon function not available (not built with icon support)" "INFO"
}
# Get control references
$btnLoadText = $window.FindName("btnLoadText")
$btnClearText = $window.FindName("btnClearText")
$txtFileStatus = $window.FindName("txtFileStatus")
$txtFileContent = $window.FindName("txtFileContent")
$btnLoadImage = $window.FindName("btnLoadImage")
$btnClearImage = $window.FindName("btnClearImage")
$txtImageStatus = $window.FindName("txtImageStatus")
$imgDisplay = $window.FindName("imgDisplay")
$btnLoadBoth = $window.FindName("btnLoadBoth")
$btnAbout = $window.FindName("btnAbout")
$btnExit = $window.FindName("btnExit")
# Function to load text file
function Load-TextFile {
Write-AppLog "=== Load-TextFile function called ===" "INFO"
try {
$textPath = Get-ResourcePath "test.txt"
if ($textPath) {
Write-AppLog "Text file path resolved: $textPath" "INFO"
Write-AppLog "File exists: $(Test-Path $textPath)" "INFO"
if (Test-Path $textPath) {
$fileInfo = Get-Item $textPath
Write-AppLog "File size: $($fileInfo.Length) bytes" "INFO"
}
$content = Get-Content -Path $textPath -Raw
Write-AppLog "Content loaded successfully (length: $($content.Length) characters)" "INFO"
$txtFileContent.Text = $content
$txtFileStatus.Text = "Loaded: $textPath"
$txtFileStatus.Foreground = "Green"
Write-Host "Text file loaded from: $textPath" -ForegroundColor Green
Write-AppLog "Text file loaded and displayed successfully" "INFO"
} else {
Write-AppLog "Text file NOT FOUND - Get-ResourcePath returned null" "ERROR"
$txtFileContent.Text = "test.txt file not found!"
$txtFileStatus.Text = "Error: test.txt not found in script directory, working directory, or temp folder"
$txtFileStatus.Foreground = "Red"
Write-Host "Text file not found!" -ForegroundColor Red
}
} catch {
Write-AppLog "EXCEPTION in Load-TextFile: $($_.Exception.Message)" "ERROR"
Write-AppLog "Stack trace: $($_.ScriptStackTrace)" "ERROR"
$txtFileContent.Text = "Error loading text file: $($_.Exception.Message)"
$txtFileStatus.Text = "Error loading file"
$txtFileStatus.Foreground = "Red"
Write-Host "Error loading text file: $($_.Exception.Message)" -ForegroundColor Red
}
Write-AppLog "=== Load-TextFile function completed ===" "INFO"
}
# Function to load image
function Load-ImageFile {
Write-AppLog "=== Load-ImageFile function called ===" "INFO"
try {
$imagePath = Get-ResourcePath "image.png"
if ($imagePath) {
Write-AppLog "Image file path resolved: $imagePath" "INFO"
Write-AppLog "File exists: $(Test-Path $imagePath)" "INFO"
if (Test-Path $imagePath) {
$fileInfo = Get-Item $imagePath
Write-AppLog "File size: $($fileInfo.Length) bytes" "INFO"
}
Write-AppLog "Creating BitmapImage object..." "INFO"
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.UriSource = New-Object System.Uri($imagePath)
$bitmap.EndInit()
Write-AppLog "BitmapImage created successfully" "INFO"
$imgDisplay.Source = $bitmap
$txtImageStatus.Text = "Loaded: $imagePath"
$txtImageStatus.Foreground = "Green"
Write-Host "Image loaded from: $imagePath" -ForegroundColor Green
Write-AppLog "Image loaded and displayed successfully" "INFO"
} else {
Write-AppLog "Image file NOT FOUND - Get-ResourcePath returned null" "ERROR"
$imgDisplay.Source = $null
$txtImageStatus.Text = "Error: image.png not found in script directory, working directory, or temp folder"
$txtImageStatus.Foreground = "Red"
Write-Host "Image file not found!" -ForegroundColor Red
}
} catch {
Write-AppLog "EXCEPTION in Load-ImageFile: $($_.Exception.Message)" "ERROR"
Write-AppLog "Stack trace: $($_.ScriptStackTrace)" "ERROR"
$imgDisplay.Source = $null
$txtImageStatus.Text = "Error loading image: $($_.Exception.Message)"
$txtImageStatus.Foreground = "Red"
Write-Host "Error loading image: $($_.Exception.Message)" -ForegroundColor Red
}
Write-AppLog "=== Load-ImageFile function completed ===" "INFO"
}
# Event handlers
if ($btnLoadText) {
$btnLoadText.Add_Click({
Load-TextFile
})
}
if ($btnClearText) {
$btnClearText.Add_Click({
$txtFileContent.Text = ""
$txtFileStatus.Text = "Text cleared"
$txtFileStatus.Foreground = "Gray"
})
}
if ($btnLoadImage) {
$btnLoadImage.Add_Click({
Load-ImageFile
})
}
if ($btnClearImage) {
$btnClearImage.Add_Click({
$imgDisplay.Source = $null
$txtImageStatus.Text = "Image cleared"
$txtImageStatus.Foreground = "Gray"
})
}
if ($btnLoadBoth) {
$btnLoadBoth.Add_Click({
Load-TextFile
Load-ImageFile
})
}
if ($btnAbout) {
$btnAbout.Add_Click({
$aboutMessage = @"
PowerShell Executor Builder - Test Application
This is a demonstration GUI application that:
• Reads content from test.txt file
• Displays image.png file
• Works with embedded resources when built as executable
Created for testing the PowerShell Executor Builder system.
File Locations Checked:
1. Script directory
2. Working directory
3. Temp directory (for extracted resources)
Version: 1.0
"@
[System.Windows.MessageBox]::Show($aboutMessage, "About Test Application", "OK", "Information")
})
}
if ($btnExit) {
$btnExit.Add_Click({
Write-AppLog "Exit button clicked - saving log and closing application"
Save-AppLog
$window.Close()
})
}
# Add window closing event to save log
$window.Add_Closing({
Write-AppLog "=== Test GUI Application Closing ==="
Write-AppLog "Application ran for: $((Get-Date) - (Get-Date $global:AppLogMessages[0].Split(']')[0].Trim('[')))"
Save-AppLog
})
# Show startup information
Write-Host "" -ForegroundColor Yellow
Write-Host "===============================================" -ForegroundColor Yellow
Write-Host "Test GUI Application Started" -ForegroundColor Yellow
Write-Host "===============================================" -ForegroundColor Yellow
$scriptDir = if ($MyInvocation.ScriptName) { Split-Path -Parent $MyInvocation.ScriptName } else { $PSScriptRoot }
if (-not $scriptDir) { $scriptDir = Get-Location }
Write-Host "Script Directory: $scriptDir" -ForegroundColor Cyan
Write-Host "Working Directory: $(Get-Location)" -ForegroundColor Cyan
if ($env:PS_RESOURCE_DIR) {
Write-Host "Resource Directory (from exe): $env:PS_RESOURCE_DIR" -ForegroundColor Green
Write-Host "Running as EXECUTABLE with embedded resources" -ForegroundColor Green
} else {
Write-Host "Resource Directory: Not set (running as script)" -ForegroundColor Yellow
Write-Host "Running as SCRIPT - resources should be in script directory" -ForegroundColor Yellow
}
Write-Host "" -ForegroundColor Cyan
Write-Host "Looking for files: test.txt, image.png" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Yellow
Write-Host ""
# Show the window
$window.ShowDialog() | Out-Null