none
Fetch user information from an Exchange Online account RRS feed

  • Question

  • Hi All,

    I need to fetch basic properties (smtp address, user name, display name etc.) of all users which belong to an Exchange online account of an organization. Exchange web services do not have any web method which returns list of all users. ResolveName() method is not a good option since I need to fetch information of all users. Given that it is Exchange online account, Active directory cannot be an option for me. I would prefer to use C#.

    Could anyone please give me direction on how to do this? Thank you

    Tuesday, April 26, 2011 6:30 PM

All replies

  • Are you using BPOS or the Office365 Beta ? with BPOS there is Migration Powershell Snap-in you can install http://technet.microsoft.com/en-us/library/cc742577.aspx and then you can get the information using the get-msonlineuser http://technet.microsoft.com/en-us/library/ee662259.aspx. If it Office365 you can use remote powershell and the normal Exchange 2010 cmdlets like get-mailbox see http://www.mikepfeiffer.net/2010/11/office-365-connecting-to-exchange-online-with-remote-powershell/. You should always start by getting the underlying powershell cmds working then you can use Managed code to execute these cmdlets like http://blogs.technet.com/b/exchange/archive/2009/11/02/3408653.aspx

    Cheers

    Wednesday, April 27, 2011 10:37 AM
  • Hello Glean,

    Thank for the response.

    It is for Exchange 365. As per my understanding get-mailbox command returns very limited properties. I am interested in display name, given name, last name, common name, email address and Object GUID properties. Could you please suggest how can I get all these properties?

    Wednesday, April 27, 2011 1:32 PM
  • With most of the Exchange cmdlets you need to join them together eg you can join get-mailbox with get-user in c# with office365 this would look like

     

     System.Net.ServicePointManager.ServerCertificateValidationCallback +=
      delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                  System.Security.Cryptography.X509Certificates.X509Chain chain,
                  System.Net.Security.SslPolicyErrors sslPolicyErrors)
      {
        return true; // **** Always accept
      };
          System.Security.SecureString secureString = new System.Security.SecureString();
          string myPassword = "password";
          foreach (char c in myPassword)
            secureString.AppendChar(c);
          PSCredential credential = new PSCredential("glen@domain.com", secureString);
          WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
          connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
          connectionInfo.SkipCACheck = true;
          connectionInfo.SkipCNCheck = true;
          
          connectionInfo.MaximumConnectionRedirectionCount = 4;
          Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
          runspace.Open();
          // Make a Get-Mailbox requst using the Server Argument
          Command gmGetMailbox = new Command("get-mailbox");
          gmGetMailbox.Parameters.Add("ResultSize", "Unlimited");
          Pipeline plPileLine = runspace.CreatePipeline();
          plPileLine.Commands.Add(gmGetMailbox);
          Collection<PSObject> RsResultsresults = plPileLine.Invoke();
          Dictionary<string, PSObject> gmResults = new Dictionary<string, PSObject>();
          foreach (PSObject obj in RsResultsresults)
          {
            gmResults.Add(obj.Members["WindowsEmailAddress"].Value.ToString(), obj);    
          }
          Command gmGetUser = new Command("get-user");
          plPileLine.Stop();
          plPileLine.Dispose();
          plPileLine = runspace.CreatePipeline();
          gmGetUser.Parameters.Add("RecipientTypeDetails", "UserMailbox");
          gmGetUser.Parameters.Add("ResultSize", "Unlimited");
          plPileLine.Commands.Add(gmGetUser);
          RsResultsresults = plPileLine.Invoke();
          foreach (PSObject obj in RsResultsresults)
          {
            PSObject gmObj = gmResults[obj.Members["WindowsEmailAddress"].Value.ToString()];
            Console.WriteLine("DisplayName : " + obj.Members["DisplayName"].Value.ToString());
            Console.WriteLine("FirstName : " + obj.Members["FirstName"].Value.ToString());
            Console.WriteLine("LastName : " + obj.Members["LastName"].Value.ToString());
            Console.WriteLine("EmailAddress : " + obj.Members["WindowsEmailAddress"].Value.ToString());
            Console.WriteLine("ExchangeGuid : " + gmObj.Members["ExchangeGuid"].Value.ToString());
          }
          runspace.Close();
         
    

     

    Cheers
    Glen 

    Thursday, April 28, 2011 10:59 AM
  • Thanks a lot Glean. I will try this out.
    Friday, April 29, 2011 5:00 AM
  • Glen,

    Thanks for the code. it works Great and smooth...:)

     I have question with reference to URL at:

    http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh125002.aspx

     "get-user" is defined as "Get-MsolUser" why there is a difference?

    When i try to run with "Get-MsolUser" it says that:

    "The term 'Get-MsolUser' is not recognized as the name of a cmdlet, function, script file, or operable program. "

    where can i find the the powershell commands documentation that runs like the one provider by u in your code.

    I am looking for command for password reset as defined in above URL as "Set-MsolUserPassword", what its name of command that runs.

    Any documentation for these cmdlet would be great help.

    Looking to hear from you.

    -Shah


    CyberBug
    • Edited by Cy63r Monday, July 4, 2011 4:55 PM typo
    Monday, July 4, 2011 3:46 PM
  • Secondly, when i get-Help for "Get-MsolUser" , it returns details via powershell console.

    but when i run get-help for  "get-user", via powershell console, it return nothing...:)

    Any comments on that...

    -Shah


    CyberBug
    Monday, July 4, 2011 3:58 PM
  • Investigated more, looks like there are two set of power shell commands.

    1. Use Windows PowerShell in Exchange Online at

    http://help.outlook.com/en-us/140/dd575549.aspx

    2.  second Power shell for office 365 administration at:

    http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh125002.aspx

    comment on it please..

    First set of command runs fine with as identified in this thread by Glen. Thank Glen for this code

    Is there any sample code for running 2nd set of command via c#. basically i am confused what should be ShellURL parameter in contrcutor of  WSManConnectionInfo.

    Any comment on that.

    Thanks

    -Shah


    CyberBug
    Monday, July 4, 2011 5:36 PM
  • For Office365 admin you need to first install the module http://g.microsoftonline.com/0BD00en-US/504 that contains the cmdlets to run this is different from Exchange Online where your using Remote powershell.

    One you have done this you then run the commandlets in a local runspace eg you need to do something like

        InitialSessionState iss = InitialSessionState.CreateDefault();
          iss.ImportPSModule(new[] { "MSOnline" });
          using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
          {
            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("glen@blah.onmicrosoft.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();
            }
          }
        }
    

     

    Cheers
    Glen 

     

    Tuesday, July 5, 2011 2:50 AM
  • Reallly Thanks Glen . That was Great Help.....
    CyberBug
    Tuesday, July 5, 2011 6:01 PM
  • Hi, i trying to get the registrated domains in the office365 subscription with powershell commands in C#. I do it like Clen Scales for properties related to exchange but the domains are not related to exchange, they are related to Msol.

    The command to get the domains is 'Get-MsolDomain'. In the normal Powershell I first had to execute the 'Import-Module msonline' and 'Connect-MsolService' so i tryed to do the same in my C# Project. I doesn't work, when I start my WebSite I get the error message:"The syntax is not supported by this runspace. This might be because it is in no-language mode."

    So I think it's the worng ShellUri parameter in

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);

    I looked for one which is related to Msol but i didn't found something...

    Can someone help me pls...

    ps: Sorry for my bad english I hope you can unterstand what i mean.
    Thursday, December 8, 2011 4:15 PM
  • Looks like your getting Remote Powershell which is used to access conventional Exchange 2010 cmdlets on ExchangeOnline (eg things like Get-Mailbox etc) with the Office365 admin cmdlets which are from a locally installed module eg ('Get-MsolDomain').

    So to use 'Get-MsolDomain' first download and Install "Microsoft Online Services Module for Windows PowerShell " from http://g.microsoftonline.com/0BD00en-US/504 

    Once you have this installed you should be able to use something like

          InitialSessionState iss = InitialSessionState.CreateDefault();
          iss.ImportPSModule(new[] { "MSOnline" });
          using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
          {
            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("blah@onmicrosoft.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 getMsolDomain = new Command("Get-MsolDomain");
              powershell.Commands.AddCommand(getMsolDomain);
              results = null;
              errors = null;
              results = powershell.Invoke();
              foreach (PSObject obj in results)
              {
                  Console.WriteLine(obj.Properties["Name"].Value.ToString());
              }
            }
          }
    

     

    Cheers
    Glen

     

    Friday, December 9, 2011 3:52 AM
  • Thank you :) I saw you answered a question like mine some posts above, sorry for asking it again...

    I have a litte Problem now, my application says that

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

    isn't a Name of a Cmdlet, function, application... it just can't be found(CommandNotFoundException). I have installed the module of your Link...

     

    Friday, December 9, 2011 10:02 AM
  • What version of the cmldets did you install 32 bit or 64 bit ? if you installed the 32 bit version and you project is target x64 than that's the type of error you would get eg its essentially saying the module isn't loaded so check the compile version of you project. You can also test it but starting powershell and seeing if you can do it manually.

    Cheers
    Glen

    Monday, December 12, 2011 5:22 AM
  • I installed the 64bit version but I want to use it in a web project. I think I can't change if it's 32 or 64 bit or? If yes how can I change it?
    Thursday, December 15, 2011 8:01 AM
  • If you install the 64 Bit version of Cmdlets then you can only use it from a 64 bit Powershell session and project similar to if you use the 32 bit version. If you compiling your Web Service as 32 bit then the 64 bit cmdlets wont work use the 32 bit ones instead.

    Cheers

    Glen

    Friday, December 16, 2011 2:53 AM
  • I tryedto install the 32 bit version of the cmdlets but I can't because I have a 64 bit Windows on my system so I have to live with the 64 bit version. But another question how can I change the bit version (32 or 64) of my Web Service? I could'nt find it...
    Friday, December 16, 2011 10:20 AM
  • You need to start with IIS eg maybe http://support.microsoft.com/kb/894435 

    Cheers
    Glen

    Monday, December 19, 2011 2:47 AM
  • I didn't found a solution for my problem. I run my web project with ASP.NET 2.0. I have a 64bit Windows7, installed the 64bit version of the module and my application pools in my IIS are all configured to 64bit.

    So I no idea why it don't works. Which reasons can my problem have else?

    Monday, December 19, 2011 3:15 PM
  • Are you building your project with a Platform Target of x64.

    My suggestion would be you try this first in a small x64 console application to test your code this will ensure what your doing works then try it in a WebService.

    Cheers

    Glen 

    Tuesday, December 20, 2011 1:45 AM
  • How can I see if it's a 64 or 32bit website in a web project? In a normal winforms project I can see and change it in the project properties but in my web project I just have startoptions and ASP.NET configuration... there is nothing about the the target platform.

    I will try it in a winforms application, thanks. In a console application the System.Management.Automation namespace is'nt known.


    Edit: The using System.Management.Automation namespace can't be found in my winforms application, too oO is there any trick or something? I need it to call the InitialSessionState object...
    • Edited by ALEBO Tuesday, December 20, 2011 8:01 AM
    Tuesday, December 20, 2011 7:53 AM
  • see http://msdn.microsoft.com/en-us/library/windows/desktop/ee706560%28v=vs.85%29.aspx

    I would try getting it to work in a console/winform app first then try porting it to the Web Service you may be better to ask the question you have a ASP.NET in that specific forum.


    Cheers
    Glen

    Thursday, December 22, 2011 6:12 AM