AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 790133
Accepted
Confusias
Confusias
Asked: 2016-07-16 13:05:39 +0800 CST2016-07-16 13:05:39 +0800 CST 2016-07-16 13:05:39 +0800 CST

调整内存大小时,用于在 2012 R2 中创建 VM 的 PowerShell 脚本失败

  • 772

我创建了一个基本脚本以在 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"
powershell hyper-v-server-2012-r2
  • 1 1 个回答
  • 1747 Views

1 个回答

  • Voted
  1. Best Answer
    Peter Hahndorf
    2016-07-16T22:40:58+08:002016-07-16T22:40:58+08:00

    使用 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 上的更多信息:about_Parameters和about_Functions_Advanced_Parameters

    • 3

相关问题

  • 资源锁和电源外壳

  • 脚本 - 如何断开远程桌面会话?

  • 如何限制向通讯组发送到外部地址?

  • Powershell对值与数组的作用不同?

  • Windows Powershell Vim 键绑定

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve