无法将参数绑定到参数“command”,因为它为null,PowerShell

xzabzqsa  于 2023-03-30  发布在  Shell
关注(0)|答案(5)|浏览(622)

我有一个类似于下面代码的函数。它接收命令和命令参数。我必须在后台运行这个命令并收集输出。但最后一条语句让我感到困惑
错误:

Cannot bind argument to parameter 'Command' because it is null.
+ CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpre
ssionCommand
+ PSComputerName        : localhost

验证码:

$cmd = 'Get-content'
$Arg = 'path to file'
$sb = "$cmd $Arg -ErrorVariable e -ErrorAction Stop"
invoke-Expression $sb #This printsoutput
$job = Start-job -ScriptBlock {Invoke-Expression $sb}
wait-job -id $job.Id
$job | Receive-job #this should print output but throwing error

我很确定最后一行是抛出错误的那一行。

bgtovc5b

bgtovc5b1#

除了-argumentlist之外,将$sb放入脚本块作用域的另一种方法是使用$using:作用域。(PowerShell 3+)

$cmd = 'Get-content'
$Arg = 'path to file'
$sb = "$cmd $Arg -ErrorVariable e -ErrorAction Stop"
$job = Start-job -ScriptBlock {Invoke-Expression $using:sb}
wait-job -id $job.Id
$job | Receive-job
kiayqfof

kiayqfof2#

这里的问题是您实际上并没有给Invoke-Expression一个命令。
每当你创建一个新的上下文(在本例中是一个作业),你就失去了对父会话环境的访问权。
管理此操作的方法是通过Start-Job-ArgumentList参数将值作为参数传递:

start-job -ScriptBlock {} -ArgumentList

为了方便将$sb交给ScriptBlock,你可以这样做:

$sb = "$cmd $Arg -ErrorVariable e -ErrorAction Stop"
$job = start-job -ScriptBlock { Param([string]$sb)
    Invoke-Expression $sb
} -ArgumentList $sb

这可能会让人感到困惑,所以这是用更友好的名称编写的相同代码:

$OuterSB = "$cmd $Arg -ErrorVariable e -ErrorAction Stop"
$job = start-job -ScriptBlock { Param([string]$InnerSB)
    Invoke-Expression $InnerSB
} -ArgumentList $OuterSB
sqserrrh

sqserrrh3#

我得到了同样的错误,但当我使用管理员权限运行PowerShell时,我没有得到此错误。祝你好运!

4c8rllxm

4c8rllxm4#

我也得到了相同的错误时运行命令与用户没有适当的授权.这在事实上可以相关.例如我的代码:

$allParams = @(10, "Some Process")

Start-Job -ScriptBlock $restartProcessRobot -ArgumentList $allParams

<#
@descr
    Function that restarts given proces -DisplayName after given number of seconds.
@param1 Integer number of seconds after which the process should be restarted.
@param2 String  display name of process(es) that must be affected. Wild cards accepted.
#>
$restartServiceAfter = { 
    param($seconds, $processName)
    Start-Sleep $seconds
    Restart-Service -DisplayName $processName -Force
}

用户无权重新启动服务。

mznpcxlj

mznpcxlj5#

当我的conda环境的名称中有一个错字时,我得到了这个错误:它是env2而不是env3

(base) PS C:\Users\mc\Documents\GitHub> conda activate env3

EnvironmentNameNotFound: Could not find conda environment: env3
You can list all discoverable environments with `conda info --envs`.

Invoke-Expression : Cannot bind argument to parameter 'Command' because it is an empty string.
At C:\ProgramData\Anaconda3\shell\condabin\Conda.psm1:107 char:36
+         Invoke-Expression -Command $activateCommand;
+                                    ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Invo
   keExpressionCommand

(base) PS C:\Users\mc\Documents\GitHub> conda activate env2
(env2) PS C:\Users\mc\Documents\GitHub>

相关问题