Web服务集用户许可证Office 365

本文关键字:Office 许可证 用户 服务 Web | 更新日期: 2023-09-27 18:10:04

所以基本上,我想从c#代码设置AD的用户许可(Powershell脚本)。下面是代码:

//adminUser & adminPassword from app.config
    public static string SetUserLicense(string userPrincipalName, string adminUser, SecureString adminPassword, string licenses)
            {
                string strReturn = "";
                try
                {
                    // Create Initial Session State for runspace.
                    InitialSessionState initialSession = InitialSessionState.CreateDefault();
                    initialSession.ImportPSModule(new[] { "MSOnline" });
                    // Create credential object.
                    PSCredential credential = new PSCredential(adminUser, adminPassword);
                    // Create command to connect office 365.
                    Command connectCommand = new Command("Connect-MsolService");
                    connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));
                    Command userCommand = new Command("Set-MsolUser");
                    userCommand.Parameters.Add((new CommandParameter("UserPrincipalName", userPrincipalName)));
                    userCommand.Parameters.Add((new CommandParameter("UsageLocation", "ID")));
                    Command licCommand = new Command("Set-MsolUserLicense");
                    licCommand.Parameters.Add((new CommandParameter("UserPrincipalName", userPrincipalName)));
                    licCommand.Parameters.Add((new CommandParameter("AddLicenses", licenses)));
                    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, licCommand })
                        {
                            if (com != null)
                            {
                                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 == licCommand)
                                {
                                    strReturn = error[0].ToString();
                                }
                                else if (results.Count >= 0 && com == licCommand)
                                {
                                    strReturn = "User License update successfully.";
                                }
                            }
                        }
                        // Close the runspace.
                        psRunSpace.Close();
                    }
                }
                catch (Exception ex)
                {
                    strReturn = ex.Message;
                }
                return strReturn;
            }

然而,当我运行它时,一切都工作正常(未授权现在变成授权)。然后,我发布了代码,所以我得到了dll;服务。在服务器上运行的Asmx。之后,我制作了一个服务代理,并添加了服务引用(web服务URL),这样代理就可以定期调用SetUserLicense函数。

下面是服务代理调用Web服务的代码:

NewWSOffice365.ServicesSoapClient Service = new NewWSOffice365.ServicesSoapClient();
string Result = Service.SetUserLicense("blabla@bns.org");

问题是服务代理运行时,我得到错误:

在调用其他cmdlet之前,必须先调用Connect-MsolService cmdlet

奇怪的是,我把Connect-MsolService放在了我的c#代码中(见上文)。这里:http://code.msdn.microsoft.com/office/Office-365-Manage-licenses-fb2c6413并将IIS AppPool UserProfile设置为true(默认为false)。

Web服务集用户许可证Office 365

在使用"Connect-MsolService"之前需要添加Powershell会话证书就是您的上述证书。

PSCommand psSession = new PSCommand();
psSession.AddCommand("New-PSSession");
psSession.AddParameter("ConfigurationName", "Microsoft.Exchange");
psSession.AddParameter("ConnectionUri", new Uri("https://outlook.office365.com/powershell-liveid/"));
psSession.AddParameter("Credential", credential);
psSession.AddParameter("Authentication", "Basic");
psSession.AddParameter("AllowRedirection");
powershell.Commands = psSession;
powershell.Invoke();
PSCommand connect = new PSCommand();
connect.AddCommand("Connect-MsolService");
connect.AddParameter("Credential", credential);
powershell.Commands = connect;
powershell.Invoke();