I need to invoke some PowerShell cmdlets only one time but I want to all the info from all the cmdlets using C#
.
Eg:
Get-AzureADUser
Get-AzureADUserMembership
Get-AzureADUserLicenseDetail
This all cmdlets have to be invoked only once. But I should get all the info. How to do that using PowerShell class in C# so that time taken will be reduced?
Tried one by invoking multiple times:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline UserDetailsPipe = runspace.CreatePipeline();
UserDetailsPipe.Commands.AddScript("Get-AzureADUser");
foreach (PSObject info in UserDetailsPipe.Invoke()) /////////*******
{
ArrayList Groups = new ArrayList(); // to hold memberOf
ArrayList Licenses = new ArrayList(); // to hold of licenses
string UserPrincipalName = info.Members["UserPrincipalName"].Value.ToString();
string DisplayName = info.Members["DisplayName"].Value.ToString();
//Getting MemberOf
Pipeline memberPipe = runspace.CreatePipeline();
memberPipe.Commands.AddScript("Get-AzureADUser -ObjectId '" + UserPrincipalName + "'| Get-AzureADUserMembership");
//Getting Licenses
Pipeline licensePipe = runspace.CreatePipeline();
licensePipe.Commands.AddScript("$license = Get-AzureADUserLicenseDetail -ObjectId '" + UserPrincipalName + "' | select ServicePlans ");
licensePipe.Commands.AddScript("$license.ServicePlans");
foreach (var licensenames in licensePipe.Invoke())////////*****
{
Licenses.Add(licensenames.Members["ServicePlanName"].Value.ToString());
}
foreach (var memberOf in memberPipe.Invoke())////////*******
{
Groups.Add(memberOf.Members["DisplayName"].Value.ToString());
}
}
Made all scripts together and invoked once as @ivcubr said and works somehow better than the previous method
public static void Display()
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
PowerShell UserDetailsPipe = PowerShell.Create();
UserDetailsPipe.Commands.AddScript("Get-AzureADUser");
Console.WriteLine("\n{0,-20}{1,-45}{2,-30}{3,-30}", "DisplayName", "UserPrincipalName", "MemberOf", "Licenses");
Console.WriteLine("{0,-20}{1,-45}{2,-30}{3,-30}", "-----------", "-----------------", "--------", "--------");
var Users = UserDetailsPipe.Invoke();
foreach (var info in Users)
{
ArrayList Groups = new ArrayList(); // to hold memberOf
ArrayList Licenses = new ArrayList(); // to hold of licenses
string UserPrincipalName = info.Members["UserPrincipalName"].Value.ToString();
string DisplayName = info.Members["DisplayName"].Value.ToString();
//Getting MemberOf
PowerShell newPipe = PowerShell.Create();
newPipe.Runspace = runspace;
newPipe.AddScript("Get-AzureADUserMembership -ObjectId '" + UserPrincipalName + "' ; $license = Get-AzureADUserLicenseDetail -ObjectId '" + UserPrincipalName + "' | select ServicePlans ;$license.ServicePlans");
var AllInfo= newPipe.Invoke();
try
{
foreach (var licensenames in AllInfo)
{
if (licensenames.Members["DisplayName"]!=null)
Groups.Add(licensenames.Members["DisplayName"].Value.ToString());
if (licensenames.Members["ServicePlanName"] != null)
Licenses.Add(licensenames.Members["ServicePlanName"].Value.ToString());
}
}
catch (Exception)
{
}
//Displaying UsersInfo
for (int groupIndex = 0, licenseIndex = 0; groupIndex < Groups.Count || licenseIndex < Licenses.Count; groupIndex++, licenseIndex++)
{
if (groupIndex == 0 && licenseIndex == 0)
{
Console.Write("{0,-20}{1,-45}", DisplayName, UserPrincipalName);
}
else
{
Console.Write("{0,-20}{1,-45}", " ", " ");
}
if (groupIndex < Groups.Count)
{
Console.Write("{0,-30}", Groups[groupIndex]);
}
else
{
Console.Write("{0,-30}", " ");
}
if (licenseIndex < Licenses.Count)
{
Console.Write("{0,-30}", Licenses[licenseIndex]);
}
else
{
Console.Write("{0,-30}", " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.