none
C# controller function to set the ID as value & Name as Text from the PowerShell Data Source. RRS feed

  • General discussion

  • I am fetching the list of AD Groups in the DropDownList, however the I am trying to set the ObjectGUID property as value & Name property as text for the dropdownlist selections. I have created the function using System.Management.Automation library to interact directly with PowerShell commands. The only place where I am not able to figure out is setting up the text & value property. Here's my C# Code.

    public List<SelectListItem> PowerShellExecutorLst(string scriptPath, string arg)
            {
                string outString = "";
                var shell = PowerShell.Create();
                shell.Commands.AddCommand(scriptPath).AddArgument(arg);
                var results = shell.Invoke();
                if (results.Count > 0)
                {
                    var builder = new StringBuilder();
                    foreach (var psObj in results)
                    {
                        builder.Append(psObj.BaseObject.ToString() + "\r\n");
                    }
                    outString = Server.HtmlEncode(builder.ToString());
                }
                List<string> strLst = outString.Split(new char[] { '\n' }).ToList();
                List<SelectListItem> listItems = strLst.Select(s => new SelectListItem { Value = s, Text=s }).ToList();
                shell.Dispose();
                return listItems;
            }

    Because my script is giving output of 2 properties Name & ObjectGUID, the outString variable is not able to determine proper value. Just for reference this is my script:

    Get-ADGroup -Filter {CN -eq 'TestGroup'} -Properties Name, ObjectGUID |
    Select Name, ObjectGUID 




    • Edited by Nikul Vyas Wednesday, June 26, 2019 7:04 PM Fixed the powershell script
    • Changed type Nikul Vyas Tuesday, September 10, 2019 9:02 PM
    Wednesday, June 26, 2019 4:58 PM

All replies

  • You either have to convert the output to a string or use a type that consumes objects.

    Your issue is with C# and not PowerShell.

    To get a string result do this:

    Get-ADGroup TestGroup |
        Select Name, ObjectGUID |
        Out-String
    
    You do not need to specify properties for these properties as they are the default.  You cannot specify a name as a filter.


    \_(ツ)_/

    Wednesday, June 26, 2019 5:41 PM
  • Got your point & did some editing in the script.

    The problem is in a drop down list the whole output is shown as a string which is not needed. The C# function's last third line has a string variable that is used for assigning value and text to dropdown list item. I want to store ObjectGUID in value & Name in text, so that when the user sees it only Name of the group would be visible and the value which is passed will be the ObjectGUID.


    • Edited by Nikul Vyas Wednesday, June 26, 2019 7:48 PM
    Wednesday, June 26, 2019 7:48 PM
  • I see no dropdown list anywhere.  A Forms DDL can take objects and display one property while returning a second value.  To do this you need to add the objects to the list items and assign "DisplayMember" and "ValueMember" of the DDL to the names of the objects properties.

    Normally we wouldn't so this.  We would just assign the display member and access the object in the "SelectedItems" array.  Now we get the whole object and can select the properties we need.

    To work effectively with forms we want to use objects and not strings.

    You should also stop suing AD CmdLets in C# since the Net classes are already available and do not need PowerShell to work.

    see System.DirectoryServices namespace.


    \_(ツ)_/


    • Edited by jrv Wednesday, June 26, 2019 8:12 PM
    Wednesday, June 26, 2019 8:10 PM