简体   繁体   中英

How to execute a powershell script available in remote machine?

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.

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.

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.

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.

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

Check my answer here .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM