Powershell: Args / params not populated

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)") } 

Write-Host part is for debugging.
I run it as .\csvtosql.ps1 mycsv.csv dbo.MyTable (from powershell) 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 passed, also the same errors if I try to use named parameters.

What can lead to the fact that the parameters will not be transferred?

UPDATE : PowerShell ISE asks me for these parameters using GUI hints, and then gives me the same error that they are not being reported.

+6
source share
3 answers
+2
source

If you did not mark the parameter with the ValueFromRemainingArguments attribute (indicates whether the cmdlet parameter accepts all other command-line arguments associated with this parameter), Args is disabled. If you only need count arguments, call a special variable:

 $PSBoundParameters.Count 
+4
source

You call your script with positional parameters (i.e. without a name), and PowerShell does not know how to map them to script parameters. You need to either call your script using parameter names:

 .\csvtosql.ps1 -input mycsv.csv -table dbo.MyTable 

or update your script to indicate the preferred order of positional parameters:

 param ( [Parameter(Mandatory=$true,Position=0)] [string] $input, [Parameter(Mandatory=$true,Position=1)] [string] $table ) 
-1
source

Source: https://habr.com/ru/post/917936/


All Articles