8

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.

7
  • So.. what does function Set-TargetList return. Is that a [string[]] array? Commented Nov 1, 2018 at 10:44
  • @Theo Yes, Set-TargetList returns a [string[]] array. Commented Nov 1, 2018 at 11:20
  • @TobiasKKS: Can you dump the type of $Targets in your script? You can use $Targets.GetType(). The error says that $targets is an empty string, not a string array. Thx Commented Nov 1, 2018 at 12:04
  • @Moerwald I did a $Targets.GetType() whereever $Targets is referenced, and they all returned String[] and System.Array Commented Nov 1, 2018 at 12:28
  • 2
    Simple answer: [string] can be $null. You need to validate each entry in your array before using it: if ($null -ne $item) { ... } Commented Nov 1, 2018 at 13:50

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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:

Cannot bind argument to parameter '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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.