반응형

이전 포스팅에서 VHDX 파일을 복사하는 스크립트를 생성해 보았습니다.

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

 

이번에는 VM 생성하는 스크립트 제작입니다.

 

[질문1] Hyper-v에서 VM만드는 파워쉘 스크립트 만들어주세요.

# Define the virtual machine configuration
$VMName = "MyVM"
$VHDLocation = "C:\Hyper-V\Virtual Hard Disks"
$VHDFileName = "$VMName.vhdx"
$VHDPath = "$VHDLocation\$VHDFileName"
$VMProcessorCount = 2
$VMMemoryStartupBytes = 2GB

# Create a new virtual machine
New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -ProcessorCount $VMProcessorCount

# Create a virtual hard disk for the virtual machine
New-VHD -Path $VHDPath -SizeBytes 60GB

# Attach the virtual hard disk to the virtual machine
Add-VMHardDiskDrive -VMName $VMName -Path $VHDPath

# Start the virtual machine
Start-VM -Name $VMName

-> VM 생성하는 기본 스크립트입니다. 일부는 Sample 값으로 입력되어 있고, VHD파일도 새로 생성합니다. 다음 질문에서 몇가지의 기본 값을 지정하고, 선택사항은 Drop box 변경할 있도록 하겠습니다.

 

[질문2-1] 가상디스크의 기본위치는 "D:\wingtiptoys.kr" 에 있고 해당 폴더의 vhdx 파일 중 Drop box 형태로 선택하도록 해주세요.

[질문2-2] RAM은 2GB, ProcessorCount는 4, Switchname은 현재 존재하는 가상 스위치를 Drop box 형태로 선택하도록 해주세요.

# Define the virtual machine configuration
$VMName = "MyVM"
$VHDLocation = "D:\wingtiptoys.kr"
$VMProcessorCount = 4
$VMMemoryStartupBytes = 2GB

# Get a list of existing virtual switches
$Switches = Get-VMSwitch | Select-Object -Property Name

# Display a drop-down menu to select a virtual switch
$SelectedSwitch = $Switches | Out-GridView -Title "Select a virtual switch" -PassThru

# Get a list of existing virtual hard disks in the specified location
$VHDs = Get-ChildItem -Path $VHDLocation -Filter *.vhdx | Select-Object -Property Name

# Display a drop-down menu to select a virtual hard disk
$SelectedVHD = $VHDs | Out-GridView -Title "Select a virtual hard disk" -PassThru

# Create a new virtual machine
New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -ProcessorCount $VMProcessorCount

# Attach the selected virtual hard disk to the virtual machine
Add-VMHardDiskDrive -VMName $VMName -Path "$VHDLocation\$($SelectedVHD.Name)"

# Connect the virtual machine to the selected virtual switch
Add-VMNetworkAdapter -VMName $VMName -SwitchName $SelectedSwitch.Name

# Start the virtual machine
Start-VM -Name $VMName

-> VM이름을 타이핑하여 지정하도록 하고, 2세대 VM으로 지정하도록 추가 수정을 요청

 

[질문3] VM이름은 타이핑하여 지정할 수 있도록 수정해주세요.

# Define the virtual machine configuration
$VMName = Read-Host "Enter the name of the virtual machine"
$VHDLocation = "D:\wingtiptoys.kr"
$VMProcessorCount = Read-Host "Enter the number of processors"
$VMMemoryStartupBytes = Read-Host "Enter the amount of memory (in GB)" + "GB"

# Get a list of existing virtual switches
$Switches = Get-VMSwitch | Select-Object -Property Name

# Display a drop-down menu to select a virtual switch
$SelectedSwitch = $Switches | Out-GridView -Title "Select a virtual switch" -PassThru

# Get a list of existing virtual hard disks in the specified location
$VHDs = Get-ChildItem -Path $VHDLocation -Filter *.vhdx | Select-Object -Property Name

# Display a drop-down menu to select a virtual hard disk
$SelectedVHD = $VHDs | Out-GridView -Title "Select a virtual hard disk" -PassThru

# Create a new virtual machine
New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -ProcessorCount $VMProcessorCount -Generation 2

# Attach the selected virtual hard disk to the virtual machine
Add-VMHardDiskDrive -VMName $VMName -Path "$VHDLocation\$($SelectedVHD.Name)"

# Connect the virtual machine to the selected virtual switch
Add-VMNetworkAdapter -VMName $VMName -SwitchName $SelectedSwitch.Name

# Start the virtual machine
Start-VM -Name $VMName

-> 옵션도 지정해 달라고 요청하겠습니다.

 

[질문4] Store the virtual machine in a different location 옵션도 동일한 VHDX가 있는 경로와 동일하게 지정해주세요.

-> 요청한 옵션은 Path라는 Parameter 지정하여 설정하였다. 라고 설명하고 있습니다.

# Define the virtual machine configuration
$VMName = Read-Host "Enter the name of the virtual machine"
$VHDLocation = "D:\wingtiptoys.kr"
$VMProcessorCount = Read-Host "Enter the number of processors"
$VMMemoryStartupBytes = Read-Host "Enter the amount of memory (in GB)" + "GB"

# Get a list of existing virtual switches
$Switches = Get-VMSwitch | Select-Object -Property Name

