# How to remotely execute a PS Script that resides on a remote server?

The log files were ending up in my profile somewhere, at least that’s what Search was turning up, although I couldn’t access them.

Changed the Out-File to specify the full path and it now works correctly from both local and remote.

So, back to how to specify the script path and parameters programmatically… I can’t use this:

Invoke-Command -ComputerName ad1hfdahp802 -ScriptBlock {
d:\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1 INT
}


in real life…

Right. So, now that we’ve got the remote script working, we can work on that. Let’s do just one thing. I find it’s often easier to walk into things one step at a time, so if something breaks, you know exactly what it is.

Try 1:

Invoke-Command -ComputerName ad1hfdahp802 -ScriptBlock {
param($a) d:\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1$a
} -Arg 'INT'


Try 2:

$a = 'INT' Invoke-Command -ComputerName ad1hfdahp802 -ScriptBlock { d:\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1$using:a
}


I’m just curious to see what happens either way. Do make sure you’re running the shell as someone with remote access to the remote machine - normally, someone who’s in the local Administrators group on the remote box.

Try 1 and Try 2 both worked. Given that the server name is outside the ScriptBlock and Arg list I put that in a variable also:

$s='ad1hfdahp802'$a = 'INT'
Invoke-Command -ComputerName $s -ScriptBlock { d:\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1$using:a }


which worked fine.

I poked at #1 some more and tried the following which also works:

$s='ad1hfdahp802'$arg='INT'
Invoke-Command -ComputerName $s -ScriptBlock { param($a)
d:\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1 $a } -Arg$arg


So the next thing is how to calculate the path to the ps1 file in a variable outside the ScriptBlock and then use that variable inside the ScriptBlock…

Just to be certain it wasn’t just a problem with the PS install on my computer I ran this from above on another computer also running 4.0:

$server='ad1hfdahp802'$scriptpath='d:\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1'
$SDFEnvironment='INT' Invoke-Command -ComputerName$server -ScriptBlock {$using:scriptpath$using:sdfenvironment}


With the same result. In the Script window of the ISE I get a squiggly under the second “using” and if I hover over it the pop-up says “Unexpected token ‘$using:SDFEnvironment’…” You don’t get this error if you put this code into the Script window of the ISE? Can you share your$PSVersionTable?