6

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.

Tobias KKS
  • 130
  • 1
  • 4
  • 17
  • So.. what does function `Set-TargetList` return. Is that a `[string[]]` array? – Theo Nov 01 '18 at 10:44
  • @Theo Yes, `Set-TargetList` returns a `[string[]]` array. – Tobias KKS Nov 01 '18 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 – Moerwald Nov 01 '18 at 12:04
  • @Moerwald I did a `$Targets.GetType()` whereever `$Targets` is referenced, and they all returned `String[]` and `System.Array` – Tobias KKS Nov 01 '18 at 12:28
  • Which version of PowerShell are you using? Type `$PSVersionTable`. You can also try to wrap `$targets` in another array -> `Run-RemoteScript -Targets (,$targets) .. `. – Moerwald Nov 01 '18 at 13:30
  • 2
    Simple answer: `[string]` can be `$null`. You need to validate each entry in your array before using it: `if ($null -ne $item) { ... }` – Maximilian Burszley Nov 01 '18 at 13:50
  • I'm on v5. Wrapping `$targets` seems to work, but I'm getting an error saying `Invoke-Command : One or more computer names are not valid.` – Tobias KKS Nov 02 '18 at 07:16

2 Answers2

15

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.

Vadim Berman
  • 1,932
  • 1
  • 20
  • 39
0

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.

Ogglas
  • 62,132
  • 37
  • 328
  • 418