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){…}