I would like to be able to input the VM name, memory size, the harddrive size. I’m running into an error when trying to give it a specified amount of RAM and HDD. Any suggestions?
Not so certain I understand why you are reading values in your function via Read-Host when you are already passing these in as parameters to the function? It defeats the purpose.
No, you want to parametrize your function so you don’t have to prompt the user inside the function, using Read-Host. Instead, the information is passed in at the same time the function is called. In my example, the function is created, and then called including the required parameters (notice we made those mandatory).
Function New-Server2012r2VM {
Param(
[Parameter(Mandatory=$true)]
[int]$RAM,
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[int]$HDD
)
Begin {
} #End Begin
Process {
New-VM -Name $Name -MemoryStartupBytes ($RAM * 1gb) -path C:\HyperV\$Name
New-VHD -path C:\HyperVStorage\$Name.vhdx -SizeBytes ($HDD * 1gb) -Dynamic
Add-VMHarddiskDrive -VMName $Name -path C:\HyperVStorage\$Name\$Name.vhdx
Set-VMdvddrive -VMname $Name -ControllerNumber 1 -path C:\users\dbrooks\desktop\en_windows_server_2012_x64_dvd_915478.iso
Add-VMNetworkAdapter -VMName $Name -SwitchName vSwitch1
Start-VM $Name
} # End Process
} #End Function
New-Server2012r2VM -RAM 2 -Name DC01 -HDD 80