Add the users to Office 365 and CRM online using Powershell - technet.microsoft.com/.../jj151815.aspx
// Create Initial Session State for runspace. - import-module MSOnline
initialSession = InitialSessionState.CreateDefault();
initialSession.ImportPSModule(new[] { "MSOnline" });
//credential - $O365Cred = Get-Credential
credential = new PSCredential(_username, securePass);
// Create command to connect office 365. - connect-msolservice -credential $O365Cred
Command connectCommand = new Command("Connect-MsolService");
connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));
//get lincense information and no of active and consumed ones - Get-MsolAccountSku
Command getLicenseCommand = new Command("Get-MsolAccountSku");
using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
{
// Open runspace.
psRunSpace.Open();
//Iterate through each command and executes it.
foreach (var com in new Command[] { connectCommand, getLicenseCommand })
{
var pipe = psRunSpace.CreatePipeline();
pipe.Commands.Add(com);
// Execute command and generate results and errors (if any).
results = pipe.Invoke();
var error = pipe.Error.ReadToEnd();
if (error.Count > 0 && com == connectCommand)
{
Console.WriteLine(error[0].ToString() + "Problem in login");
break;
}
if (error.Count > 0 && com == getLicenseCommand)
{
Console.WriteLine(error[0].ToString() + "Problem in getting licenses");
break;
}
else
{
if (results != null && com == getLicenseCommand)
{
license=results[0].Properties["AccountSkuId"].Value.ToString();
Int32.TryParse(results[0].Properties["ActiveUnits"].Value.ToString(), out totalLicenses);
Int32.TryParse(results[0].Properties["ConsumedUnits"].Value.ToString(), out usedLicenses);
}
}
}
if (totalLicenses - usedLicenses > 0)
{
//create user with license - New-MsolUser -UserPrincipalName xxx@xxx.onmicrosoft.com -AlternateEmailAddresses "xxx"
//-DisplayName "Code User" -FirstName "Code" -LastName "User" -ForceChangePassword 1 -UsageLocation "US"
//-LicenseAssignment "xxx"
Command createUserCommand = new Command("New-MsolUser");
createUserCommand.Parameters.Add((new CommandParameter("UserPrincipalName", _newUserName)));
createUserCommand.Parameters.Add((new CommandParameter("AlternateEmailAddresses", _emailAddress)));
createUserCommand.Parameters.Add((new CommandParameter("DisplayName", _firstName + " " + _lastName)));
createUserCommand.Parameters.Add((new CommandParameter("FirstName", _firstName)));
createUserCommand.Parameters.Add((new CommandParameter("LastName", _lastName)));
createUserCommand.Parameters.Add((new CommandParameter("ForceChangePassword", 1)));
createUserCommand.Parameters.Add((new CommandParameter("UsageLocation", "US")));
createUserCommand.Parameters.Add((new CommandParameter("LicenseAssignment", license)));
var pipe = psRunSpace.CreatePipeline();
pipe.Commands.Add(createUserCommand);
// Execute command and generate results and errors (if any).
results = pipe.Invoke();
var error = pipe.Error.ReadToEnd();
if (error.Count > 0)
Console.WriteLine(error[0].ToString() + "Problem in adding user");
else
generatedPassword=results[0].Properties["Password"].Value.ToString();
//send email to user with password and also add user to business unit in crm
}
// Close the runspace.
psRunSpace.Close();