Updating the active directory group membership using the instance of PowerShell. RRS feed

  • Question

  • I am building an application that interacts with Active Directory using System.Management.Automation (Not using Directory Services because currently new to that library and learning it). To update the group membership of for a group in the active directory I am creating a JSON object on my view and invoking a function to pass the object & the URI from front end to back end via a function in my controller.

    The basic idea is to allow removal of AD group members in bulk by passing the JSON object as a parameter to the shell script which will be executed in an instance of PowerShell created in the function. I am using .ajax call to invoke the controller function and passing the JSON object that I generated as an argument along with the current URI. The shell.commands.AddParameter() function accepts argument in only string format. So, I typecasted it with ToString() and converting it to JSON in the PowerShell script. I am passing the URL from code behind as the URL is subject to change. I am not getting any errors However, I am also not able to see any update in membership in the AD. Json Object is getting generated from HTML Table.

    My shell script

    param($objMemberUpdate, $uri)
    $body = $objMemberUpdate | ConvertTo-JSON
    Invoke-WebRequest -Uri $uri -Method Post -Body $objMemberUpdate

    My Controller Function in ASP MVC to Invoke PowerShell Instance and executing Shell Script file from specified location.

    public string UpdateMemberList(JsonResult objMemberUpdate)
            var uri = HttpContext.Request.Url.AbsoluteUri;
            var shell = PowerShell.Create();
            shell.Commands.AddCommand(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Set-ADGroupMembership.ps1").AddParameter(objMemberUpdate.ToString(), uri);
            var results = shell.Invoke();
            return results.ToString();

    The Ajax Call that I am calling on a button click on my HTML page.

    //Make Array Object to pass in the API For Membership Update
        $("#btnUpdate").click(function () {
            var RemoveMembers = [];
            var RemoveAfter = [];
            var MemberUpdate = {};
            var GroupGUID = "";
            $("table [id*=ddlReqdAdjustment]").each(function () {
                if ($(this).val() != "Keep") {
                    GroupGUID = $(this).parent().parent().children().eq(4)[0].innerText;
                    var date = $(this).parent().parent().children().eq(8)[0].firstElementChild.value;
                    var ObjectGUID = $(this).parent().parent().children().eq(3)[0].innerText + "@@" + $('#ddlDirectory').val();
                    if ($(this).val() == "Remove") {
                        var format = ObjectGUID;
                    } else {
                        var format = date + "|" + ObjectGUID;
            MemberUpdate = {
                "Directory": $('#ddlDirectory').val(),
                "Group": GroupGUID,
                "Remove": RemoveMembers,
                "RemoveAfter": RemoveAfter,
                "ResultFormat": "json",
                "OnBehalfOf": "11112201"            
                type: "POST",
                url: "/Group/UpdateMemberList",
                data: { objMemberUpdate: MemberUpdate },
                success: function (response) {

    The selected member in the table is supposed to get removed from the Group whose GroupGUID (ObjectGUID attribute in AD) is mentioned from the AD. However, from the C# function I m getting parse error. 

      Message=System error.
      Source=<Cannot evaluate the exception source>
    <Cannot evaluate the exception stack trace>

    • Edited by Nikul Vyas Tuesday, July 2, 2019 7:16 PM Updated the error text
    • Moved by jrv Tuesday, July 2, 2019 7:24 PM Correct forum
    • Moved by CoolDadTx Wednesday, July 3, 2019 1:31 PM ASP.NET related
    Tuesday, July 2, 2019 6:39 PM

All replies

  • Hi Nickul,

    Thank you for posting here.

    Since this thread is related to asp.net mvx, I suggest that you could post in the following forum.


    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 3, 2019 2:54 AM