Cypher236
Cypher236

Reputation: 527

How do I pass parameters to the batch file in this PowerShell script?

I have syntax similar to the below in PowerShell:

param($1,$2,$3,$4)
$Session = New-PSSession -ComputerName $1
$scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock("cmd /c $2 $3 $4")
Invoke-Command -Session $Session -ScriptBlock $scriptblock

Where $2 is a .bat and $3 & $4 are arguments I want to pass to the executable. If I run it without arguments I can run the .bat but when I add in the arguments it fails. What am I doing wrong?

Edit: To provide further information, I am calling the above by running this in PowerShell:

-ExecutionPolicy Bypass -F "powershellscript for the above" -1 "value" -2 "value" -3 "value" -4 "value"

Upvotes: 1

Views: 320

Answers (1)

Giorgi Chakhidze
Giorgi Chakhidze

Reputation: 3459

To use local variables inside remote session, you must prefix them with $Using: scope modifier.

Try this:

param($1,$2,$3,$4)
$Session = New-PSSession -ComputerName $1
$scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock({ cmd /c $Using:2 $Using:3 $Using:4 })
Invoke-Command -Session $Session -ScriptBlock $scriptblock

Upvotes: 2

Related Questions