Pass Memory in GB Using Import-CSV Powershell to New-VM in Hyper-V Version 3
- by PowerShell
I created the below function to pass memory from a csv file to create a VM in Hyper-V Version 3
Function Install-VM {
param (
[Parameter(Mandatory=$true)]
[int64]$Memory=512MB
)
$VMName = "dv.VMWIN2K8R2-3.Hng"
$vmpath = "c:\2012vms"
New-VM -MemoryStartupBytes ([int64]$memory*1024) -Name $VMName -Path $VMPath -Verbose
}
Import-Csv "C:\2012vms\Vminfo1.csv" | ForEach-Object { Install-VM -Memory ([int64]$_.Memory) }
But when i try to create the VM it says mismatch between the memory parameter passed from import-csv, i receive an error as below
VERBOSE: New-VM will create a new virtual machine "dv.VMWIN2K8R2-3.Hng".
New-VM : 'dv.VMWIN2K8R2-3.Hng' failed to modify device 'Memory'. (Virtual machine ID
CE8D36CA-C8C6-42E6-B5C6-2AA8FA15B4AF)
Invalid startup memory amount assigned for 'dv.VMWIN2K8R2-3.Hng'. The minimum amount of memory you can assign to
a virtual machine is '8' MB. (Virtual machine ID CE8D36CA-C8C6-42E6-B5C6-2AA8FA15B4AF)
A parameter that is not valid was passed to the operation.
At line:48 char:9
+ New-VM -ComputerName $HyperVHost -MemoryStartupBytes ([int64]$memory*10 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.HyperV.PowerShell.VMTask:VMTask) [New-VM], VirtualizationOpe
rationFailedException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.NewVMCommand
Also please not in the csv file im passing memory as 1,2,4.. etc as shown below, and converting them to MB by multiplying them with 1024 later
Memory
1
Can Anyone help me out on how to format and pass the memory details to the function