29

I was trying to execute a script in remote computer.

I did "Enable-PSremoting" in the remote machine.

I placed a script hello.ps1 in remote machine.

[My client machine is Windows XP and remote computer is Windows 2003 ]

Then from my client computer i was trying to execute the script.

invoke-command -computer $MachineName -filepath "C:\hello.ps1"

I got the following error.

Invoke-Command : Cannot find path 'C:\hello.ps1' because it does not exist.

I think it tries to find script from client machine.

If i try to run

invoke-command -computer $MachineName -command { C:\hello.ps1 } , It executes the script available in remote machine in client side.

But i would like to execute the remote script in remote machine itself.

How to make it to run the script available in remote machine?

Updated:

Actually this command "invoke-command -computer $MachineName -command { C:\hello.ps1 }" works in remote side and returned the result to client side. I misunderstood by seeing the return values that it is executing at client side.

1
  • 4
    I love you. 5 years after your thread is posted, after days and days of googling and overflowing and trying every configuration of Invoke-yadayada, almost ready to give up and I see your "Update:" section above and try just that. It gave me same error but I hooked a Remote session in a variable in your code and finally have something that works: ::::::::: $s = New-PSSession -ComputerName "WTxxxxxL32" -Credential $credential ::::: Invoke-Command -Session $s -Command {D:\ServerDLLDev\RemoteCOMInstall.ps1}
    – JustJohn
    May 5, 2017 at 22:14

3 Answers 3

16

When you write :

invoke-command -computer $MachineName -filepath "C:\hello.ps1"

The script C:\hello.ps1 will be taken from the client computer and brought to the server to be executed. So you've got the error file does not exist because Invoke-Command is looking for the file in the client computer.

5
  • How about giving parameters to the remote script? Dec 2, 2015 at 11:17
  • 3
    have a look to -ArgumentList.
    – JPBlanc
    Dec 2, 2015 at 12:15
  • @JPBlanc I'm having difficulty finding out how to use -ArgumentList option to execute the remote file on the remote machine specified Sep 7, 2016 at 16:11
  • Which kind of difficuktis ? Your script takes arguments (using $argv) and you give the values in -ArgumentList.
    – JPBlanc
    Sep 8, 2016 at 3:12
  • How can I use a filepath on the server? -filepath "server:C:\hellp.ps1"? I don't want to mount the drive. Thanks a lot!
    – FelixH
    Aug 3, 2020 at 6:48
1

I got the same error but I hooked a Remote session in a variable in your code and finally have something that works:

$s = New-PSSession -ComputerName "WTxxxxxL32" -Credential $credential

Invoke-Command -Session $s -Command {D:\ServerDLLDev\RemoteCOMInstall.ps1}

There are a million Invoke-etc solutons but the simplest worked finally for me. Thanks to you.

4
  • This means you are running the powershell command on your VM from your local machine??
    – ChanGan
    Sep 9, 2017 at 13:41
  • No VM involved. Client computer and Server. The New-PSSession creates a session to run code on the Remote server. Invoke-Command for the session then runs the script which resides on the server. Before I had the code on client, but that created an error.
    – JustJohn
    Sep 12, 2017 at 18:18
  • My server machine is on other domain. this solution will work??
    – ChanGan
    Sep 13, 2017 at 2:01
  • My server is on other domain. Try it. The session is based on computer name just like how SSMS connects (don't quote me). Essentially, the same credentials you use to connect with RDP from client is same credentials. You notice that I reference "$credential" because I loaded the username/pword into that because I use it several times. Try it with hard coded username/pword.
    – JustJohn
    Sep 13, 2017 at 15:15
-2

It's ok just change the order

Instead of

invoke-command -computer $MachineName -filepath "C:\hello.ps1"

Use

invoke-command -filepath "C:\hello.ps1" -computer $MachineName

1
  • How does this solve anything? The order of named parameters doesn't change how they're evaluated or how the cmdlet executes. Dec 30, 2021 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.