PowerShell
PowerShell

Reputation: 2061

Pass Memory in GB Using Import-CSV Powershell to New-VM in Hyper-V Version 3

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

Upvotes: 1

Views: 1904

Answers (1)

Shay Levy
Shay Levy

Reputation: 126762

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

Upvotes: 3

Related Questions