PowerShell is an easy and powerful tool from managing servers to supporting end user issues. Howerver, it would be hard for those who have no knowledge about PowerShell script. Further more, it costs time to run script either built in commands or written functions.
Since PowerShell is on .Net framework, it can call assemblies in .Net framework. .Net framwork provide GUI assemly called "PresentationFramework" so lets manipulate it.
GUI objects can be defined with lines of codes but it is much efficient to design GUI with visual panel in Visual Studio Code, save as XAML format then import the XAML file for GUI.
XAML overview - WPF .NET | Microsoft Learn
XAML overview - WPF .NET
Learn how the XAML language is structured and implemented by Windows Presentation Foundation (WPF) for .NET.
learn.microsoft.com
※ As prerequisites, need Visual Studio Code with "PowerShell Pro Tool" plugin.
Run Visual Studio Code and open file as XAML format.
On the top right corner, click "Show Form Designer" icon then "PSScriptPad" will pop up.
Click "New" --> "New WPF Form"
※ WPF(Windows Presentation Foundation)
On the left menu panel, there is "Toolbox" tab where objects are listed. Click "Toolbox" --> "PresentationFramework" --> "Label" then drwa an object in the canvas.
To modify properties of the object, click "Properties" tab and modify them if necessary.
Now save the XAML file then call the file in PowerShell.
#Call PresentationFramework assembly
Add-Type -AssemblyName PresentationFramework
#Set XAML path
$xamlFile="C:\PowerShell\GUI\HelloWorld.xaml"
#Modified XAML file
$inputXAML = Get-Content -Path $xamlFile -Raw
$inputXAML = $inputXAML -replace 'mc:Igronorable="d"','' -replace "x:N", "N" -replace '^<Win.*','<Window'
[XML]$XAML = $inputXAML
#Set XML reader object and read
$reader = New-Object System.Xml.XmlNodeReader $XAML
try
{
$psForm = [Windows.MarkUp.XamlReader]::Load($reader)
}
catch
{
Write-Host $_.Exception
throw
}
#Save all variabled in XAML file
$XAML.SelectNodes("//*[@Name]") | % {
try
{
Set-Variable -Name "var_$($_.Name)" -Value $psForm.FindName($_.Name) -ErrorAction Stop
}
catch
{
throw
}
}
#Print variables
Get-Variable var_*
#Show GUI
$psForm.ShowDialog()
#Close GUI
$psForm.Close()
Run the script then GUI window will pop up.
'ICT' 카테고리의 다른 글
[Python] Editing PDF Fillable Form (0) | 2023.06.15 |
---|---|
[PowerShell] GUI - S/W Installation with AD Group Assignment (0) | 2023.06.12 |
[DailyLog] 2023-05-11(목) (0) | 2023.05.12 |
9. Learning Projects - Lottery number generator (0) | 2023.05.08 |
[PowerShell]9. Learning Projects - Finding prime numbers (0) | 2023.05.08 |
댓글