0

I have created a trail business account in Office365 and added some users under my domain.

Now I want to list the users using C#. I know that using graph api we can get token and be using that token we can access office365 data.

But what I want is, in a console application, the user will type their username and pwd ( of my domain) and the output should be the user details.

The main thing is I should not complicate like registering the application and generating token. I need to retrieve the user details simply.

I don't have Azure AD subscription and I need to do without a subscription.

So How can I do this?

saravanan ks
  • 133
  • 1
  • 12
  • You can use [PowerShell cmdlet Get-MsolUser](https://learn.microsoft.com/en-us/powershell/module/msonline/get-msoluser?view=azureadps-1.0) to do so. – Robert Dyjas Jul 03 '18 at 07:15
  • this will invoke a prompt window to get access. isn't it? – saravanan ks Jul 03 '18 at 07:20
  • It depends how you implement it. – Robert Dyjas Jul 03 '18 at 07:37
  • Actually, It asks login prompt, but I should not want login prompt, programmatically I have to give all credentials like uname, pwd. How to do that? – saravanan ks Jul 03 '18 at 07:50
  • There are multiple options. For example you can have service account which will pull the data. We use something like this but in quite different scenario: user creates a new item on SharePoint list and this list is checked every 15 mins by the script which pulls the data from O365 and send it to user via email. – Robert Dyjas Jul 03 '18 at 08:02
  • What I want is, In a visual studio console, the user will give username and password and that should be passed as a PowerShell credential. How to do that? – saravanan ks Jul 03 '18 at 08:48
  • It's a bit broad question, but check [this article](https://blog.kloud.com.au/2016/04/21/using-saved-credentials-securely-in-powershell-scripts/) to get to know more about saving credentials. Then you can just use `Connect-MsolService -Credential $creds`. Please note that it might work in a different way depending on your environment. – Robert Dyjas Jul 03 '18 at 09:04
  • And one more... , `Connect-MsolService ` ' is not recognized as the name of a cmdlet, function, script file, or operable program. I imported that service in my windows Powershell application, How to import this in visual studio – saravanan ks Jul 03 '18 at 09:10
  • You need to import module file using `Import-Module`. Please try to show some effort. – Robert Dyjas Jul 03 '18 at 09:15
  • Tried a lot. But MSOnline file does not exist in my system so that I couldn't specify the path of the module in C# – saravanan ks Jul 03 '18 at 10:00
  • If that works in PowerShell, it must exist somewhere. Try `get-module msonline -ListAvailable|fl path` to find the path of it. – Robert Dyjas Jul 03 '18 at 10:06
  • Yup found , now I will work on visual studio, thanks a lot! – saravanan ks Jul 03 '18 at 10:11
  • You're welcome. I'll convert this discussion to the answer so you can mark it as accepted if you wish! – Robert Dyjas Jul 03 '18 at 10:12

1 Answers1

1

There are multiple options to do so and a lot depends on your environment. One of the possibilities is to use Get-MsolUser PowerShell cmdlet.

By default, this will prompt you for credentials (if you don't specify any). If you want to avoid this, you might check this article to check how to save credentials to the variable and then pass it to the cmdlet like this:

Connect-MsolService -Credential $creds

Please note that it might work in a different way depending on your environment.

As per the discussion in comments, if you receive the error

Connect-MsolService is not recognized as the name of a cmdlet, function, script file, or operable program

that means you have to import the module manually (although PowerShell 3 and higher in most cases will import the module automatically). To find the path of the module use

Get-Module msonline -ListAvailable | fl Path

And then import the file using Import-Module.

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34
  • `PowerShell ps = PowerShell.Create();` `ps.AddCommand("Import-Module").AddParameter("Name", "C:\\Program Files\\WindowsPowerShell\\Modules\\Azure\\5.1.2\\Azure.psd1");` `ps.AddCommand("Get-AzureADUser");` `var modules = ps.Invoke();` tried for AzureAD but not working , command not found error – saravanan ks Jul 03 '18 at 11:40
  • That's another issue far behind the scope of your initial question. [so] is Q&A site, not a forum so I'd suggest to post another question with more description + what you already tried. – Robert Dyjas Jul 03 '18 at 11:43
  • I referred so many pages, but struggling in just importing and executing powershell cmdlets – saravanan ks Jul 03 '18 at 11:49
  • [What about this solution](https://stackoverflow.com/a/17071164/9902555)? – Robert Dyjas Jul 03 '18 at 11:52
  • same `Connect-Msolservice` is not recognized – saravanan ks Jul 03 '18 at 11:56
  • working fine in powershell but in visual studio only this command is not working – saravanan ks Jul 03 '18 at 11:58
  • Post separate question, please, if you want to increase chances that someone will look on it. – Robert Dyjas Jul 03 '18 at 11:59
  • successfully module has been imported – saravanan ks Jul 04 '18 at 05:03
  • Actually I have a problem now, I have posted that as a question, since no views on it, I am sharing this link here so that you can check my question [link](https://stackoverflow.com/questions/51172767/getting-particular-user-details-from-azuread-using-powershell-cmdlets-in-c-sharp) – saravanan ks Jul 04 '18 at 13:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174351/discussion-between-saravanan-ks-and-robdy). – saravanan ks Jul 04 '18 at 13:20
  • Sorry but I won't be able to help you with that. Just a remark, the fact that your question hasn't been answered after 2 hours doesn't mean it won't be later. And please remember that [so] is not free coding and troubleshooting service - the fact that someone responded to your question doesn't mean that he/she offers extended troubleshooting with some other issues you have. Please be patient and spend time trying to troubleshoot the issue on your own instead of trying to find someone who could do this for you. Thanks :) – Robert Dyjas Jul 04 '18 at 13:34