Simple remote execution of PowerShell scripts using the HTTP API _powershell

Source: Internet
Author: User
Tags json

There may be situations where you do not want to fully experience remote administration via PSRP (PowerShell Remoting Protocol), or if you need to execute some PowerShell scripts from a non-Windows system. You might write code directly based on Ws-man, as I wrote in this article a long time ago. But that's complicated and requires some knowledge of ws-man and soap.

I wrote an HTTP listener using the. Net HttpListener class in the PowerShell script. You can start an instance of a listener very simply (you need to elevate your privileges to listen to the network), now you can execute any PowerShell command, and it can return execution results in multiple formats.
Once you have installed this component (just copy it to the Documents\windowspowershell\modules folder) and import it, you can start an instance by Start-httplistener.

By default I let it listen on port 8888, which integrates Windows authentication, but can still be configured in parameters. Here I opened another PowerShell window and executed the "Get-process PowerShell"

Note that I use –usedefaultcredentials to verify that my current security context is used, otherwise you will be told that Anonymous access is not supported.

By default, I return output in JSON format. Because the resulting JSON object is very large, it will be truncated. The body of the HTTP response is in the content attribute, but it is a byte array. I use UTF8. NET class to decode and display part of the JSON data:

I can use Convertfrom-json to make it more presentable in PowerShell:

Of course, if you are using a different client script such as Python,perl or Ruby, you can invoke the script library to use the JSON version in these languages.

If you're just playing, you can use text formatting to make things easier and easier to read, just like executing the same command line directly in PowerShell:

Other formats to choose from, if you call the HTTP endpoint without any arguments, you will get basic usage feedback:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.