我无法在c#程序中运行SkypeOnline PowerShell脚本

时间:2017-09-30 07:54:55

标签: c# powershell skype-for-business

我正在尝试在C#程序中运行此SkypeOnline脚本。 并且有例外。 脚本在PowerShell命令行中运行良好。

using (var powerShellInstance = PowerShell.Create())
{
    powerShellInstance.Commands
        .AddCommand("Import-Module")
        .AddArgument("SkypeOnlineConnector");

    var script = 
        "$Username =\"username\"\n" + 
        "$Password =\"password\"\n" +
        "$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force\n" +
        "$cred = new-Object System.Management.Automation.PSCredential ($Username, $secpasswd)\n" +
        "$CSSession = New-CsOnlineSession -Credential $cred\n" +
        "Import-PSSession $CSSession -AllowClobber\n" + 
        "Get-CsOnlineUser";

    powerShellInstance.AddScript(script);
    powerShellInstance.Invoke();
}

这导致异常:

Unhandled Exception: System.Management.Automation.CmdletInvocationException: Method not found: 'Microsoft.IdentityModel.
Clients.ActiveDirectory.AuthenticationResult Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.Acqui
reToken(System.String, System.String, System.Uri, Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior)'. --->
 System.MissingMethodException: Method not found: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireToken(System.String, System.String, System.
Uri, Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior)'.
   at Microsoft.Rtc.Management.OnlineConnector.GetAccessToken.BeginProcessing()
   at System.Management.Automation.Cmdlet.DoBeginProcessing()
   at System.Management.Automation.CommandProcessorBase.DoBegin()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper(TInput,TOutput)(PSDataCollection'1 input, PSDataCollectio
n'1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke(TInput,TOutput)(PSDataCollection'1 input, PSDataCollection'1 ou
tput, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at MSPControl.Providers.MicrosoftOnline.Tests.Console.Program.Main(String[] args) in Program.cs:line 22

0 个答案:

没有答案