Enterprise IT Solutions/Microsoft

[PowerShell] GUI 애플리케이션 만들기

iseop 2022. 1. 5. 19:55   인쇄용 버전

이번에 올릴 스크립트는 제가 실무에서 활용했던 마법사 형태의 GUI 툴입니다. 

개인적으로 당연히 터미널 속에서 작업을 끝낼 수 있는 스크립트를 선호하지만 어떤 이유로 Windows Forms를 이용해서 GUI 형태로 만들었습니다.

스크립트 내용은 제외하고 껍데기만 가져와서 정리해 보았습니다.

 

스크린샷

0123

 

필요한 컴포넌트 임포트

Add-Type -AssemblyName system.Windows.Forms 
[System.Windows.Forms.Application]::EnableVisualStyles()

 

스테이지별 작업 정의

$Global:step=0
$Global:hosts='localhost','127.0.0.1'

function Run-Check {
    if($Global:step -eq 1) {
        $prevBtn.Enabled=$false
        $nextBtn.Enabled=$true
        $doneBtn.Enabled=$false

        $contentBox.text="Processing..."
        $titleBox.Text="STEP $Global:step"

        $result=Invoke-Command -ComputerName $Global:hosts -Command {hostname}
        $contentBox.text=($result|out-string)
    }

    if($Global:step -eq 2) {
        $prevBtn.Enabled=$true
        $nextBtn.Enabled=$true
        $doneBtn.Enabled=$false

        $contentBox.text="Processing..."
        $titleBox.Text="STEP $Global:step"

        $result=Invoke-Command -ComputerName $Global:hosts -Command {hostname}
        $contentBox.text=($result|out-string)
    }

    if($Global:step -eq 3) {
        $prevBtn.Enabled=$true
        $nextBtn.Enabled=$true
        $doneBtn.Enabled=$false

        $contentBox.text="Processing..."
        $titleBox.Text="STEP $Global:step"

        $result=Invoke-Command -ComputerName $Global:hosts -Command {hostname}
        $contentBox.text=($result|out-string)
    }

    if($Global:step -eq 4) {
        $prevBtn.Enabled=$true
        $nextBtn.Enabled=$true
        $doneBtn.Enabled=$false

        $contentBox.text="Processing..."
        $titleBox.Text="STEP $Global:step"

        $result=Invoke-Command -ComputerName $Global:hosts -Command {hostname}
        $contentBox.text=($result|out-string)
    }

    if($Global:step -eq 5) {
        $prevBtn.Enabled=$true
        $nextBtn.Enabled=$false
        $doneBtn.Enabled=$true

        $contentBox.text="Processing..."
        $titleBox.Text="STEP $Global:step"

        $result=Invoke-Command -ComputerName $Global:hosts -Command {hostname}
        $contentBox.text=($result|out-string)
    }
}

 

Windows Forms 오브젝트 정의

$w=1024
$h=720

$form=New-Object system.windows.forms.form
$form.Icon=[System.Drawing.SystemIcons]::Asterisk
$form.Text="A Good Wizard"
$form.MaximizeBox=0
$form.Height=$h
$form.Width=$w
$form.Left=20
$form.Top=20
$form.StartPosition="Manual"
$form.FormBorderStyle=1
$form.Topmost-$false

$titleBox=New-Object system.windows.forms.RichTextBox 
$titleBox.Width=$w-40
$titleBox.Height=30
$titleBox.Top=10
$titleBox.Left=10
$titleBox.Font="Consolas, 14" 
$titleBox.Multiline=$false
$titleBox.ReadOnly=$true 
$titleBox.BackColor="#3333FF"
$titleBox.ForeColor="white"
$titleBox.BorderStyle=0
$titleBox.Text="Welcome Message"

$contentBox=New-Object system.windows.forms.RichTextBox
$contentBox.Width=$w-40
$contentBox.Height=$h-140
$contentBox.Top=50 
$contentBox.Left=10
$contentBox.Font="consolas, 12"
$contentBox.ForeColor="#00FF00" 
$contentBox.BackColor="Black"
$contentBox.BorderStyle=0
$contentBox.ReadOnly=$true 
$contentBox.Text="
***1. Click next button to begin.*** 
***2. When finished, done button should be activated.***
***3. Write the things you found.***
***4. Save the log file.***

Last Modified: August 9, 2021 Author: Iseop"

$prevBtn=New-Object system.windows.forms.Button
$prevBtn.Enabled=$false
$prevBtn.Text="< Prev"
$prevBtn.Width=80
$prevBtn.Height=30
$prevBtn.Top=$h-80
$prevBtn.Left=$w-260
$prevBtn.Add_Click(
    {
        $Global:step--;
        Run-Check
    }
)

$nextBtn=New-Object system.windows.forms.Button
$nextBtn.Text="Next >"
$nextBtn.Width=80
$nextBtn.Height=30
$nextBtn.Top=$h-80
$nextBtn.Left=$w-170
$nextBtn.Add_Click(
    {
        $Global:step++;
        Run-Check
    }
)

$doneBtn=New-Object system.windows.forms.Button
$doneBtn.Text="Done"
$doneBtn.Width=50
$doneBtn.Height=30
$doneBtn.Top=$h-80
$doneBtn.Left=$w-80
$doneBtn.Enabled=$false
$doneBtn.Add_Click(
    {
        $logfile=((Get-Date -Format "yyyyMMdd")+".log")
        "$env:USERNAME finished the wizard." > $logfile
        notepad $logfile
        $form.Close()
    }
)

$pinTopCheckBox=New-Object System.Windows.Forms.CheckBox
$pinTopCheckBox.Width=200
$pinTopCheckBox.Top=$h-80
$pinTopCheckBox.Left=10
$pinTopCheckBox.Text="Pinned on top"
$pinTopCheckBox.checked=$false 
$pinTopCheckBox.Add_CheckStateChanged(
    {
        if(!($pinTopCheckBox.checked)) { 
            $form.topmost=$false
        } else {
            $form.topmost=$true
        }
    }
)

$form.Controls.Add($titleBox)
$form.Controls.Add($contentBox)
$form.Controls.Add($prevBtn)
$form.Controls.Add($nextBtn)
$form.Controls.Add($doneBtn)
$form.Controls.Add($pinTopCheckBox)

 

실행

$form.ShowDialog()