Powershell Scripting with Hyper-V and PoC

Shane Taubman 1 Reputation point
2020-11-11T22:56:50.823+00:00

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?

Thanks

Shane

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,513 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 46,561 Reputation points
    2020-11-12T03:48:37.503+00:00

    Why not do this instead?

    $maxRAM = [int64]([math]::round($MemoryAvailable /1024)) * 1024*1024*1024
    

    Or this?

    $maxRAM = [int64]([math]::round($MemoryAvailable /1024)) * 1GB
    

    EDIT: cast the result to be [int64] instead of the [double] type that results from dividing 4067 by 4 in the original post.

    0 comments No comments

  2. Ian Xue 36,336 Reputation points Microsoft Vendor
    2020-11-12T09:17:57.597+00:00

    Hi,

    You don't have to covert $maxRAM to string since -MaximumBytes accepts Int64. Just use ([math]::round($MemoryAvailable /1024)) * 1GB. In addition, (Get-VMHostNumaNode).MemoryAvailable is an array of the memory of all the numa nodes. It cannot be devided unless you have only one node. You should add them up to get the total memory

    $MemoryAvailable = (Get-VMHostNumaNode | Measure-Object -Property MemoryAvailable -Sum).sum/4  
    $maxRAM = ([math]::round($MemoryAvailable /1024)) * 1GB  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.