我使用XML文件来存储创建虚拟机的配置。我的Powershell脚本创建了VM,但在尝试将MemoryStartup
值传递给new-vm
函数时出现了此错误。
我试图通过在变量前面指定[int64]来解决这个问题,但没有成功。我可以从XML文件中读取数据,但它将其视为字符串,函数要求其为int64。
在我的xml文件中,我放入:4096MB
在我的powershell脚本中:
new-vm -ComputerName $Using:Hostname -name $Using:VMName -MemoryStartupBytes [int64]$Using:MemoryStartup
错误:
Cannot bind parameter 'MemoryStartupBytes'. Cannot convert value "[int64]4096MB" to type "System.Int64". Error: "Input string was not in a correct format."
+ CategoryInfo : InvalidArgument: (:) [New-VM], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.HyperV.PowerShell.Commands.NewVM
我似乎找不到任何显式的方法来在Powershell中执行这种类型的强制转换,因为它似乎使用本机隐式函数将内存值转换为整数。然而,通过尝试数字运算来进行隐式强制转换似乎可以做到这一点:
"512MB"/1
536870912
因此:
new-vm -ComputerName $Using:Hostname -name $Using:VMName -MemoryStartupBytes [int64]$($MemoryStartup/1)
在Powershell中,如何从字符串转换为UInt64?字符串到数字的转换。一定要跳过去给上级消息来源投赞成票。
找到了一个解决方案:
XML文件:4GB
Powershell文件:新vm-ComputerName$Hostname-name$VMName-MemoryStartupBytes(调用表达式$MemoryStartup(-第2代-路径$LocationPath
通过使用Invoke表达式,它的工作方式就像一个魅力