I have explored a lot of resources but there is no information in anywhere about how to utilize the PSObject values that has multiple AD attributes into the webGrid or HTML Table. I have a simple string function that I am using to fetch the details from
PowerShell Script. The PS Object has the details returned by the script but I can't figure out a way to bind the webGrid Columns with those attributes. Basically, the object has column name and row values.
The Script that I am Passing is :
$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
public List<string> PowerShellExecutorGrd(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.ToString().Trim('@').Trim('{').Trim('}') + "\n\r");
}
outString = Server.HtmlEncode(builder.ToString());
}
List<string> strLst = outString.Split(new char[] { '\n' }).ToList();
shell.Dispose();
return strLst;
}
For now you return a string for each user. Though parsing strings works, my approach would be to take each value to populate a real class and handle that this way with multiple properties. BTW given how PowerShell works I believe you might be able to get
an object back rather than just a string output. Is this the direction you want to ttake ?
Member
21 Points
67 Posts
Loading the PowerShell AD Group Details from PSObject into WebGrid.
Jun 10, 2019 06:48 PM|vyasnikul|LINK
I have explored a lot of resources but there is no information in anywhere about how to utilize the PSObject values that has multiple AD attributes into the webGrid or HTML Table. I have a simple string function that I am using to fetch the details from PowerShell Script. The PS Object has the details returned by the script but I can't figure out a way to bind the webGrid Columns with those attributes. Basically, the object has column name and row values.
The Script that I am Passing is :
And the values that strLst stores is :
I am using JsonResult method to migrate value from controller to my View:
On my View I am invoking the function on ajax using script block:
The output I am trying to have is as follows:
How do I load this data into a webGrid with the property as column name & values as the data? I used HTML Razor Syntax to design GUI.
All-Star
18815 Points
3831 Posts
Re: Loading the PowerShell AD Group Details from PSObject into WebGrid.
Jun 11, 2019 02:51 AM|Nan Yu|LINK
Hi vyasnikul ,
Since you are returning the List<string> to client side , you can try below code sample :
JS :
Best Regards,
Nan Yu
All-Star
48710 Points
18179 Posts
Re: Loading the PowerShell AD Group Details from PSObject into WebGrid.
Jun 11, 2019 07:19 AM|PatriceSc|LINK
Hi,
For now you return a string for each user. Though parsing strings works, my approach would be to take each value to populate a real class and handle that this way with multiple properties. BTW given how PowerShell works I believe you might be able to get an object back rather than just a string output. Is this the direction you want to ttake ?
It would allow to swap using PowerShell with something else later (or now ?). If using a recent version, I believe it would be easier to query AD using https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.groupprincipal?view=netframework-4.8 (using FindByIdentity to get the group and GetMembers to get group members)
Member
21 Points
67 Posts
Re: Loading the PowerShell AD Group Details from PSObject into WebGrid.
Jun 11, 2019 03:42 PM|vyasnikul|LINK
Patrice,
This is new to me but I'll take a look at it for sure. Meanwhile, if you have any sources where they provide examples please let me know.
Thank you so much for this idea.
Member
21 Points
67 Posts
Re: Loading the PowerShell AD Group Details from PSObject into WebGrid.
Jun 11, 2019 04:50 PM|vyasnikul|LINK
Perfectly, working Nan Thanks. PowerShell is amazing to work on but a bit challenging when we need to deal with creating a GUI to perform operations.
All-Star
48710 Points
18179 Posts
Re: Loading the PowerShell AD Group Details from PSObject into WebGrid.
Jun 12, 2019 06:10 PM|PatriceSc|LINK
Here is a quick Console demo that shows how you could query an AD group with this namespace :