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

**URL:** <https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662>\
**Category:** PowerShell Help\
**Created:** [January 31, 2016, 11:41pm UTC](https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662 "2016-01-31T23:41:53Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![richard-schaefer](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@richard-schaefer](https://forums.powershell.org/u/richard-schaefer)\
**Post date:** [February 1, 2016, 4:34am UTC](https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662/21 "2016-02-01T04:34:57Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 1, 2016, 4:49am UTC](https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662/22 "2016-02-01T04:49:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richard-schaefer](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@richard-schaefer](https://forums.powershell.org/u/richard-schaefer)\
**Post date:** [February 1, 2016, 4:56am UTC](https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662/23 "2016-02-01T04:56:09Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![richard-schaefer](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@richard-schaefer](https://forums.powershell.org/u/richard-schaefer)\
**Post date:** [February 1, 2016, 9:59pm UTC](https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662/24 "2016-02-01T21:59:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662/25 "2024-05-16T20:42:59Z")

</div>



[Previous page](https://forums.powershell.org/t/how-to-remotely-execute-a-ps-script-that-resides-on-a-remote-server/5662.md?page=1)
