private void SaveUser_Click(object sender, EventArgs e) { try { // Check for required parameters. if (CheckRequiredFields()) { // Initialize user object and fill with values. Cursor.Current = Cursors.WaitCursor; O365User userEntry = new O365User(); userEntry.DisplayName = DisplayName.Text; userEntry.FirstName = FirstName.Text; userEntry.LastName = LastName.Text; userEntry.UserPrincipalName = UserPrincipalName.Text; userEntry.Department = Department.Text; userEntry.Country = Country.Text; userEntry.UsageLocation = UsageLocation.Text; // Choose appropriate operation. UserOperation(userEntry); Cursor.Current = Cursors.Default; } } catch (Exception ex) { Cursor.Current = Cursors.Default; MessageBox.Show(ex.Message); } }
/// <summary> /// Perform add/update user operation using command New-MsolUser / Set-MsolUser. /// </summary> /// <param name="userEntry"></param> private void UserOperation(O365User userEntry) { try { // Create Initial Session State for runspace. InitialSessionState initialSession = InitialSessionState.CreateDefault(); initialSession.ImportPSModule(new[] { "MSOnline" }); // Create credential object. PSCredential credential = new PSCredential(UserCredential.UserName, UserCredential.Password); // Create command to connect office 365. Command connectCommand = new Command("Connect-MsolService"); // Create command to add/update office 365 user. Command userCommand = null; if (opType == UserOpertaionType.Save) { userCommand = new Command("New-MsolUser"); } else if (opType == UserOpertaionType.Update) { userCommand = new Command("Set-MsolUser"); } userCommand.Parameters.Add((new CommandParameter("UserPrincipalName", userEntry.UserPrincipalName))); userCommand.Parameters.Add((new CommandParameter("DisplayName", userEntry.DisplayName))); userCommand.Parameters.Add((new CommandParameter("FirstName", userEntry.FirstName))); userCommand.Parameters.Add((new CommandParameter("LastName", userEntry.LastName))); userCommand.Parameters.Add((new CommandParameter("Country", userEntry.Country))); userCommand.Parameters.Add((new CommandParameter("Department", userEntry.Department))); userCommand.Parameters.Add((new CommandParameter("UsageLocation", userEntry.UsageLocation))); using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession)) { // Open runspace. psRunSpace.Open(); //Iterate through each command and executes it. foreach (var com in new Command[] { connectCommand, userCommand }) { var pipe = psRunSpace.CreatePipeline(); pipe.Commands.Add(com); // Execute command and generate results and errors (if any). Collection <PSObject> results = pipe.Invoke(); var error = pipe.Error.ReadToEnd(); if (error.Count > 0 && com == userCommand) { MessageBox.Show(error[0].ToString()); this.DialogResult = DialogResult.None; return; } else if (results.Count >= 0 && com == userCommand) { // If user added successfully then display some information of newly added user. if (opType == UserOpertaionType.Save) { MessageBox.Show("UserPrincipalName : " + results[0].Properties["UserPrincipalName"].Value.ToString() + "\r\n" + "Default Password : "******"Password"].Value.ToString(), "User added successfully."); } else if (opType == UserOpertaionType.Update) { MessageBox.Show("User details updated successfully."); } this.DialogResult = DialogResult.OK; } } // Close the runspace. psRunSpace.Close(); } } catch (Exception) { throw; } }