PowerShell & Hyper-V (PoC)

Hi Guys,

I’m currently working on a powershell script to build my PoC environment and using the following code to set memory for each of my VMs

$MemoryAvailable = (Get-VMHostNumaNode).MemoryAvailable/4

I’m splitting the total memory by 4 to get the number which in this case for 16GB computer is 4067. Then I’m rounding it off to 4GB as per next command

$maxRAM = ([math]::round($MemoryAvailable /1024)).tostring() + “GB”

So now I get 4GB for $maxRAM variable but once I do that it makes it a “string” type and NOT an “int” type which then won’t allow me to use variable $maxRAM in the following command

Set-VMMemory -VMName $VMName -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes $maxRAM -Buffer 20

So this part “-MaximumBytes $maxRAM” fails with the following

Set-VMMemory : Cannot bind parameter ‘MaximumBytes’. Cannot convert value “4GB” to type “System.Int64”. Error: “Input string was not in a correct format.”

Some assistance to get around this would be much appreciated?



Just remove the tostring and gb. It wants a number so without it that should work?

Yes I can do that but I need a command to convert 4 into Bytes (4000000000) . What is the best method doing that?

(Get-VMHostNumaNode).MemoryAvailable/4 * 1GB

quote quote=270889.MemoryAvailable/4 * 1GB

That works and has it entered as 4366907998208 which then Hyper-V reports Maximum Memory as 4164608MB. Is there a way to round it off so it’s same as 4GB? in bytes?


It’s telling you it wants it in MB plus the documentation does too

Set-VMMemory TestVM -DynamicMemoryEnabled $true -MinimumBytes 64MB -StartupBytes 256MB -MaximumBytes 2GB -Priority 80 -Buffer 25

So multiply by 1MB

$MemoryAvailable = (Get-VMHostNumaNode).MemoryAvailable/4
$maxRAM = [math]::round($MemoryAvailable /1024) * 1MB
Set-VMMemory -VMName $VMName -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes $maxRAM -Buffer 20