Context
I have an ASP.NET Web API application invoked by an ASP.NET MVC application.
Inside the Web API, I have a method that will need to create an Office365 mailbox amongst other stuff (create folders, create a contact, create a signature etc.…)
The creation of that mailbox actually hides few steps that need to occur in a particular order:
- Create a User in the local Active Directory (return true or false).
- Force AD Connect to synchronize between the local AD and the Azure AD because there is a default 30 min delay which we can’t wait for, hence why we need to force the synchronization (return void).
- Verify if the Office365 account exist or not (return true or false).
- Verify if we still have available Office365 licenses to assign to that user.
(NOTE: if not enough licenses, an email will be sent to purchase more licenses...)
- Assign Office365 license to that user.
(NOTE: I believe the action of assigning a license to a user actually creates the mailbox...correct me if I’m wrong)
- Create a few folders under that user’s mailbox (return void).
- Create two new Contact (return void).
- Create a signature (return void).
Question
I’m a bit confused with a few things...firstly, some of these tasks (not all) can be achieved by using Microsoft Graph
while other tasks simply can’t because Graph
doesn’t support it (yet).
Secondly, I can achieve some of these tasks (for example: force AD Connect to sync) by using some PowerShell cmdlets
. In fact, I’m sure the entire steps above can be achieved using multiple cmdlets
(or simply combined within one big PowerShell script).
Thirdly, I’m grasping at straws to understand what is the best way to tackle this.
For example, considering I’m in the context of an HttpRequest
, should my Web API method use a mixture of some PowerShell cmdlets
and some Microsoft Graph
?
Should I be mixing both? Or not?
Should I instead, find all the cmdlets
, place them in order inside one big PS1 file and simply execute that one big PS1 file from within my Web API method?
Should I instead find all the Web API hidden inside each cmdlets
and use those Web API calls instead?
I seem to find a little bit of Pros and Cons in each and can’t settle on what’s best.
I have concerns that if I try to run cmdlets
directly from within my C# code that if one of the cmdlets
fails or throws an exception, then how to handle that? Likewise if I put everything in one big file...
Unfortunately, I seem to have more questions than answers and perhaps someone can help me shed some light on this :-)
Sincerely
PS: Although maybe a better solution would be to use Azure Functions with some kind of Queue but unfortunately, I’m not familiar enough with Azure Functions nor do I have the time to implement a nicer way. If possible, I’d like to stick with making this happen within the Web API method.
Based on my understanding, the Microsoft Graph
only allow us to manipulate the Microsoft cloud resource like Office 365, etc.
It is not able to using it or other REST service to force Azure AD connect to synchronize the changing from local to cloud(Start-ADSyncSyncCycle -PolicyType Delta
). Instead of mixing the business logic to different places(C# and Powershell code), I'd like to keep it in the same place(PowerShell script in this scenario) to easy to maintain. And we need to write the the exception/error handling in the PowerShell script.
Hope it is helpful.
来源:https://stackoverflow.com/questions/42519100/create-an-office365-mailbox-from-within-c-sharp-web-api-method