Exemple #1
0
        // Function to manage license.(assign and remove license)
        private void ManageLicenseOperations_Click(object sender, EventArgs e)
        {
            try
            {
                // Initialize license object and fill with values.
                Cursor.Current = Cursors.WaitCursor;
                AssignLicense assignLicense = new AssignLicense();
                assignLicense.LicenseName       = SelectLicense.SelectedValue.ToString();
                assignLicense.UserPrincipalName = SelectUser.SelectedValue.ToString();

                // Choose appropriate operation.
                ManageLicense(assignLicense);
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message);
            }
        }
Exemple #2
0
        // Add/Remove license using powershell command.
        private void ManageLicense(AssignLicense assignLicense)
        {
            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.
                string manageLicenseSwitch = "";
                if (opType == LicenseOpertaionType.Assign)
                {
                    manageLicenseSwitch = "AddLicenses";
                }
                else if (opType == LicenseOpertaionType.Remove)
                {
                    manageLicenseSwitch = "RemoveLicenses";
                }
                Command licenseCommand = new Command("Set-MsolUserLicense");
                licenseCommand.Parameters.Add((new CommandParameter("UserPrincipalName", assignLicense.UserPrincipalName)));
                licenseCommand.Parameters.Add((new CommandParameter(manageLicenseSwitch, assignLicense.LicenseName)));
                using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
                {
                    // Open runspace.
                    psRunSpace.Open();

                    // Iterate through each command and executes it.
                    foreach (var com in new Command[] { connectCommand, licenseCommand })
                    {
                        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 == licenseCommand)
                        {
                            MessageBox.Show(error[0].ToString());
                            this.DialogResult = DialogResult.None;
                            return;
                        }
                        else if (results.Count >= 0 && com == licenseCommand)
                        {
                            if (opType == LicenseOpertaionType.Assign)
                            {
                                MessageBox.Show("License assigned successfully.");
                            }
                            else if (opType == LicenseOpertaionType.Remove)
                            {
                                MessageBox.Show("License removed successfully.");
                            }
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    // Close the runspace.
                    psRunSpace.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }