0

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!

Jeff
  • 760
  • 1
  • 12
  • 26
  • I suggest try grabbing this repository and observe how things work regarding Windows Azure PowerShell: [Git Repo Link](https://github.com/Azure/azure-sdk-tools-samples) – Nelson Melecio Dec 15 '16 at 06:01
  • @Nelson: its run on powershell. thing is i cant find a way to execute my ps1 files locally onto a remote machine using winrm. boss told me to have it possibly use winrm, not ps-session – Jeff Dec 15 '16 at 06:05
  • [read this first](https://www.opsgility.com/blog/windows-azure-powershell-reference-guide/introduction-remote-powershell-with-windows-azure/) – Nelson Melecio Dec 15 '16 at 06:05
  • ah yeah, i saw that one too. not what were looking for. looking for something like winrm invoke script -r:{remote-machine} – Jeff Dec 15 '16 at 06:09
  • Have you read this stuff? [MSDN Reference for invoking a script](https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.core/Invoke-Command) and [How To Run PowerShell Commands Against a Remote VM](https://redmondmag.com/articles/2015/07/16/run-powershell-commands-against-a-remote-vm.aspx) – Nelson Melecio Dec 15 '16 at 06:20
  • yeps. neither invoke command or pssession will work as winrm is the client's specification to use – Jeff Dec 15 '16 at 06:22
  • That's weird indeed. Have you set an ssl certificate successfully while trying those stuff? and have you successfully set your listener when invoking those commands? – Nelson Melecio Dec 15 '16 at 06:25
  • no, were just using basic auth with winrm, the above command already works, just the powershell script (*.ps1) thing. because we already have these ps1 files, that we need to execute on a remote machine using winrm without copying it onto the remote machine – Jeff Dec 15 '16 at 06:33

1 Answers1

1

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
BartekB
  • 8,492
  • 32
  • 33
  • thanks man, but how do i execute a script that is on my local computer. seems like winrs will be looking the file on the remote machine – Jeff Dec 15 '16 at 07:32
  • the client specs was to execute the locally stored powershell scripts (*.ps1), not to copy on the target machine. – Jeff Dec 15 '16 at 07:43
  • OK, so to the list of "you loose" add "run script that exist only on a client machine". `winrs` is not able to copy the script over for you, you would have to either copy it over, or convert whole script to base64 and use it with -encodedCommand (if script is short - else it will fail due to char limit in cmd) – BartekB Dec 15 '16 at 11:29
  • thanks Bartek for your answers. i now better understand how this works. i guess its better off to copy the scripts to the target machines and batch execute them. – Jeff Dec 15 '16 at 16:04