26

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.

Samselvaprabu
  • 13,922
  • 28
  • 113
  • 200
  • 2
    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 05 '17 at 22:14

3 Answers3

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.

JPBlanc
  • 63,198
  • 12
  • 118
  • 155
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.

JustJohn
  • 1,115
  • 2
  • 19
  • 38
  • This means you are running the powershell command on your VM from your local machine?? – ChanGan Sep 09 '17 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 '17 at 18:18
  • My server machine is on other domain. this solution will work?? – ChanGan Sep 13 '17 at 02: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 '17 at 15:15
0

I had the exact same probe, and solved it with a combination of [WMICLASS] 's create() and Start-Process.

Check my answer here.

Community
  • 1
  • 1
Ocelot
  • 1,595
  • 3
  • 22
  • 49