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!