Execute remote powershell script from local system

1.7k Views Asked by At

I am new to this powershell..Execuse me if anything wrong here.

Now coming straight to the question I have a powershell script stored in one of the drive say "D:\myfile.ps1" which is in remote system.

Now I need to execute this script from my local system and this is the way I have done

ExecuteCommand(Directory.GetCurrentDirectory & "\Psexec \\myservername -i -u username -p password powershell.exe Invoke-Command -ComputerName \\myservername -FilePath D:\myscript.ps1")

The result is showing me this way without any output giving me an error

"powershell exited on myservername with error code 1'

enter image description here

And If run this script to run .exe on the same server it's working fine

ExecuteCommand(Directory.GetCurrentDirectory & "\psexec \\myservername -u username -p password D:\myfile.exe")

So what's the correct way to execute my script to run on remote desktop ?

Update after suggestions:

enter image description here

1

There are 1 best solutions below

1
JPBlanc On

Can you just try :

ExecuteCommand(Directory.GetCurrentDirectory & "\Psexec \\myservername -i -u username -p password powershell.exe -command  "&{Invoke-Command -ComputerName \\myservername -FilePath D:\myscript.ps1}"