So basically, I want to set AD's user license (Powershell script) from C# code. Here is the code:
//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;
}
However, when I run it, everything works well (unlicensed now become licensed). Then, I published the code so I get the DLLs & Services.asmx which run on the server. After that, I make a service agent and added service reference (web service URL), so periodically, the agent can call SetUserLicense function.
Here is code from service agent which calls the Web Service:
NewWSOffice365.ServicesSoapClient Service = new NewWSOffice365.ServicesSoapClient();
string Result = Service.SetUserLicense("[email protected]");
The problem is when the service agent runs, I get error:
You must call the Connect-MsolService cmdlet before calling any other cmdlets.
The weird thing, I've put Connect-MsolService in my C# code (see above). Everything meets its requirement, here: http://code.msdn.microsoft.com/office/Office-365-Manage-licenses-fb2c6413 and set IIS AppPool UserProfile to true (default: false).