gpt4 book ai didi

c# - Web 服务集用户许可证 Office 365

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:30 25 4
gpt4 key购买 nike

基本上,我想通过 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 和 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)。

最佳答案

您需要在使用“Connect-MsolService”之前添加 Powershell session 凭据是您的上述凭据。

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();

关于c# - Web 服务集用户许可证 Office 365,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24377390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com