Cannot bind parameter to argument 'command' because it is null. Powershell

Viewed 13592

I had a function similar to below code. It receives command and the command arguments. I had to run this command in background and collect the output. But that last statement is bugging me with this error

Error:

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

Code:

$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

I am pretty sure last line is the one throwing the error.

4 Answers

I got the same error but when I run PowerShell using administrator privileges, I don't get this error. Good luck!

I also got the same error when running command with user without proper authorizations. This in deed can be related to that. For example my code:

$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
}

And user is not authorized to restart services.

Related