Powershell脚本在Console应用程序上工作但不在ASP.NET

时间:2017-06-30 12:34:13

标签: c# asp.net powershell iis skype-for-business

我有一个PowerShell脚本可以在Skype for Business Online上连接,它正在使用控制台应用程序上的powershell,但是当我从ASP.NET调用时不能正常工作

我运行ASP.NET时的异常:

“术语'Get-CsPowerShellEndpoint'未被识别为cmdlet,函数,脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确然后再试一次“

string command = @"$PlainPassword ='****';
                                    $UserName = '****';
                                    $SecurePassword = $PlainPassword | ConvertTo-SecureString-AsPlainText -Force;
                                    $SkypeOnlineCred = New - Object - TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword;
                                    Remove-Variable -Name PlainPassword;
                                    Remove-Variable -Name SecurePassword;
                                    Remove-Variable -Name UserName;
                                    $SkypeOnlineSession = New-CsOnlineSession Credential $SkypeOnlineCred;
                                         Import-PSSession -Session $SkypeOnlineSession | Out-Null;";

var initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] {
                    "C:\\Program Files\\Common Files\\Skype for Business Online\\Modules\\SkypeOnlineConnector\\SkypeOnlineConnectorStartup.psm1"
                 });

                
using (Runspace runspace = RunspaceFactory.CreateRunspace(initial))
{
	// Open runspace 
	runspace.Open();

	// Initialize PowerShell engine
	using (PowerShell shell = PowerShell.Create())
	{
		shell.Runspace = runspace;
		

		// Add the script to the PowerShell object
		shell.Commands.AddScript(command);
		try
		{
			// Execute the script
			var results = shell.Invoke();

			if (shell.Streams.Error.Count > 0)
			{
				throw new Exception(shell.Streams.Error[0].Exception.Message);
			}
			// display results, with BaseObject converted to string
			// Note : use |out-string for console-like output
			return results;
		}
		catch (Exception e)
		{
			throw new Exception("On Invoke" + e.Message);
		}          
	}
}

0 个答案:

没有答案