반응형

아래의 자료에서 .Net Framework class인 System.Drawing과 Sysmte.Windows.Forms를 이용할 수 있는 Powershell Script Sample을 제공하고 있습니다.

Selecting items from a list box - PowerShell | Microsoft Learn

 

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select a Computer'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select a computer:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80

[void] $listBox.Items.Add('atl-dc-001')
[void] $listBox.Items.Add('atl-dc-002')
[void] $listBox.Items.Add('atl-dc-003')
[void] $listBox.Items.Add('atl-dc-004')
[void] $listBox.Items.Add('atl-dc-005')
[void] $listBox.Items.Add('atl-dc-006')
[void] $listBox.Items.Add('atl-dc-007')

$form.Controls.Add($listBox)

$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $listBox.SelectedItem
    $x
}

스크립트를 실행하면 아래와 같은 Drop-down list 를 생성합니다.

이 스크립트를 GPT에게 학습하게 합니다.

 

아래와 같이 확인됩니다.

답변 원문
답변 기계번역

 

그리고는 이전에 작성한 VHDX 파일을 복사하는 Script를 위 스크립트를 이용해서 수정하도록 요청하였습니다.

2023.02.11 - [ChatGPT] - ChatGPT. OS별 VHDX 파일을 지정한 뒤, 특정 경로를 지정하면 해당 경로로 복사해 주는 스크립트 생성

 

아래와 같이 답변을 주었으며, 역시 Script 길이가 길어지면 끊어지는 것을 확인할 수 있습니다.

Keep going을 입력하면 이어서 생성합니다.

 

아래의 스크립트가 완성되었습니다.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$VHDXFolders = Get-ChildItem -Path "F:\sysprepVHD" -Directory
$VHDXFiles = @()

foreach ($Folder in $VHDXFolders) {
    $VHDXFiles += Get-ChildItem -Path $Folder.FullName -Filter *.vhdx
}

$VHDXFileNames = @()
foreach ($File in $VHDXFiles) {
    $VHDXFileNames += $File.Name
}

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select a VHDX file'
$form.Size = New-Object System.Drawing.Size(300, 200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75, 120)
$okButton.Size = New-Object System.Drawing.Size(75, 23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150, 120)
$cancelButton.Size = New-Object System.Drawing.Size(75, 23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10, 20)
$label.Size = New-Object System.Drawing.Size(280, 20)
$label.Text = 'Please select a VHDX file:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10, 40)
$listBox.Size = New-Object System.Drawing.Size(260, 20)
$listBox.Height = 80

[void] $listBox.Items.AddRange($VHDXFileNames)

$form.Controls.Add($listBox)

$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    $SelectedFile = $listBox.SelectedItem
    $SelectedFilePath = ($VHDXFiles | Where-Object { $_.Name -eq $SelectedFile }).FullName
    
    $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
    $SaveFileDialog.InitialDirectory = [Environment]::GetFolderPath("Desktop")
    $SaveFileDialog.Filter = "VHDX files (*.vhdx)|*.vhdx"
    $SaveFileDialog.FileName = $SelectedFile

    if ($SaveFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
        $NewFolderPath = [System.IO.Path]::GetDirectoryName($SaveFileDialog.FileName)
        $NewFileName = [System.IO.Path]::GetFileName($SaveFileDialog.FileName)

        if (!(Test-Path $NewFolderPath)) {
            New-Item -ItemType Directory -Path $NewFolderPath
        }

        $BufferSize = 1024 * 1024
        $Buffer = New-Object byte[] $BufferSize
        $SourceStream = [System.IO.File]::OpenRead($SelectedFilePath)
        $DestinationStream = [System.IO.File]::Create($NewFolderPath + "\" + $NewFileName)
        $BytesRead = 0
        $TotalBytes = [System.Math]::Min($SourceStream.Length, [System.Int64]::MaxValue)
        $BytesCopied = 0

        while (($BytesRead = $SourceStream.Read($Buffer, 0, $BufferSize)) -gt 0) {
            $DestinationStream.Write($Buffer, 0, $BytesRead)
            $BytesCopied += $BytesRead
            Write-Progress -Activity "Copying $SelectedFile" -PercentComplete ([System.Int32](100 * $BytesCopied / $TotalBytes))
        }

        $SourceStream.Close()
        $DestinationStream.Close()
    }
}

VHDX 파일은 선택한 뒤,

 

아래와 같이 폴더 및 파일명을 지정하면

 

복사가 진행됩니다.

 

복사 확인

 

이번에는 동일하게 VM을 생성해주는 Script를 수정해 보겠습니다.

2023.02.12 - [ChatGPT] - ChatGPT. VM을 생성하는 Script 제작

 

이어서 Keep going

Keep Going

 

아래와 같이 스크립트가 완성되었습니다.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$VMName = Read-Host "Enter the name of the virtual machine"
$VHDLocation = "D:\wingtiptoys.kr"
$VMProcessorCount = Read-Host "Enter the number of processors"

