Implement a simple interface with Django. Use the Bootstrap template directly to create a front-end page, Django framework, and then call the PowerShell API in the background to implement the query.

This is part of the chat system, logistics management system, chat system, e-commerce seconds kill system video.Copy the code

Video, source code in the public 呺** →→→ code without bugs

Here is a simple demo. Enter the AD group and display the group members

Django has nothing to say, basic MTV framework flow, the main fun is this PowerShell API module. HttpListener modules are available online. I made some modifications to remove an authentication function, so you can manually add one if you want. I’m trying to save trouble here is directly used to verify the version.

It provides a restful interface to execute Powershell commands. Http GET requests to the corresponding interface directly, and then returns the result in JSON format

Import-Module C:\users\yuan.li\Documents\GitHub\Powershell\HTTPListener.psm1
start-httplistener -verb -Auth None
Copy the code

Test it out:

The browser

Python

It is worth mentioning that there are two places to set up the specific Powershell commands. One is to type command= directly after the URI. Simple commands are fine, but if the command is complex and long, this is not a good place to do it.

Alternatively, you can write a function in the HTTPListener module file so that it can be loaded into memory as well. Command = is simply followed by the function name and argument.

Such as:

function search-adgroupmemeber($group){
    Get-ADGroupMember $group | select name, SamAccountName,Distinguishedname
}
Copy the code

So LET me just call it

http://localhost:8888/? command=search-adgroupmemeber 'domain admins'
Copy the code

According to the results

Okay, it’s almost working, so let’s make an interface on Django

Url. Py routingurl(r'^powershell', views.powershell), views.py view functionsimport requests
def powershell(req):
    if req.method=="GET":
        return render(req,'powershell.html')
    elif req.method=="POST":
        name=req.POST.get("caption")
        print(name)
        res=requests.get("http://localhost:8888/? command=get-adgroupmember '%s' | select name, distinguishedname"%name)
        print(res)
        result=res.json(a)print(result)
        return render(req,'powershell.html', {'result':result})
Copy the code

The powershell.html template, which I’m not using AJAX, is a direct form submission

{%  extends 'base.html' %}
{% block css %}
    <style>
        .go{
            width:20px;
             border: solid 1px;
            color: #66512c;
            display: inline-block;
            padding: 5px;
        }
        .pagination .page{
            border: solid 1px;
            color: #66512c;
            display: inline-block;
            padding: 5px;
            background-color: #d6dade;
            margin: 5px;
        }
        .pagination .page.active{
            background-color: black;
            color: white;
        }
        .hide{
            display: none;
        }
        .shade{
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
            background: black;
            opacity: 0.6;
            z-index: 100;
        }
        .add-modal,.edit-modal{
            position: fixed;
            height: 300px;
            width: 400px;
            top:100px;
            left: 50%;
            z-index: 101;
            border: 1px solid red;
            background: white;
            margin-left: - 200.px;
        }
        .group{
            margin-left: 20px;
            margin-bottom: 15px;
        }
    </style>
{% endblock %}
{% block content %}
Copy the code

Powershell test page

Querying a User Group





Members of the DN operation
`items`.`name` `items`.`distinguishedname` Modify the |delete
{% endblock %}
{% block title%}PowerShell{% endblock %}
{% block js%}
<script>
</script>
{% endblock %}
Copy the code

The result is a query.