I'm working on Office 365 plugin in C# and trying this PowerShell command:
$newLicense = New-MsolLicenseOptions -AccountSkuId example:ENTERPRISEPACK -DisabledPlans SHAREPOINTWAC,MCOSTANDARD,SHAREPOINTENTERPRISE
Set-MsolUserLicense -UserPrincipalName test@example.com -AddLicenses "example:ENTERPRISEPACK" -LicenseOptions $newLicense
In PS, this works well. In C#, I'm having trouble to run this command. Here is my C# code, from PowerShellInvoker class:
var iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new[] { MsOnline });
iss.ThrowOnRunspaceOpenError = true;
_runspace = RunspaceFactory.CreateRunspace(iss);
_runspace.Open();
_invoker = new RunspaceInvoke(_runspace);
I've tried many ways:
scriptText_ = "$newLicense = New-MsolLicenseOptions -AccountSkuId {1} -DisabledPlans SHAREPOINTWAC,MCOSTANDARD,SHAREPOINTENTERPRISE\n"+
"Set-MsolUserLicense -UserPrincipalName test@example.com -AddLicenses \"example:ENTERPRISEPACK\" -LicenseOptions $newLicense");
I use this following method to execute other commands which works perfectly:
_invoker.Invoke(scriptText_);
And also:
var pipeline = _runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText_);
return pipeline.Invoke(); // and getting back the variable
I also tried to add the variable in the Runspace object:
_runspace.SessionStateProxy.SetVariable ("newLicense", pipeline.Invoke());
But the command do not work, and returns no error. I don't really understand the PowerShell environment (I'm a beginner in this).
Thanks in advance for all the help you can provide.
I suspect you are encountering an error that the PowerShell engine isn't showing you. Try this approach instead to observe non-terminating errors:
var ps = PowerShell.Create();
ps.AddScript(@"Get-ChildItem c:\xyzzy");
var results = ps.Invoke();
if (ps.HadErrors)
{
foreach (var errorRecord in ps.Streams.Error)
{
Console.WriteLine(errorRecord);
}
}
foreach (var result in results)
{
Console.WriteLine(result);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.