채팅 GPT.Copy-VHDX, New-VM 스크립트 병합

이전의

2023.02.12 – (ChatGPT) – ChatGPT. Out-GridView를 .Net 기반의 System.Windows.Forms로 변경

채팅 GPT. Out-GridView를 .Net 기반의 System.Windows.Forms로 변경

다음 데이터는 .Net Framework 클래스인 System.Drawing 및 Sysmte.Windows.Forms를 사용할 수 있는 Powershell 스크립트의 예를 제공합니다.목록 상자에서 항목 선택 – PowerShell | Microsoft Learn 추가 유형 -AssemblyName 시스템

blog.limcm.kr

채팅 GPT~처럼 전에 생산 2 스크립트 병합하려고 하지만, 길이 또한 공통 기술하다 분석하다 어려움 통증 물건 마치. 작은 구체적으로 물어볼 때 가까운.

이전에 만든 스크립트입니다. 다음과 함께 지역만 나누다 하나의 스크립트 파일에서 복사 반죽 하다.



첫번째 스크립트에서 $ 대상 스트림 이것 목적지 경로 및 VHDX파일 이름 보여주다.


VHDX던지다 지정하다 $VHD는 $DestinationStream.Name으로 지정된 경우 VHDX던지다 선택하다 창문 생략하다 숫자 가지다.


여기 $VHDLocation = “디:\wingtiptoys.kr통과 연결하다 ~을 위한 $DestinationStream.이름 존재하다 폴더 경로만 보여야 한다 하다.

이것 부분 공통 기술도착하다 나는 물었다.

$DestinationStream의 값은 D:\Wingtiptoys.kr\Client1.vhdx라는 이름을 생성했습니다. 여기에 폴더 경로라는 변수를 만듭니다.


나뭇가지 던지다 이용하다 잘 아는.

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

분할 경로(Microsoft.PowerShell.Management) – PowerShell | Microsoft Learning

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

다음과 같은 함께 확인됩니다.


그 다음에 $ 폴더 경로두번째 $VHD 위치 통과 고치다.



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

대개 가상 기기이것 만들다.


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    
}

아래에 지역만 내 자신의 환경에 맞다 수정된 경우 질문 아니요 ~ 할 것이다.