Home > Back-end >  Running Invoke-Command to Stop-Process while passing a variable not working
Running Invoke-Command to Stop-Process while passing a variable not working

Time:10-27

The syntax below absolutely works to stop a process on a remote computer:

$hostname = 'PC1'

$process = 'program1*'

$Session = New-PSSession $Hostname

Invoke-Command -Session $Session -ScriptBlock {param($process) Stop-Process -ProcessName $process -Force} -ArgumentList $process

$Session | Remove-PSSession

However, in Jenkins, I parameterized hostname and process, so the user enters the input hostname and process, and Jenkins creates the two variables $env:hostname and $env:process. This is not working well, the argument is not being passed onto Stop-Process:

$session = New-PSSession $env:hostname

Invoke-Command -Session $session -ScriptBlock {param($env:process) Stop-Process -ProcessName $env:process -Force} -ArgumentList $env:process

$Session | Remove-PSSession

The error I'm getting is

Cannot bind argument to parameter 'Name' because it is null.
At C:\Users\user.name\AppData\Local\Temp\jenkins10480966582412717483.ps1:25 char:1
  Invoke-Command -Session $session -ScriptBlock {param($env:process) St ...
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (:) [Stop-Process], ParameterBindingValidationException
      FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StopProcess 
   Command
      PSComputerName        : pc1
 
Build step 'PowerShell' marked build as failure
Finished: FAILURE

I know this has something to do with quotes, please give me a hand, thank you!

CodePudding user response:

Don't change anything about your Invoke-Command call except the input argument, i.e. what you pass to -ArgumentList:

Invoke-Command `
  -Session $Session `
  -ScriptBlock { param($process) Stop-Process -ProcessName $process -Force } `
  -ArgumentList $env:process

Don't ever use an environment-variable reference to define a parameter variable: param($env:process)

The name of the parameter variable, as declared inside the param(...) block, is unrelated to whatever value you pass to it via -ArgumentList (i.e., it is unrelated to what the name of the variable is that you happen to be using to pass that value, and whether that variable is a regular (shell-only) variable or an environment variable), and inside the -ScriptBlock argument you must only refer to the value via the parameter variable.

See the relevant section of the conceptual about_Functions help topic, which, with respect to the params(...) method of declaring parameters, applies equally to script blocks ({ ... })

CodePudding user response:

Or if this makes you feel more at ease...

$Session = New-PSSession $env:Hostname
$Process = $ENV:Process

Invoke-Command -Session $Session -ScriptBlock {
    Stop-Process -ProcessName $Using:Process -Force 
}
  • Related