locked
View The contents of json through a controller function. RRS feed

  • Question

  • User-1549556126 posted

    I am passing a json object from my view through ajax call. Using the Action result and creating a class object I am passing the values as arguments to a function that executes powershell script. However to cross check I am not able to figure out what is the format that is being passed through the script's parameter is there a way to trace it?

    This is my Ajax call on button click

    $.ajax({
                    url: '@Url.Action("UpdateMemberList", "Group")',
                    type: "POST",
                    data: '{objMemberUpdate:'+ JSON.stringify(MemberUpdate)+'}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.message);
                    }
                });
    

    This is my controller method with class defined below

    public ActionResult UpdateMemberList(GroupReviewUpdate objMemberUpdate)
            {
                var shell = PowerShell.Create();
                var resultValue = "";
                shell.Commands.AddCommand(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Set-ADGroupMembership.ps1");
                shell.Commands.AddArgument(objMemberUpdate);
                var result = shell.Invoke();
                if (result.Any())
                {
                    resultValue = result.ToString();
                }
                else {
                    resultValue = "Eh!";
                }
                return Json(new {
                    ResultValue = resultValue
                });
            }
        }
    
        public class GroupReviewUpdate {
            public string Directory { get; set; }
            public string Group { get; set; }
            public List<string> Remove { get; set; }
            public List<string> RemoveAfter { get; set; }
            public string ResultFormat { get; set; }
            public string OnBehalfOf { get; set; }
        }

    This is the script that I am executing.(Set-ADGroupMembership.ps1)

    param($objMemberUpdate)
    $Rsult = Invoke-WebRequest -Uri "https://myDomain-api-test-com:4444/RecordGroupReview.ps1x" -Method Post -Body (ConvertTo-Json $objMemberUpdate) -UseDefaultCredentials:$true
    Write-Output($Rsult)

    I want to see the data of $objMemberUpdate in visual studio is it possible to do it?

    Wednesday, July 3, 2019 12:47 AM

All replies

  • User753101303 posted

    Use F12 Network in your browser which allows to inspect http queries: https://developers.google.com/web/tools/chrome-devtools/network/#details

    I believe you need :

     data: MemberUpdate,

    ie you pass an object and on the server side ASP.NET creates the parameter and map properties based on their name (and so you post the object directly rather than an object having a objMemberUpdate property as you are doing for now).

    Make sure you then get a correctly populated objMemberUpdate in your API and then you'll be ready for the next step ;-) See https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019 if needed.

    Edit: I'm not sure that using PowerShell to send http requests from C# will be quicker than looking at how https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client works.

    Wednesday, July 3, 2019 8:10 AM
  • User1520731567 posted

    Hi vyasnikul,

    If you want to debug powershell scripts in VS,

    You could download plugin: Tool->Extensions and Updates-> 'PowerShell Tools for Visual Studio'.

    More details,you could refer to this video:

    https://www.youtube.com/watch?v=g4lJOoVJmjM

    You could also download Windows PowerShell ISE:

    The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In the ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphic user interface. The ISE provides multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and support for right-to-left languages. Menu items and keyboard shortcuts are mapped to many of the same tasks that you would do in the Windows PowerShell console. For example, when you debug a script in the ISE, you can right-click on a line of code in the edit pane to set a breakpoint.

    Best Regards.

    Yuki Tao

    Wednesday, July 3, 2019 8:40 AM