PC_Aide

Create new-vm (Mode advance).ps1

Jan 13th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  <# Author : PC Aide (Youtuber French)
  2.  Channel Youtube: https://www.youtube.com/user/Boliccov
  3.  Code source (Author) : http://techthoughts.info/new-hyper-v-vm-via-powershell-or-gui/
  4. #>
  5. #----------------USER CREATION QUESTIONS-------------------
  6. [string]$vmName= Read-Host ”Name of VM”
  7. #__________________________________________________________
  8. [int32]$generation = Read-Host "Generation Type"
  9. #__________________________________________________________
  10. [string]$dynamic = $null
  11. while("yes","no" -notcontains $dynamic){
  12.     $dynamic = Read-Host "Will this VM use dyanmic memory? (yes/no)"
  13. }
  14. if($dynamic -eq "yes"){
  15.     [bool]$dynMemory = $true
  16.     [int64]$minMemory = Read-Host "Memory Minimum (MB)"
  17.     [int64]$maxMemory = Read-Host "Memory Maximum (MB)"
  18.     [int64]$startMemory = Read-Host "Starting Memory (MB)"
  19.     #convert to bytes
  20.     $minMemory = 1MB*$minMemory
  21.     $maxMemory = 1MB*$maxMemory
  22.     $startMemory = 1MB*$startMemory
  23.     [int64]$memory = $minMemory
  24. }
  25. else{
  26.     [int64]$memory = Read-Host "Memory (MB)"
  27.     #convert to bytes
  28.     $memory = 1MB*$memory
  29. }
  30. #__________________________________________________________
  31. Write-Host "--------AVAILABLE SWITCHES--------" -BackgroundColor Black
  32. Get-VMSwitch | Select-Object -ExpandProperty Name
  33. Write-Host "--------AVAILABLE SWITCHES--------" -BackgroundColor Black
  34. [string]$vmSwitch = Read-Host "Please enter a virtual switch name"
  35. #__________________________________________________________
  36. [int32]$cpu = Read-Host "Number of CPUs"
  37. #__________________________________________________________
  38. [string]$vmPath = Read-Host "Enter path for VM config files (Ex E:\VM\)"
  39. [string]$newVMPath = $vmPath
  40. #__________________________________________________________
  41. [string]$vhdPath = Read-Host "Enter path where .vhdx will reside (Ex E:\VHD\)"
  42. [string]$newVHD = $vhdPath+$VMName+".vhdx"
  43. [int64]$vhdSize = Read-Host "Enter VHDSize (GB)"
  44. $vhdSize = [math]::round($vhdSize *1Gb, 3) #converts GB to bytes
  45. #__________________________________________________________
  46.  
  47. #----------------END USER CREATION QUESTIONS---------------
  48.  
  49. try{
  50.     #-----------------CONFIRM CREATE NEW VM----------------
  51.     Write-Host "Creating new VM:" $vmName "Generation type:" $generation `
  52.         "Starting memory:" $memory "stored at:" $newVMPath ", `
  53.            with its .vhdx stored at:" $newVHD "(size" $vhdSize ")" -ForegroundColor Cyan
  54.     [string]$confirm = $null
  55.     while("yes","no" -notcontains $confirm){
  56.         $confirm = Read-Host "Proceed? (yes/no)"
  57.     }
  58.     #---------------END CONFIRM CREATE NEW VM--------------
  59.      
  60.     if($confirm -eq "yes"){
  61.          #------------------CREATE NEW VM-----------------------
  62.         NEW-VM –Name $vmName -Generation $generation –MemoryStartupBytes $memory `
  63.             -Path $newVMPath –NewVHDPath $newVHD –NewVHDSizeBytes $vhdSize | Out-Null
  64.         Start-Sleep 5 #pause script for a few seconds to allow VM creation to complete
  65.         #----------------END CREATE NEW VM---------------------
  66.  
  67.         #---------------CONFIGURE NEW VM-----------------------
  68.         ADD-VMNetworkAdapter –VMName $vmName –Switchname $vmSwitch
  69.         #______________________________________________________
  70.         Set-VMProcessor –VMName $vmName –count $cpu
  71.         #______________________________________________________
  72.         if($dynMemory -eq $true){
  73.             Set-VMMemory $vmName -DynamicMemoryEnabled $true -MinimumBytes $minMemory `
  74.                 -StartupBytes $startMemory -MaximumBytes $maxMemory
  75.         }
  76.         Start-Sleep 8 #pause script for a few seconds - allow VM config to complete
  77.         #---------------END CONFIGURE NEW VM-------------------
  78.         #display new VM information
  79.         Get-VM -Name $vmName | Select Name,State,Generation,ProcessorCount,`
  80.             @{Label=”MemoryStartup”;Expression={($_.MemoryStartup/1MB)}},`
  81.             @{Label="MemoryMinimum";Expression={($_.MemoryMinimum/1MB)}},`
  82.             @{Label="MemoryMaximum";Expression={($_.MemoryMaximum/1MB)}} `
  83.             ,Path,Status | ft -AutoSize
  84.     }
  85.     else{
  86.         Exit
  87.     }
  88.    
  89. }
  90. catch{
  91.     Write-Host "An error was encountered creating the new VM" `
  92.         -ForegroundColor Red -BackgroundColor Black
  93.     Write-Error $_
  94. }
Advertisement
Add Comment
Please, Sign In to add comment