Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Args/params not being populated

Tags:

powershell

I have a PowerShell script:

param (
    [Parameter(Mandatory=$true)][string]$input,
    [Parameter(Mandatory=$true)][string]$table
)

Write-Host "Args:" $Args.Length
Get-Content $input |
    % { [Regex]::Replace($_, ",(?!NULL)([^,]*[^\d,]+[^,]*)", ",'`$1'") } |
    % { [Regex]::Replace($_, ".+", "INSERT INTO $table VALUES (`$1)") }

The Write-Host part is for debugging.
I run it as .\csvtosql.ps1 mycsv.csv dbo.MyTable (from powershell shell), and get

Args: 0
Get-Content : Cannot bind argument to parameter 'Path' because it is an empty s
tring.
At C:\temp\csvtosql.ps1:7 char:12
+ Get-Content <<<<  $input |
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBinding
   ValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
   lowed,Microsoft.PowerShell.Commands.GetContentCommand

I get exactly the same error with any parameters that I pass, also the same error if I try to use named parameters.

What can cause parameters not to be passed in?

UPDATE: PowerShell ISE asks me for these parameters using GUI prompts, then gives me the same error about them not being passed in.

like image 908
Andrey Shchekin Avatar asked Jun 13 '12 07:06

Andrey Shchekin


2 Answers

Unless you marked a parameter with the ValueFromRemainingArguments attribute (indicates whether the cmdlet parameter accepts all the remaining command-line arguments that are associated with this parameter), Args is "disabled". If all you need is the arguments count call the special variable:

$PSBoundParameters.Count
like image 192
Shay Levy Avatar answered Nov 07 '22 08:11

Shay Levy


  • Do not mix. Make use of $args or parameters.
  • Also do note that $input is a special variable, don't declare it as a parameter. http://dmitrysotnikov.wordpress.com/2008/11/26/input-gotchas/
like image 3
Emiliano Poggi Avatar answered Nov 07 '22 10:11

Emiliano Poggi