Executing the Azure Active Directory Module (MSOL Commands) Power Shell commands from the C#


Executing the Azure Active Directory Power Shell commands from the C#.

What is a use of the Azure PowerShell Module?

Azure Active Directory Module (ADPS) for Windows PowerShell cmdlets for Azure AD administrative tasks such as user management, domain management and for configuring single sign-on.

Executing the Azure PowerShell from the C# code:

Most of the Azure administrative work performed through the PowerShell jobs only and some scenarios we have to execute PowerShell commands in C#.
            I am going to explain how to execute Azure PowerShell from the C#.
To start writing the code the first step is we need to install Azure PowerShell Module as mentioned below.

Install the Azure AD Module

The Azure AD Module is supported on the following Windows operating systems with the default version of Microsoft .NET Framework and Windows PowerShell: Windows 8.1, Windows 8, Windows 7, Windows Server 2012 R2, Windows Server 2012, or Windows Server 2008 R2.
First install the Microsoft Online Services Sign-In Assistant for IT Professionals RTW from the Microsoft Download Center. Then install the Azure Active Directory Module for Windows PowerShell (64-bit version), and click Run to run the installer package.

Scenario: Get the Security Group Name from Azure Active Directory by key word.


Code for the Console Application.

                                     Download Code 
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            GetSecurityGroup();
        }

        public static void GetSecurityGroup()
        {
            try
            {
               
                // Create Initial Session State for runspace.
                InitialSessionState initialSession = InitialSessionState.CreateDefault();
                initialSession.ImportPSModule(new[] { "MSOnline" });
                // Assign username to variable.
                UserCredential.UserName = "UserName"; // Pass Your AD User Name

                string password = "Your Password"; //  Pass your Password
                // Create and assign secure password string.
                SecureString securePass = new SecureString();
                foreach (char secureChar in password)
                {
                    securePass.AppendChar(secureChar);
                }

                UserCredential.Password = securePass;
                // Create credential object.
                PSCredential credential = new PSCredential(UserCredential.UserName, UserCredential.Password);
                // Create command to connect office 365.
                Command connectCommand = new Command("Connect-MsolService");
                connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));
                // Create command to get office 365 user.
                Command getGroupCommand = new Command("Get-MsolGroup");
                getGroupCommand.Parameters.Add((new CommandParameter("SearchString", "Pass your security group name")));
                              
                using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
                {
                    // Open runspace.
                    psRunSpace.Open();
                    //Iterate through each command and executes it.
                    foreach (var com in new Command[] { connectCommand, getGroupCommand})
                    {
                        var pipe = psRunSpace.CreatePipeline();
                        pipe.Commands.Add(com);
                        // Execute command and generate results and errors (if any).
                        Collection<PSObject> results = pipe.Invoke();
                        var error = pipe.Error.ReadToEnd();

                        if (error.Count > 0)
                        {
                            return;
                        }
                        if(results.Count>0)
                        {
                         Microsoft.Online.Administration.Group Group = (Microsoft.Online.Administration.Group)results[0].BaseObject;
                                  string  Groupid = Group.ObjectId.ToString();
                                  string GroupName = Group.DisplayName;
                              
                       }

                       
                    }
                    // Close the runspace.
                    psRunSpace.Close();
                }
                //Cursor.Current = Cursors.Default;
            }
            catch (Exception)
            {
             
            }
        }
    }
}



Popular Posts