Search code examples
c#powershellstring-formatting

How do I access the values in the function stored in the PSObject?


I have created a function that returns the list of PowerShell attributes when I pass the script path into it. I am not able to access the values in the object where the function is storing them. Basically the motive of this function is to load the ad attributes inside the webgrid.

Tried using breakpoint to track down whether the values are loaded or not. Basically the values are loaded with the attribute names and string stored in them from AD Groups that I am fetching through the scripts. I am getting the count of 6 users but the return values look like "Object object" when I load them in the textarea. I have added the screenshot of the values when I put the breakpoint.

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> result = outString.Split(new char[] { '\n' }).ToList();
        List<SelectListItem> listItems = result.Select(s => new SelectListItem { Value = s, Text=s }).ToList();
        shell.Dispose();
        return listItems;
    }

My Script:

$GroupName ='Test - Group'
$SamAccountName = Get-ADGroup -Filter { CN -eq $GroupName } -Properties  SamAccountName | Select -ExpandProperty SamAccountName

#Getting Members of the Group - Not Storing Employee Number
Get-ADGroupMember -Identity $SamAccountName | where {$_.objectclass -eq 'user'} | 
Get-ADUser -Properties displayname,  samAccountName, ObjectGUID | 
Select displayname, samAccountName, ObjectGUID

The snapshot of the values with attributes

Sample Values Stored in my psObject are

@{displayname=User1; samAccountName=usr1; ObjectGUID=8a3fab53-4c8b-483d-89f0-e26de236a627}
@{displayname=User2; samAccountName=usr2; ObjectGUID=0a3fab53-4c8b-483d-89f0-e26de236a627}
@{displayname=User3; samAccountName=usr3; ObjectGUID=9a3fab53-4c8b-483d-89f0-e26de236a627}

I just want these values to fill inside the webGrid but can't figure out the way what to return from the function.


Solution

  • So, all I had to do is define column heading statically in html & map it with the jQuery function code.

    <div id="tbl">
    <table>
        <thead>
            <tr id="thead">
                <th>displayName</th>
                <th>samAccountName</th>
                <th>ObjectGUID</th>
            </tr>
        </thead>
        <tbody id="tbody">
        </tbody>
    </table>
    

    jQuery

     $(function(){
    
        $.ajax({
            type: "POST",
            url: "/Group/FillMembers",           
            success: function (response) {
                $.each(response.message, function (key, value) {
                    var arr = value.split(";")
                    var tbody = $("#tbody");
                    var tr = $("<tr></tr>")
                    $.each(arr, function (i, obj) {
                        var temp = arr[i].trim().split("=")[1];
                        var td = $("<td></td>")
                        td.append(temp);
                        tr.append(td);
                    })
                    tbody.append(tr);
    
                });            
            }
        });      
    })