이전 게시물
2023.02.12 – (ChatGPT) – ChatGPT. Out-GridView를 .Net 기반의 System.Windows.Forms로 변경
ChatGPT. Out-GridView를 .Net 기반의 System.Windows.Forms로 변경
다음 데이터는 .Net Framework 클래스인 System.Drawing 및 Sysmte.Windows.Forms를 사용할 수 있는 Powershell 스크립트 예제를 제공합니다. 목록 상자에서 항목 선택 – PowerShell | Microsoft Learn 추가 유형 -AssemblyName 시스템
blog.limcm.kr
채팅GPT~처럼 지금까지 생산 2개 스크립트 합병을 시도하다 하지만, 길이 에게 긴 GPT하다 분석하다 어려움 시달리다 물건 같았다. 약간의 더 특별한 물어볼 때 접근.
이전에 만든 스크립트. 더 나아가 함께 구역만 나뉘다 싱글 스크립트 파일에서 복사 반죽 하다.

첫 번째 스크립트에서 $DestinationStream 그만큼 목표 경로 및 VHDX파일 이름 나타내다.

VHDX붓다 전화하다 $VHD는 $DestinationStream.Name으로 지정된 경우 VHDX붓다 선택하다 창문 나가다 숫자 있다.

여기 $VHDLocation = “디:\wingtiptoys.kr” ~에서 연결하다 ~하기 위해 $DestinationStream.이름 ~에 접합재 유일한 방법 표시되어야 한다 하다.
그만큼 부분 GPT에게 나는 물었다.
| $DestinationStream의 값은 이미 D:\Wingtiptoys.kr\Client1.vhdx라는 이름이 되었습니다. 결과적으로 폴더 경로만 호출하는 변수를 여기에서 만듭니다. |

분할 경로 붓다 사용 잘 아는.
$FolderPath = Split-Path -Path $DestinationStream.Name
분할 경로(Microsoft.PowerShell.Management) – PowerShell | 마이크로소프트 런
| Split-Path cmdlet은 경로의 지정된 부분만 반환합니다. B. 상위 폴더, 하위 폴더 또는 파일 이름. 또한 분할 경로가 가리키는 대상을 얻고 경로가 상대적인지 절대적인지 알 수 있습니다. |
아래에 함께 확인됩니다.

그 다음에 $FolderPath두번째 $VHDLocation ~에서 변화.

$VHD 부분은 아래에 함께 변화.

엄밀히 말하자면 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
$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
}
$SelectedFile = $VHDXFileNames | Out-GridView -Title "Select a VHDX file" -OutputMode Single
$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그만큼 만들어진.

2 부~에 사용자 또는 입력 선택하다 부분 불편한 펠트. 1 부~에 빨리 계속하다 숫자 ~하도록 하다 변경되었습니다.
#Part 1: SysPrep 폴더를 지정합니다.
$VHDXFolders = Get-ChildItem -Path "F:\sysprepVHD" -Directory
#Part 2: VM 정보 입력 Script
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"
$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()
}
#Part 3: Copy VHDX
$VHDXFiles = @()
foreach ($Folder in $VHDXFolders) {
$VHDXFiles += Get-ChildItem -Path $Folder.FullName -Filter *.vhdx
}
$VHDXFileNames = @()
foreach ($File in $VHDXFiles) {
$VHDXFileNames += $File.Name
}
$SelectedFile = $VHDXFileNames | Out-GridView -Title "Select a VHDX file" -OutputMode Single
$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 4: New-VM
$VHDLocation = Split-Path -Path $DestinationStream.Name
$VHDs = $DestinationStream.Name
if ($result -eq (System.Windows.Forms.DialogResult)::OK) {
$SelectedSwitch = $listBox.SelectedItem
New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -Generation 2 -Path $VHDLocation -SwitchName $SelectedSwitch -VHDPath $VHDs
Set-VM -Name $VMName -ProcessorCount $VMProcessorCount
Start-VM -Name $VMName
}
아래에 지역만 소유하다 환경을 위해 맞다 변경된 경우 크기가 큰 문제 아니요 ~ 할 것이다.
