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

  • Question

  • 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 {Name -eq 'TestGroup'} -Properties Name, ObjectGUID |
    Select Name, ObjectGUID 




    Wednesday, June 26, 2019 7:08 PM

All replies