2

I'm working on the some proof of concept code for a system that will manage a large number of Office365 accounts, however, I seem to be struggling at the first hurdle with a rather daft problem.

I'm using the RunspaceFactory to fire my Powershell commands at Office 365 and whilst the code appears to be running without any errors I never get a list of users back.

Firstly here's my code....

Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();

Pipeline pipeline = runSpace.CreatePipeline();

Command msolConnect = new Command("Connect-MsolService");

System.Security.SecureString securePassword = new System.Security.SecureString();
foreach (char pwdLetter in password)
{
    securePassword.AppendChar(pwdLetter);
}
PSCredential credential = new PSCredential(username, securePassword);
msolConnect.Parameters.Add("Credential", credential);
pipeline.Commands.Add(msolConnect);

Command msolGetUser = new Command("Get-MsolUser");
msolGetUser.Parameters.Add("SearchString", "hayley");
pipeline.Commands.Add(msolGetUser);


Collection<PSObject> connectOutput = pipeline.Invoke();
foreach (PSObject psObject in connectOutput)
{
    Console.WriteLine(psObject.Members["DisplayName"].Value.ToString());
}

The pipline.HadErrors is false and the connectOutput is always empty. It appears that code is succesfully running but without returning any results.

I have tried;

  • the same command in Windows Powershell and I get back a list of expected results.
  • mis-spelling SearchString (just to check that the command was running and the parameter was being passed) and an error is generated as I would expect I have also
  • using ImportPSModule(new[] { "MsOnline" }) to ensure the MSOnline module is available
  • other commands (e.g. Get-MsolGroup) and get back an empty list

I know find myself scratching my head on a Friday afternoon hoping that someone else may be able to help me...

Thanks in advance,

Darren

Darren
  • 23
  • 1
  • 3
  • You will already have had these thoughts but: 1) any chance the pipeline is being closed before the results have come back? 2) Had a chance to check the security context within which the pipeline is running (esp: how this affects the PS profile)? – Aidanapword May 02 '14 at 16:25
  • Thanks @Aidanapword, As far as I can determine the pipeline is still open when the results are returned. I'm pretty sure it's not a security context issue but will do some testing this afternoon. – Darren May 06 '14 at 10:29
  • Security context appears okay as well... – Darren May 09 '14 at 09:45
  • @Darren How was this resolved? – wruckie Nov 05 '17 at 23:49

2 Answers2

2

First you need to install SharePoint Online Management Shell from the following link: https://www.microsoft.com/en-us/download/details.aspx?id=35588. But you may also need to install Microsoft Online Services Sign-In Assistant version 7.0 or greater version from the following link: https://www.microsoft.com/en-my/download/details.aspx?id=39267 and then need to install Windows Azure Active Directory Module.

After that you can follow the following code:

InitialSessionState iss = InitialSessionState.CreateDefault();
            iss.ImportPSModule(new[] { "MSOnline" });
            using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
            {
                Pipeline pipeline = myRunSpace.CreatePipeline();
                myRunSpace.Open();


                // Execute the Get-CsTrustedApplication cmdlet.
                using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
                {
                    powershell.Runspace = myRunSpace;
                    Command connect = new Command("Connect-MsolService");
                    System.Security.SecureString secureString = new System.Security.SecureString();
                    string myPassword = "Password";
                    foreach (char c in myPassword)
                        secureString.AppendChar(c);

                    connect.Parameters.Add("Credential", new PSCredential("admin@domain.microsoftonline.com", secureString));
                    powershell.Commands.AddCommand(connect);


                    Collection<PSObject> results = null;
                    Collection<ErrorRecord> errors = null;
                    results = powershell.Invoke();
                    errors = powershell.Streams.Error.ReadAll();
                    powershell.Commands.Clear();
                    Command getuser = new Command("Get-MsolUser");
                    getuser.Parameters.Add("MaxResults", 4);
                    powershell.Commands.AddCommand(getuser);
                    results = null;
                    errors = null;
                    results = powershell.Invoke();

                    foreach (PSObject psObject in results)
                    {
                        Console.WriteLine(psObject.Members["DisplayName"].Value.ToString());
                    }
                }
            }
Bayzid
  • 21
  • 3
0

Try appending the Get-MsolUser with -All