我创建了一个基本脚本以在 Hyper-V 中快速添加一个 VM,并且一切正常,直到它到达Set-VMMemory命令。此时变量似乎失败并产生错误。
脚本代码:
$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"
使用 Read-Host 时,您将获得一个字符串,并且 KB/GB 运算符的处理方式与预期整数时不同。您需要将输入转换为整数,例如:
我几乎从不使用 Read-Host,脚本参数对于获取用户输入要强大得多,而且您不会遇到这样的问题。
在脚本的开头,您有:
然后,您将像这样调用脚本:
参数非常有用,TechNet 上的更多信息:about_Parameters和about_Functions_Advanced_Parameters