Skip to content

Instantly share code, notes, and snippets.

@InputObject2
Last active May 3, 2018 17:49
Show Gist options
  • Save InputObject2/11fb658c6887e99730457bc0b1080490 to your computer and use it in GitHub Desktop.
Save InputObject2/11fb658c6887e99730457bc0b1080490 to your computer and use it in GitHub Desktop.
<#
Create a Unix VM.
1- We grab the current VM's that are named "ntprsrv-app##"
2- We get the number+1, that's the new name.
3- We create the folders under D:\VM\VMname\Virtual Hard Disks
4- We copy the "D:\Software\CentOS 7 - Ready to Copy - 2018-01-20.vhdx" to it.
5- We create a new VM using the disk, there's a prompt for the number of CPU and RAM.
6- We disable SecureBoot
7- We boot that bad boy up.
8- We set a timer for 30secs then we pop up that it's ready.
#>
clear
$DynamicMemory = Read-Host "Dynamic Memory ? (y/n) "
Switch ($DynamicMemory)
{
Y {Write-host -foreground green "Yes, let's use a modern OS!`n"; $dynMem=$true}
N {Write-Host -foreground yellow "No, I'm gonna be using some weirdly done apps or a database.`n"; $dynMem=$false}
Default {Write-Host -foreground yellow "Default is that we're using it, because we believe in the power of Myth !`n"; $dynMem=$true}
}
if($dynMem) {
$maxMem = Read-Host "Maximum memory ? (GB) "
$minMem = Read-Host "Minimum memory ? (GB) "
$startMem = Read-Host "Startup memory ? (GB) "
} else {
$startMem = Read-Host "Memory ? (GB) "
}
$cores = Read-Host "vCPU's ? (GB) "
$many = Read-Host "How many VM do you want ? (1-...) "
Write-Host "`nOk, enough questions, time for action!`n"
Write-Host "Starting timer.`n"
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
For ($i=0; $i -le $many; $i++) {
Write-host "Creating VM $i of $many."
$vmname = "NTPRSRV-APP" + (([int](get-vm -name "ntprsrv-app*" | select @{ Label = 'Number' ;Expression = { $_.VMName.Substring(11,2) } } | sort number | select -Last 1).number) + 1)
write-host "Starting script...`n`nThis will create a VM with name $vmname.`nLet's go!`n`n"
if(-not (Test-Path "D:\VM\$vmname\Virtual Hard Disks\$vmname.vhdx")) {
New-Item -Force -ItemType Directory -Path "D:\VM\$vmname\Virtual Hard Disks" | out-null
Write-host "Copying the VHD, it's about 10gb so should take a couple mins." -NoNewline
Import-Module BitsTransfer
Start-BitsTransfer -Source 'D:\Software\CentOS 7 - Ready to Copy - 2018-01-20.vhdx' -Destination "D:\VM\$vmname\Virtual Hard Disks\$vmname.vhdx" -Description "Copying VHDX" -DisplayName "Copy"
Write-Host -ForegroundColor Green " Done."
}
Write-host "Creating new virtual machine." -NoNewline
New-VM -Name $vmname -MemoryStartupBytes ([int]$startMem*1GB) -BootDevice VHD -VHDPath "D:\VM\$vmname\Virtual Hard Disks\$vmname.vhdx" -Path "D:\VM\$vmname\" -Generation 2 -SwitchName (Get-VMSwitch -SwitchType External).name | out-null
Write-Host -ForegroundColor Green " Done."
Write-host "Customizing virtual machine." -NoNewline
if($dynMem) { Set-VMMemory -VMName $vmname -MaximumBytes ([int]$maxMem*1GB) -DynamicMemoryEnabled $true -MinimumBytes ([int]$minMem*1GB) }
Set-VM -VMname $vmname -ProcessorCount $cores -AutomaticStopAction ShutDown -AutomaticStartAction StartIfRunning -AutomaticStartDelay (Get-Random -Minimum 100 -Maximum 800)
Set-VMFirmware -VMName $vmname -EnableSecureBoot Off -FirstBootDevice (get-VMHardDiskDrive -VMName $vmname)
Get-VM -VMname $vmname | Enable-VMIntegrationService -Name *
Write-Host -ForegroundColor Green " Done."
Write-host "Customizing networking." -NoNewline
Set-VMNetworkAdapterVlan -VlanId 1 -VMName $vmname -Access
Write-Host -ForegroundColor Green " Done."
Start-VM $vmname
}
$stopwatch.Stop()
Write-host "Total time : $([math]::Round($stopwatch.Elapsed.TotalSeconds,0)) seconds for $many Virtual machines."
@InputObject2
Copy link
Author

Not doing any tests because we assume that everything here just works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment