반응형

이전글

2023.02.12 - [ChatGPT] - ChatGPT. Out-GridView 을 .Net 기반의 System.Windows.Forms 로 변경

 

ChatGPT. Out-GridView 을 .Net 기반의 System.Windows.Forms 로 변경

아래의 자료에서 .Net Framework class인 System.Drawing과 Sysmte.Windows.Forms를 이용할 수 있는 Powershell Script Sample을 제공하고 있습니다. Selecting items from a list box - PowerShell | Microsoft Learn Add-Type -AssemblyName Syste

blog.limcm.kr

 

이전 포스팅에 이어서 이전에 작성된 스크립트를 병합하여 한번에 동작하도록 구성해보도록 하겠습니다.

단순히 Part 1,2 구분하면 Script 다음과 같습니다.

#Part 1: Copy VHDX
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()
    }
}


#Part 2: New-VM
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
        }
    }
}

 

VHDX 지정하는 $VHDs는 $DestinationStream.Name으로 지정하면 VHDX 선택하는 창을 생략할 있습니다.

여기서 $VHDLocation = "D:\wingtiptoys.kr" 으로 연결하기 위해서는 $DestinationStream.Name 에서 폴더 경로만 표시해야 합니다.

부분을 GPT에게 문의하였습니다.

$DestinationStream 의 값에서 이미 Name : D:\Wingtiptoys.kr\Client1.vhdx 의 결과가 나왔습니다. 여기서 폴더 경로만 결과로 호출하는 변수를 만들어주세요.

Split-Path 활용하라고 알려주었습니다.

$FolderPath = Split-Path -Path $DestinationStream.Name

Split-Path (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn

Split-Path cmdlet은 상위 폴더, 하위 폴더 또는 파일 이름과 같은 경로의 지정된 부분만 반환합니다. 또한 분할 경로에서 참조하는 항목을 가져오고 경로가 상대 경로인지 절대 경로인지 알 수 있습니다.

 

아래와 같이 확인됩니다.

 

그러면 $FolderPath $VHDLocation 으로 수정합니다.

 

 

그리고 $VHDs 부분을 아래와 같이 추가합니다.

 

사실 VHDX 파일을 선택하는 구간이 이상 필요 없습니다. 부분을 문의하였습니다.

아래의 구간이 이상 필요없다고 합니다.

$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

 

# 비활성 처리를 하면서 확인하였습니다. 아래의 영역이 전부 필요 없다고 합니다.

 

우선 아래와 같이 수정하였습니다.

#Part 1: Copy VHDX
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()
    }
}


#Part 2: New-VM
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" [수정전]
$VHDLocation = Split-Path -Path $DestinationStream.Name #[수정후]
$VHDs = $DestinationStream.Name # 새로 추가

$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
           #New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -Generation 2 -Path $VHDLocation -SwitchName $SelectedSwitch -VHDPath "$VHDLocation\$SelectedVHD"
            New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -Generation 2 -Path $VHDLocation -SwitchName $SelectedSwitch -VHDPath $VHDs
            Set-VM -Name $VMName -ProcessorCount $VMProcessorCount
            Start-VM -Name $VMName
       }    
}

 

정상적으로 VM 생성되었지만, 복사가 완료한 뒤에 입력과 선택의 부분이 다소 비효율적인 같습니다.

 

입력과 선택하는 영역을 전부 앞으로 이동하였습니다. 최종 수정본은 다음과 같습니다.

#Part 1: 정보 입력
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


$VMName = Read-Host "Enter the name of the virtual machine"
$VMProcessorCount = Read-Host "Enter the number of processors"

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


$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)

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

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

    $form.Controls.Add($listBox1)

    $form.Topmost = $true

    $result = $form.ShowDialog()
    $SelectedSwitch = $listBox1.SelectedItem
}


#Part2 Copy VHDX

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()
    }
}


#Part 3: New-VM
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


$VHDLocation = Split-Path -Path $DestinationStream.Name 
$VHDs = $DestinationStream.Name 




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

아래의 순서로 진행됩니다.

1. VM Name, Processor 숫자 입력

 

2. 메모리 선택

 

3. Switch 선택

 

4. VHDX 선택

 

5. VHDX 복사할 위치 지정 (VM 생성위치 지정)

 

6. VHDX 복사진행

 

7. VM 시작

 

반응형

+ Recent posts