execute/invoke a local ps1 file using winrm

2.7k views Asked by At

i had this issue for days now. Done and researched everything pertaining to executing a powershell script on your local machine using winrm.

My goal is to execute these ps1 scripts from my local to a target remote machine. winrm is already configured and working with its basic commands, but i cannot find anything to run a powershell script (*.ps1) using winrm.

something like

winrm {execute ps1 file} -r:http://<IPaddress>:5985 -auth:basic -u:'username' -p:'password' -encoding:utf-8

Any help and suggestions are much appreciated.

Thank you guys!

1

There are 1 answers

4
BartekB On BEST ANSWER

First of all - I believe you are trying to use winrs not winrm - latter is used to configure winrm usually, former to run remote commands.

With that assumption in mind: I'm pretty sure you will have to pass a script to powershell.exe and take care of escaping command elements. Examples that worked for me (not basic auth, but that irrelevant I think):

winrs -r:ComputerName powershell -file c:\temp\test.ps1
winrs -r:ComputerName powershell -file "c:\temp\test with space.ps1"

Why would anyone want to do it that way is something different I guess. Any good reason why you are requested to go: powershell -> native command -> cmd.exe on remote and -> call PowerShell remotely ?

Doing it like that you loose:

  • any reasonable output (you get text)
  • streams
  • keeping the state after script is run
  • performance (you spawn new powershell instance with each call

You gain:

  • complexity