Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

positional parameter cannot be found that accepts argument

Tags:

powershell

if ($mbcb1.Checked -eq $true) {$dgr = "-AutoStart"}

if ($mbcb2.Checked -eq $true) {$dgrc = "-AutoComplete"}

if ($mbcb3.Checked -eq $true) {$dgren = "-NotificationEmails"}

New-MigrationBatch -Name $mbnd -SourceEndpoint $mbcx -TargetDeliveryDomain $mbtdd -CSVData ([System.IO.File]::ReadAllBytes("$cmbcsvfile")) $dgr $dgrc $dgren [email protected]


Error : 
A positional parameter cannot be found that accepts argument '-Autostart'.
    + CategoryInfo          : InvalidArgument: (:) [New-MigrationBatch], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,New-MigrationBatch
    + PSComputerName        : ps.outlook.com

if i given direct input its working but passing as variable throwing error.

like image 243
Sudhagar Rajaraman Avatar asked Feb 06 '26 17:02

Sudhagar Rajaraman


1 Answers

If you want to optionally specify parameters, use splatting:

$OptionalParameters = @{
  AutoStart = $mbcb1.Checked
  AutoComplete = $mbcb2.Checked
}
if ($mbcb3.Checked) {
  $OptionalParameters["NotificationEmails"] = '[email protected]'
}

New-MigrationBatch -Name $mbnd -SourceEndpoint $mbcx -TargetDeliveryDomain $mbtdd -CSVData ([System.IO.File]::ReadAllBytes("$cmbcsvfile")) @OptionalParameters

We simply build a hashtable with the parameter names and their arguments, and then supply it to the cmdlet as an argument (but like @name instead of $name), and then the parser will turn each entry in the hashtable into a named parameter in the form -key:value.
Finally, the $mbcb3.Checked -eq $true comparison is redundant, since Checked (assuming that $mbcb3 is a checkbox) is already either $true or $false

See the about_Splatting help file for more details about parameter splatting

like image 76
Mathias R. Jessen Avatar answered Feb 09 '26 08:02

Mathias R. Jessen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!