$MemoryOptions = @{
    "2 GB" = 2GB
    "4 GB" = 4GB
    "8 GB" = 8GB
    "10 GB" = 10GB
    "16 GB" = 16GB
    "32 GB" = 32GB
}

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select the amount of memory'
$form.Size = New-Object System.Drawing.Size(300, 200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75, 120)
$okButton.Size = New-Object System.Drawing.Size(75, 23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150, 120)
$cancelButton.Size = New-Object System.Drawing.Size(75, 23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10, 20)
$label.Size = New-Object System.Drawing.Size(280, 20)
$label.Text = 'Please select the amount of memory:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10, 40)
$listBox.Size = New-Object System.Drawing.Size(260, 20)
$listBox.Height = 80

[void] $listBox.Items.AddRange($MemoryOptions.Keys)

$form.Controls.Add($listBox)

$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    $SelectedMemory = $listBox.SelectedItem
    $VMMemoryStartupBytes = $MemoryOptions[$SelectedMemory]

    $Switches = Get-VMSwitch | Select-Object -Property Name
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Select a virtual switch'
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'

    $okButton = New-Object System.Windows.Forms.Button
    $okButton.Location = New-Object System.Drawing.Point(75, 120)
    $okButton.Size = New-Object System.Drawing.Size(75, 23)
    $okButton.Text = 'OK'
    $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $okButton
    $form.Controls.Add($okButton)

    $cancelButton = New-Object System.Windows.Forms.Button
    $cancelButton.Location = New-Object System.Drawing.Point(150, 120)
    $cancelButton.Size = New-Object System.Drawing.Size(75, 23)
    $cancelButton.Text = 'Cancel'
    $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $cancelButton
    $form.Controls.Add($cancelButton)

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10, 20)
    $label.Size = New-Object System.Drawing.Size(280, 20)
    $label.Text = 'Please select a virtual switch:'
    $form.Controls.Add($label)

    $listBox = New-Object System.Windows.Forms.ListBox
    $listBox.Location = New-Object System.Drawing.Point(10, 40)
    $listBox.Size = New-Object System.Drawing.Size(260, 20)
    $listBox.Height = 80

    [void] $listBox.Items.AddRange($Switches.Name)

    $form.Controls.Add($listBox)

    $form.Topmost = $true

    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $SelectedSwitch = $listBox.SelectedItem
        $VHDs = Get-ChildItem -Path $VHDLocation -Filter *.vhdx | Select-Object -Property Name

        $form = New-Object System.Windows.Forms.Form
        $form.Text = 'Select a virtual hard disk'
        $form.Size = New-Object System.Drawing.Size(300, 200)
        $form.StartPosition = 'CenterScreen'

        $okButton = New-Object System.Windows.Forms.Button
        $okButton.Location = New-Object System.Drawing.Point(75, 120)
        $okButton.Size = New-Object System.Drawing.Size(75, 23)
        $okButton.Text = 'OK'
        $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $form.AcceptButton = $okButton
        $form.Controls.Add($okButton)

        $cancelButton = New-Object System.Windows.Forms.Button
        $cancelButton.Location = New-Object System.Drawing.Point(150, 120)
        $cancelButton.Size = New-Object System.Drawing.Size(75, 23)
        $cancelButton.Text = 'Cancel'
        $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
        $form.CancelButton = $cancelButton
        $form.Controls.Add($cancelButton)

        $label = New-Object System.Windows.Forms.Label
        $label.Location = New-Object System.Drawing.Point(10, 20)
        $label.Size = New-Object System.Drawing.Size(280, 20)
        $label.Text = 'Please select a virtual hard disk:'
        $form.Controls.Add($label)

        $listBox = New-Object System.Windows.Forms.ListBox
        $listBox.Location = New-Object System.Drawing.Point(10, 40)
        $listBox.Size = New-Object System.Drawing.Size(260, 20)
        $listBox.Height = 80

        [void] $listBox.Items.AddRange($VHDs.Name)

        $form.Controls.Add($listBox)

        $form.Topmost = $true

        $result = $form.ShowDialog()

        if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
            $SelectedVHD = $listBox.SelectedItem
            New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -Generation 2 -Path $VHDLocation -SwitchName $SelectedSwitch -VHDPath "$VHDLocation\$SelectedVHD"
            Set-VM -Name $VMName -ProcessorCount $VMProcessorCount
            Start-VM -Name $VMName
        }
    }
}

스크립트를 실행하여 확인해보면, 아래와 같이 VM Name, Process Number 확인

 

RAM 지정

 

가상 스위치 지정

 

가상 디스크 지정

 

생성완료

 

ChatGPT를 통해서 다양한 Script 제작을 예전보다는 쉽게 할 수 있게된 것 같습니다.

반응형

+ Recent posts