0

I have two computers on the same domain. winrm is running on both machines. Test-WSMan is running on the remote machine. I can execute invoke-command -scriptblock {dir c:\automatedtests} -computername RemoteComputername and it returns the expected result:

Directory: C:\automatedtests


Mode                LastWriteTime         Length Name                                PSComputerName

d-----        1/11/2022   5:45 PM                ErrorLog.txt                        us14-pacci04
a-----        1/13/2022  12:16 PM             18 test.ps1                            us14-pacci04

. But if I try to execute a script on that same machine (invoke-command -computername 'RemoteComputerName' -filepath 'C:\AutomatedTests\test.ps1') I get the error:

invoke-command : Cannot find path 'C:\AutomatedTests\test.ps1' because it does not exist.
At line:1 char:1
+ invoke-command -session $s -filepath {C:\AutomatedTests\test.ps1}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (C:\AutomatedTests\test.ps1:String) [Invoke-Command], ItemNotFoundExcepion
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

I have been googling this for days with no solution. What am I missing? It's got to be something simple that I'm missing.

Edit 1/18/22 1:18pm PT: OK - I understand now that -filepath refers to a local file. So the only way to run a script on a remote computer (with the script stored there) is to use the share?

Ben_G
  • 770
  • 2
  • 8
  • 30
  • 3
    The `-FilePath` parameter is looking for that file on your localhost. _"Specifies a local script that this cmdlet runs on one or more remote computers."_ – Santiago Squarzon Jan 18 '22 at 19:56
  • 2
    Does `C:\AutomatedTest\test.ps1` exist locally? The `-FilePath` parameter "Specifies a local script that this cmdlet runs on one or more remote computers." – Lance U. Matthews Jan 18 '22 at 19:56
  • 2
    What was said above. In other words, remove the `{...}` braces from around the path: `invoke-command -session $s -filepath C:\AutomatedTests\test.ps1` – Mathias R. Jessen Jan 18 '22 at 19:56
  • Does this answer your question? [Invoke-Command invocation of server script error](https://stackoverflow.com/questions/16194488/invoke-command-invocation-of-server-script-error) – Lance U. Matthews Jan 18 '22 at 20:05
  • Yes - that describes why it's not working as written. So I guess the only way to run it on the remote computer (with the script stored there) is to use the share? – Ben_G Jan 18 '22 at 21:16
  • 1
    I would post it as answer but I'm not 100% sure it would work. If you need to "invoke" that remote script, in the script block of your `Invoke-Command`, try with `{ & 'c:\automatedtests\test.ps1 }` – Santiago Squarzon Jan 18 '22 at 22:01
  • @mklement0 the thing is, i'm not aware of what `test.ps1` is, if it's attempting to connect to a third host he would get the double-hop issue (which Tessellating noted) – Santiago Squarzon Jan 19 '22 at 00:49
  • @mklement0 - that does not work. Trying this: invoke-command -computername 'us14-pacci04' -filepath { & 'C:\AutomatedTests\test.ps1'} I get this error. Same with { . 'C:\AutomatedTests\test.ps1'} invoke-command : The value of the FilePath parameter must be a Windows PowerShell script file. Enter the path to a file with a .ps1 file name extension and try the command again. – Ben_G Jan 19 '22 at 17:01
  • @Ben_G: Do not use `-FilePath` - use `-ScriptBlock` (or use the script block positionally), as now shown in Santiago's answer. As noted previously, `-FilePath` is for copying the _content_ of a script _from the caller_ to the remote computer, for execution there. – mklement0 Jan 19 '22 at 17:10

2 Answers2

1

You can invoke the script on the remote host using the call operator &:

Invoke-Command -ScriptBlock { & 'c:\automatedtests\test.ps1'} -ComputerName RemoteComputer

Or the dot sourcing operator .:

Invoke-Command -ScriptBlock { . 'c:\automatedtests\test.ps1'} -ComputerName RemoteComputer
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
0

You can use $sess = New-PSSession... to connect to the remote machine, then Copy-Item -ToSession $sess ... to copy the file over to a folder on the remote machine, then Invoke-Command -Session $sess ... to run it on the remote machine.

(This saves you the double-hop problem of running a command on the remote machine which needs to connect out to a fileshare)

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87