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)
            {
             
            }
        }
    }
}



8 comments:

  1. ERPTREE is a leading oracle fuison HCM Training Institute. we offer this course through online we have great
    experience in succeeding students through online courses. we can calculate our performance through their honest
    comments in our sites in supporting our services. we have referral program so candidates can earn money through
    referral. you can share your live experience with other can generate you some money.


    Oracle fusion HCM Online Training

    Oracle Fusion HCM Training

    ReplyDelete
  2. Thanks for your valuable info and time, nice and very useful article, thanks for sharing your knowledge.
    Oracle Training in chennai

    ReplyDelete
  3. Hello,

    Do we need to add any reference to use the "Get-MsolPartnerContract" command in our c# code are any 'MSol" command

    Can you please help me here i am stuck

    ReplyDelete
  4. product and to understand cloud storage better, one needs to undergo a Microsoft Azure fundamentals training course. Azure fundamental course

    ReplyDelete
  5. online professional listings can be an extraordinary expansion to your present private company advertising procedure. local personal classifieds

    ReplyDelete
  6. Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help. quads for sale

    ReplyDelete

Popular Posts