调整内存大小时,在 2012 R2 中创建虚拟机的 PowerShell 脚本失败

调整内存大小时,在 2012 R2 中创建虚拟机的 PowerShell 脚本失败

我已经创建了一个基本脚本来快速在 Hyper-V 中添加虚拟机,一切运行正常,直到达到设置虚拟机内存命令。此时变量似乎失败并产生错误。

脚本代码:

$Name = Read-Host -Prompt 'New VM Name'
$ProcessorCount = Read-Host -Prompt 'Processor Count'
$MinimumBytes = Read-Host -Prompt 'Dynamic Memory Minimum'
$MemoryStartupBytes = Read-Host -Prompt 'Memory Startup Bytes'
$MaximumBytes = Read-Host -Prompt 'Dynamic Memory Maximum'
$Priority = Read-Host -Prompt 'Dynamic Memory Priority'
$Buffer = Read-Host -Prompt 'Dynamic Memory Buffer'
$VlanId = Read-Host -Prompt 'VLAN ID'

New-VM -Name "$Name" -Path H:\VM –NewVHDPath H:\VHD\$Name\$Name.VHDX -NewVHDSizeBytes 64GB -SwitchName "INFRASTRUCTURE"
Set-VM -Name "$Name" -ProcessorCount "$ProcessorCount"
Set-VMMemory "$Name" -DynamicMemoryEnabled $true -MinimumBytes $MinimumBytes -StartupBytes $MemoryStartupBytes -MaximumBytes $MaximumBytes -Priority $Priority -Buffer $Buffer
Set-VMNetworkAdapterVlan –VMName "$Name" –Access –VlanId "$VlanId"

错误代码:

Set-VMMemory : 'test' failed to modify device 'Memory'. (Virtual machine ID 17109661-11E1-4213-97DA-19C5847C8F87)
Invalid startup memory amount assigned for 'test'. The minimum amount of memory you can assign to this virtual machine is '32' MB. (Virtual machine ID 17109661-11E1-4213-97DA-19C5847C8F87)
A parameter that is not valid was passed to the operation.
At line:12 char:1
+ Set-VMMemory "$Name" -DynamicMemoryEnabled $true -MinimumBytes $MinimumBytes -St ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Microsoft.HyperV.PowerShell.VMTask:VMTask) [Set-VMMemory], VirtualizationOperationFailedException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMMemoryCommand

我尝试过改变语法,但最终得到的结果相同或相似。如果您能提供任何关于问题出在哪里的见解,我将不胜感激。

修正脚本并添加了一些默认值选项: 再次感谢@Peter Hahndorf 为我指明正确的方向。

$Name = Read-Host -Prompt 'New VM Name'
$ProcessorCount = Read-Host "Processor Count (Press [Enter] to choose 2): "
    if ($ProcessorCount -eq ""){$ProcessorCount="2"} ; if ($ProcessorCount -eq $NULL){$ProcessorCount="2"}
[long]$MinimumBytes = Invoke-Expression (Read-Host -Prompt 'Dynamic Memory Minimum')
[long]$MemoryStartupBytes =  Invoke-Expression (Read-Host -Prompt 'Memory Startup Bytes')
[long]$MaximumBytes =  Invoke-Expression (Read-Host -Prompt 'Dynamic Memory Maximum')
[long]$VHDiskSize =  Invoke-Expression (Read-Host -Prompt 'VHDX Size')
$Priority = Read-Host "Dynamic Memory Priority (Press [Enter] to choose 50): "
    if ($Priority -eq ""){$Priority="50"} ; if ($Priority -eq $NULL){$Priority="50"}
$Buffer = Read-Host "Dynamic Memory Buffer (Press [Enter] to choose 20): "
    if ($Buffer -eq ""){$Buffer="20"} ; if ($Buffer -eq $NULL){$Buffer="20"}
$VlanId = Read-Host "Select Vlan ID (Press [Enter] to choose 2): "
    if ($VlanId -eq ""){$VlanId="2"} ; if ($VlanId -eq $NULL){$VlanId="2"}
New-VM -Name "$Name" -Path H:\VM –NewVHDPath H:\VHD\$Name\$Name.VHDX -NewVHDSizeBytes $VHDiskSize -SwitchName "INFRASTRUCTURE"
Set-VM -Name "$Name" -ProcessorCount "$ProcessorCount"
Set-VMMemory "$Name" -DynamicMemoryEnabled $true -MinimumBytes "$MinimumBytes" -StartupBytes "$MemoryStartupBytes" -MaximumBytes "$MaximumBytes" -Priority "$Priority" -Buffer "$Buffer"
Set-VMNetworkAdapterVlan –VMName "$Name" –Access –VlanId "$VlanId"

答案1

使用 Read-Host 时,您会得到一个字符串,并且 KB/GB 运算符的处理方式与预期整数时不同。您需要将输入转换为整数,例如:

 [int]$MinimumBytes = Invoke-Expression (Read-Host -Prompt 'Dynamic Memory Minimum')
 [int]$MemoryStartupBytes =  Invoke-Expression (Read-Host -Prompt 'Memory Startup Bytes')
 [int]$MaximumBytes =  Invoke-Expression (Read-Host -Prompt 'Dynamic Memory Maximum')

我几乎从不使用 Read-Host,脚本参数对于获取用户输入的功能更强大,您不会遇到像这样的问题。

在脚本的开头您有:

 param(
     [parameter(Mandatory=$true)]
     [string]$Name,
     [int]$ProcessorCount = 2,
     [long]$MinimumBytes = 512MB
     ...
 )

然后你可以像这样调用该脚本:

ScriptName.ps1 -Name "new VM" -ProcessCount 4

参数非常有用,更多信息请访问 TechNet:关于参数关于功能高级参数

相关内容