Quantcast
Channel: Azure Active Directory forum
Viewing all articles
Browse latest Browse all 16000

Error seeking the licenses granted to registered users using the MSOnline and Get-MsolUser

$
0
0

Hello guys.

I need help.

I have an application (C #) that seeks the licenses granted to registered users using the MSOnline and Get-MsolUser. The problem that this ONLY works for Windows application form, gives error when I try to get the licenses using a Web form.

The error is the following:

The term 'Connect-MsolService' is not recognized as a cmdlet name, function, script file, or operable program. Check the spelling of the name, or if a path has been included, see if the path is correct and try again.

The following method is what makes the query:

public Collection<PSObject> ExcutePowershellCommands(String user, SecureString password)
        {
            try
            {
                Collection<PSObject> userList = null;

                // Create Initial Session State for runspace.
                InitialSessionState initialSession = InitialSessionState.CreateDefault();

                initialSession.ImportPSModule(new[] { "MSOnline" });

                // Create credential object.
                PSCredential credential = new PSCredential(user, 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 users.
                Command getUserCommand = new Command("Get-MsolUser");

                using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
                {
                    // Open runspace.
                    psRunSpace.Open();

                    //Iterate through each command and executes it.
                    foreach (var com in new Command[] { connectCommand, getUserCommand })
                    {
                        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 && com == connectCommand)
                        {
                            MessageBox.Show(error[0].ToString(),"Problem in login");
                            return null;
                        }
                        if (error.Count > 0 && com == getUserCommand)
                        {
                            MessageBox.Show(error[0].ToString(),"Problem in getting users");
                            return null;
                        }
                        else
                        {
                            userList = results;
                        }
                    }
                    // Close the runspace.
                    psRunSpace.Close();
                }
                return userList;
            }
            catch (Exception)
            {
                throw;
            }
        }

How can I solve this problem? Thank you.

hugs,

Ricardo.


Viewing all articles
Browse latest Browse all 16000

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>