Web服务设置用户许可Office 365

时间:2014-06-24 02:21:01

标签: c# web-services powershell sharepoint office365

基本上,我想从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&amp;在服务器上运行的Services.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)。

1 个答案:

答案 0 :(得分:2)

在使用&#34; Connect-MsolService&#34;之前,您需要添加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();