0

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

PowerShell
  • 1,991
  • 8
  • 35
  • 57

1 Answers1

2

If you pass 1 and multiply it by 1024 you're actually asking to assign 1024 bytes (kb) of memory and the error message state that the minimum amount you can give is 8 mb.

If the number you pass is meant to be in GB, multiply it tree times by 1024, (1024*1024*1024). If you want to pass a value such as '3gb', you'll need to expand it first:

New-VM -MemoryStartupBytes (Invoke-Expression $memory) -Name $VMName
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Thanks Shay :) it was so silly of me to think 1*1024 = 1024MB :), thanks for correcting me, let me also try Invoke-Expression. Thanks! – PowerShell Nov 04 '12 at 16:33