简体   繁体   中英

execute/invoke a local ps1 file using winrm

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!

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

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