Home > Software design >  PowerShell & VirtualBox: A positional parameter cannot be found that accepts argument
PowerShell & VirtualBox: A positional parameter cannot be found that accepts argument

Time:08-04

Running this command from the same directory where the ISO and the scripts are:

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

Code:

# 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

And I get this !@#$%^&*()!!!!:

A positional parameter cannot be found that accepts argument '--DiskPath'. At line:1 char:1

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

CodePudding user response:

From the conceptual about_Parameters help topic:

The parameters follow the command name and have the following form:
-<parameter_name> <parameter_value>
-<parameter_name>:<parameter_value>
The name of the parameter is preceded by a hyphen (-) [...]

This tells you that a parameter declared with parameter variable $DiskPath, for instance, must be targeted with
-DiskPath, not --DiskPath - one hyphen, not two.

If you mistakenly use --, the token is considered a positional argument, i.e. a parameter value rather than a name.

If the target command either doesn't support positional arguments or you've already (possibly mistakenly, as in your case) bound all positional parameters with positional arguments, you'll get the error message you saw for any additional positional arguments for which no (remaining) target parameter is declared.

CodePudding user response:

The problem is the operating system powershell.

0- The operating system should have been decommissioned at the end of the last millennium, windows 10 is nothing but windows NT with a fresh coat of paint.

1- Then someone who really hates the world changed / to \

2- Then returned some years later and changed a key directory name from "programs" to "Program Files"

3- Since the escape character had been screwed, then he/she changed it to `

4- Then he/she got high and invented powershell, one more way to slow down technological the advance!

There should be a bounty to catch whoever did that !!!

  • Related