Cannot bind argument to parameter because it is an empty string

Viewed 16116

I am getting this error when calling a function from a module that I have created and imported into my main script:

 Run-RemoteScript : Cannot bind argument to parameter 'Targets' because it 
is an empty string.
At C:\Scripts\Script.ps1:114 char:39
+             Run-RemoteScript -Targets $targets -RunMethod $runMethod  ...
+                                       ~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Run-RemoteScript], 
ParameterBindingValidationException
+ FullyQualifiedErrorId : 
ParameterArgumentValidationErrorEmptyStringNotAllowed,Run-RemoteScript`

In my module, -Target is defined as a parameter like this:

[Parameter(Mandatory, Position = 0)][String[]]$Targets,

In my main script (which imports my module), $targets is defined like this:

$Targets = Set-TargetList

I have tried using a global script scope, but this did not work.

2 Answers

Probably a late answer, but in case others have a similar issue.

In my case, it was an array of strings. It was rejected when at least one of the member strings was empty or null.

Either set AllowEmptyString as an attribute in the parameter you're calling, or check whether the members are not empty strings before passing them.

Got this error when using a AzureResourceGroup project in Visual Studio 2022 after downloading a template from Azure Portal.

enter image description here

Even though I was using a parameter file from Azure with every parameter needed Visual Studio opened this Powershell window:

enter image description here

When not typing a name I got the error below in Visual Studio Output:

r 'nameFromTemplate' because it is an empty string.

Turns out Visual Studio AzureResourceGroup project can not handle a parameter named "name" even though Microsofts own template generator generates this. After renaming the parameter to appName everything worked.

Related