Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 无法将参数绑定到参数,因为它是空字符串_Powershell_Powershell Module - Fatal编程技术网

Powershell 无法将参数绑定到参数,因为它是空字符串

Powershell 无法将参数绑定到参数,因为它是空字符串,powershell,powershell-module,Powershell,Powershell Module,从已创建并导入到主脚本中的模块调用函数时,我遇到此错误: 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 ... +

从已创建并导入到主脚本中的模块调用函数时,我遇到此错误:

 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`
在我的模块中,
-Target
定义为如下参数:

[Parameter(Mandatory, Position = 0)][String[]]$Targets,
$Targets = Set-TargetList
在我的主脚本(导入我的模块)中,
$targets
的定义如下:

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

我曾尝试使用
全局
脚本作用域,但这不起作用。

可能是迟来的答案,但以防其他人有类似问题

在我的例子中,它是一个字符串数组。当至少有一个成员字符串为空或null时,它被拒绝


AllowEmptyString
设置为正在调用的参数中的属性,或者在传递成员之前检查其是否为空字符串。

So。。函数
设置TargetList
返回什么。那是一个
[string[]
数组吗?@Theo Yes,
设置目标列表
返回一个
[string[]
数组。@TobiasKKS:你能在脚本中转储
$Targets
的类型吗?您可以使用
$Targets.GetType()
。错误表明
$targets
是空字符串,而不是字符串数组。Thx@Moerwald我做了一个
$Targets.GetType()
引用
$Targets
的地方,它们都返回了
String[]
System.Array
简单回答:
[String]
可以是
$null
。在使用数组之前,需要验证数组中的每个条目:
if($null-ne$item){…}