我创建了以下函数以从 csv 文件传递内存以在 Hyper-V 版本 3 中创建 VM
Function Install-VM {
param (
[Parameter(Mandatory=$true)]
[int64]$Memory=512MB
)
$VMName = "dv.VMWIN2K8R2-3.Hng"
$vmpath = "c:\2012vms"
New-VM -MemoryStartupBytes ([int64]$memory*1024) -Name $VMName -Path $VMPath -Verbose
}
Import-Csv "C:\2012vms\Vminfo1.csv" | ForEach-Object { Install-VM -Memory ([int64]$_.Memory) }
但是当我尝试创建虚拟机时,它说从 import-csv 传递的内存参数不匹配,我收到如下错误
VERBOSE: New-VM will create a new virtual machine "dv.VMWIN2K8R2-3.Hng".
New-VM : 'dv.VMWIN2K8R2-3.Hng' failed to modify device 'Memory'. (Virtual machine ID
CE8D36CA-C8C6-42E6-B5C6-2AA8FA15B4AF)
Invalid startup memory amount assigned for 'dv.VMWIN2K8R2-3.Hng'. The minimum amount of memory you can assign to
a virtual machine is '8' MB. (Virtual machine ID CE8D36CA-C8C6-42E6-B5C6-2AA8FA15B4AF)
A parameter that is not valid was passed to the operation.
At line:48 char:9
+ New-VM -ComputerName $HyperVHost -MemoryStartupBytes ([int64]$memory*10 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.HyperV.PowerShell.VMTask:VMTask) [New-VM], VirtualizationOpe
rationFailedException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.NewVMCommand
也请不要在 csv 文件中将内存作为 1,2,4.. 等传递,如下所示,然后通过将它们乘以 1024 将它们转换为 MB
记忆1
任何人都可以帮助我了解如何格式化并将内存详细信息传递给函数