# Display a drop-down menu to select a virtual switch
$SelectedSwitch = $Switches | Out-GridView -Title "Select a virtual switch" -PassThru

# Get a list of existing virtual hard disks in the specified location
$VHDs = Get-ChildItem -Path $VHDLocation -Filter *.vhdx | Select-Object -Property Name

# Display a drop-down menu to select a virtual hard disk
$SelectedVHD = $VHDs | Out-GridView -Title "Select a virtual hard disk" -PassThru

# Create a new virtual machine
New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -ProcessorCount $VMProcessorCount -Generation 2 -Path $VHDLocation

# Attach the selected virtual hard disk to the virtual machine
Add-VMHardDiskDrive -VMName $VMName -Path "$VHDLocation\$($SelectedVHD.Name)"

# Connect the virtual machine to the selected virtual switch
Add-VMNetworkAdapter -VMName $VMName -SwitchName $SelectedSwitch.Name

# Start the virtual machine
Start-VM -Name $VMName

 

[질문4] RAM을 2,4,8,10,16,32 GB 중에서 Drop box 형태로 선택할수 있도록 수정해주세요.

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

# Define the available memory options
$MemoryOptions = @{
    "2 GB" = 2GB
    "4 GB" = 4GB
    "8 GB" = 8GB
    "10 GB" = 10GB
    "16 GB" = 16GB
    "32 GB" = 32GB
}

# Display a drop-down menu to select the amount of memory
$SelectedMemory = $MemoryOptions.Keys | Out-GridView -Title "Select the amount of memory" -PassThru
$VMMemoryStartupBytes = $MemoryOptions[$SelectedMemory]

# Get a list of existing virtual switches
$Switches = Get-VMSwitch | Select-Object -Property Name

# Display a drop-down menu to select a virtual switch
$SelectedSwitch = $Switches | Out-GridView -Title "Select a virtual switch" -PassThru

# Get a list of existing virtual hard disks in the specified location
$VHDs = Get-ChildItem -Path $VHDLocation -Filter *.vhdx | Select-Object -Property Name

# Display a drop-down menu to select a virtual hard disk
$SelectedVHD = $VHDs | Out-GridView -Title "Select a virtual hard disk" -PassThru

# Create a new virtual machine
New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -ProcessorCount $VMProcessorCount -Generation 2 -Path $VHDLocation

# Attach the selected virtual hard disk to the virtual machine
Add-VMHardDiskDrive -VMName $VMName -Path "$VHDLocation\$($SelectedVHD.Name)"

# Connect the virtual machine to the selected virtual switch
Add-VMNetworkAdapter -VMName $VMName -SwitchName $SelectedSwitch.Name

# Start the virtual machine
Start-VM -Name $VMName

-> $VMMemoryStartupBytes 변수를 지정해서 수정했다고 합니다.

 

이쯤에서 스크립트를 실행해 보겠습니다.

VM 이름, Processor 숫자 입력

 

RAM 용량 지정

 

가상 스위치 선택

 

가상 디스크 선택

 

아래와 같이 오류 발생

-> ProcessCount New-VM 아닌 Set-VM에서 사용됩니다.

 

Parameter 상의 오류는 MS 기술자료를 보고 수정하는게 빠를 수도 있습니다.

기술자료: New-VM (Hyper-V) | Microsoft Learn

 

[질문5] New-VM : A parameter cannot be found that matches parameter name 'ProcessorCount'. I think it change to Set-VM

 

다만 VHDX Attach하는 옵션이 일부 잘못되어 있어서 set-VM 추가하여 직접 수정하였습니다. New-VM으로 병합한 부분의 일부 오류가 있어서 아래와 같이 스크립트를 완성하였습니다.

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

# Define the available memory options
$MemoryOptions = @{
    "2 GB" = 2GB
    "4 GB" = 4GB
    "8 GB" = 8GB
    "10 GB" = 10GB
    "16 GB" = 16GB
    "32 GB" = 32GB
}

# Display a drop-down menu to select the amount of memory
$SelectedMemory = $MemoryOptions.Keys | Out-GridView -Title "Select the amount of memory" -PassThru
$VMMemoryStartupBytes = $MemoryOptions[$SelectedMemory]

# Get a list of existing virtual switches
$Switches = Get-VMSwitch | Select-Object -Property Name

# Display a drop-down menu to select a virtual switch
$SelectedSwitch = $Switches | Out-GridView -Title "Select a virtual switch" -PassThru

# Get a list of existing virtual hard disks in the specified location
$VHDs = Get-ChildItem -Path $VHDLocation -Filter *.vhdx | Select-Object -Property Name

# Display a drop-down menu to select a virtual hard disk
$SelectedVHD = $VHDs | Out-GridView -Title "Select a virtual hard disk" -PassThru

# Create a new virtual machine
New-VM -Name $VMName -MemoryStartupBytes $VMMemoryStartupBytes -Generation 2 -Path $VHDLocation -SwitchName $SelectedSwitch.Name -VHDPath "$VHDLocation\$($SelectedVHD.Name)"

# Set the number of virtual processors for the virtual machine
Set-VM -Name $VMName -ProcessorCount $VMProcessorCount

# Start the virtual machine
Start-VM -Name $VMName

 

 

지정한대로 생성되었습니다.

 

VM 정상적으로 동작되는 것을 확인합니다.

 

다음 글

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

반응형

+ Recent posts