PowerShell 和 VirtualBox:找不到接受参数的位置参数

问题描述 投票:0回答:1

从 ISO 和脚本所在的同一目录运行此命令:

.\CreateVM.ps1 --$ImagePath My.iso --DiskPath C:\Users admf\VM

代码:

# This script creates a Win 10 on VirtualBox

Param (
    [Parameter(Mandatory)]

    [string]$ImagePath,

    [Parameter(Mandatory)]

    [string]$DiskPath  

    )

$VMName = 'RTU'

$VBManage = 'C:\Program Files\oracle\VirtualBox\VBManage.exe'

# Destroy the VM if it already exists
Invoke-Expression "& 'DestroyVM.ps1' $VMName"

# Create the VM

$VBManage createvm --name $VMName --basefolder $ImagePath --register --ostype Win10

& $VBManage modifyvm $VMName --memory 8048 --vram 256

& $VBManage createhd --filename $DiskPath --size 80000 --format VMDK

& $VBManage storagectl $VMName --name SATA --add sata

& $VBManage storageattach $VMName --storagectl SATA --port 0 --device 0 --type hdd
 
& $VBManage storageattach $VMName --storagectl SATA --port 1 --device 0 --type dvddrive --medium $ImagePath

& $VBManage startvm $VMName

我明白了:

找不到接受参数“--DiskPath”的位置参数。 在行:1 字符:1

  • .\V0.ps1 --$ImagePath My.iso --DiskPath C:\Users\ ...
  •   + CategoryInfo          : InvalidArgument: (:) [V0.ps1], ParameterBindingException
      + FullyQualifiedErrorId : PositionalParameterNotFound,V0.ps1
    
    

来自相同的修改脚本:

$VMName = "RTU"
$VBManage = 'C:\Program` Files\Oracle\VirtualBox\VBoxManage.exe'


# Destroy the VM if it already exists
$ExistingVMs = Invoke-Expression "$VBManage list vms"
if ([bool]($ExistingVMs -match $VMName)) {
    Invoke-Expression "$vboxmanage`  controlvm` $vmName` poweroff"
    Start-Sleep -Seconds 5
    Invoke-Expression "$vboxmanage` 'unregistervm` $vmName` --delete"
}
# Create the RTU VM
Invoke-Expression "$VBManage createvm --name $VMName --basefolder $ImagePath --register --ostype Win10"

这些参数被传递给 VirtualBox。

powershell virtualbox
1个回答
2
投票

从概念性 about_Parameters 帮助主题:

参数跟在命令名称后面,格式如下:

-<parameter_name> <parameter_value>

-<parameter_name>:<parameter_value>

参数名称前面有连字符 (-)[...]

这告诉您,例如,使用参数变量

$DiskPath

 声明的参数必须以 

-DiskPath
 为目标,
而不是 --DiskPath
 - 
一个连字符,而不是两个

如果您错误地使用

--

,该标记将被视为
位置参数,即参数而不是名称。

如果目标命令不支持位置参数,或者您已经(可能是错误的,如您的情况)将所有位置参数与位置参数绑定在一起,您将收到您看到的任何其他位置参数的错误消息,其中没有声明(剩余的)目标参数。

© www.soinside.com 2019 - 2024. All rights reserved.