Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# Author : PC Aide (Youtuber French)
- Channel Youtube: https://www.youtube.com/user/Boliccov
- Code source (Author) : http://techthoughts.info/new-hyper-v-vm-via-powershell-or-gui/
- #>
- #----------------USER CREATION QUESTIONS-------------------
- [string]$vmName= Read-Host ”Name of VM”
- #__________________________________________________________
- [int32]$generation = Read-Host "Generation Type"
- #__________________________________________________________
- [string]$dynamic = $null
- while("yes","no" -notcontains $dynamic){
- $dynamic = Read-Host "Will this VM use dyanmic memory? (yes/no)"
- }
- if($dynamic -eq "yes"){
- [bool]$dynMemory = $true
- [int64]$minMemory = Read-Host "Memory Minimum (MB)"
- [int64]$maxMemory = Read-Host "Memory Maximum (MB)"
- [int64]$startMemory = Read-Host "Starting Memory (MB)"
- #convert to bytes
- $minMemory = 1MB*$minMemory
- $maxMemory = 1MB*$maxMemory
- $startMemory = 1MB*$startMemory
- [int64]$memory = $minMemory
- }
- else{
- [int64]$memory = Read-Host "Memory (MB)"
- #convert to bytes
- $memory = 1MB*$memory
- }
- #__________________________________________________________
- Write-Host "--------AVAILABLE SWITCHES--------" -BackgroundColor Black
- Get-VMSwitch | Select-Object -ExpandProperty Name
- Write-Host "--------AVAILABLE SWITCHES--------" -BackgroundColor Black
- [string]$vmSwitch = Read-Host "Please enter a virtual switch name"
- #__________________________________________________________
- [int32]$cpu = Read-Host "Number of CPUs"
- #__________________________________________________________
- [string]$vmPath = Read-Host "Enter path for VM config files (Ex E:\VM\)"
- [string]$newVMPath = $vmPath
- #__________________________________________________________
- [string]$vhdPath = Read-Host "Enter path where .vhdx will reside (Ex E:\VHD\)"
- [string]$newVHD = $vhdPath+$VMName+".vhdx"
- [int64]$vhdSize = Read-Host "Enter VHDSize (GB)"
- $vhdSize = [math]::round($vhdSize *1Gb, 3) #converts GB to bytes
- #__________________________________________________________
- #----------------END USER CREATION QUESTIONS---------------
- try{
- #-----------------CONFIRM CREATE NEW VM----------------
- Write-Host "Creating new VM:" $vmName "Generation type:" $generation `
- "Starting memory:" $memory "stored at:" $newVMPath ", `
- with its .vhdx stored at:" $newVHD "(size" $vhdSize ")" -ForegroundColor Cyan
- [string]$confirm = $null
- while("yes","no" -notcontains $confirm){
- $confirm = Read-Host "Proceed? (yes/no)"
- }
- #---------------END CONFIRM CREATE NEW VM--------------
- if($confirm -eq "yes"){
- #------------------CREATE NEW VM-----------------------
- NEW-VM –Name $vmName -Generation $generation –MemoryStartupBytes $memory `
- -Path $newVMPath –NewVHDPath $newVHD –NewVHDSizeBytes $vhdSize | Out-Null
- Start-Sleep 5 #pause script for a few seconds to allow VM creation to complete
- #----------------END CREATE NEW VM---------------------
- #---------------CONFIGURE NEW VM-----------------------
- ADD-VMNetworkAdapter –VMName $vmName –Switchname $vmSwitch
- #______________________________________________________
- Set-VMProcessor –VMName $vmName –count $cpu
- #______________________________________________________
- if($dynMemory -eq $true){
- Set-VMMemory $vmName -DynamicMemoryEnabled $true -MinimumBytes $minMemory `
- -StartupBytes $startMemory -MaximumBytes $maxMemory
- }
- Start-Sleep 8 #pause script for a few seconds - allow VM config to complete
- #---------------END CONFIGURE NEW VM-------------------
- #display new VM information
- Get-VM -Name $vmName | Select Name,State,Generation,ProcessorCount,`
- @{Label=”MemoryStartup”;Expression={($_.MemoryStartup/1MB)}},`
- @{Label="MemoryMinimum";Expression={($_.MemoryMinimum/1MB)}},`
- @{Label="MemoryMaximum";Expression={($_.MemoryMaximum/1MB)}} `
- ,Path,Status | ft -AutoSize
- }
- else{
- Exit
- }
- }
- catch{
- Write-Host "An error was encountered creating the new VM" `
- -ForegroundColor Red -BackgroundColor Black
- Write-Error $_
- }
Advertisement
Add Comment
Please, Sign In to add comment