问题描述
我想我的C#的Web应用程序中运行PowerShell脚本。
当我在PowerShell中运行以下,它工作正常。
导入模块C:\\ Program Files文件\\的Microsoft Dynamics NAV \\ \\ 80 \\服务NavAdminTool.ps1
获取-NAVTenant -ServerInstance DynamicsHost
但是,当我用我的web应用程序中运行它,它告诉我
下面是我的C#code:
InitialSessionState初始= InitialSessionState.CreateDefault();
initial.ImportPSModule(新的String [] {C:\\ Program Files文件\\的Microsoft Dynamics NAV \\ \\ 80 \\服务NavAdminTool.ps1});
运行空间运行空间= RunspaceFactory.CreateRunspace(初始);
runspace.Open();
PowerShell的PS = PowerShell.Create();
ps.Runspace =运行空间;
ps.Commands.AddCommand(GET-NAVTenant -ServerInstance DynamicsHost);
的foreach()PSObject结果ps.Invoke()
{
Console.WriteLine(result.ToString());
}
有人点我在正确的方向?
更新:
使用runspace.SessionStateProxy.PSVariable.GetValue(错误),我可以看到以下错误:
这竟然是,开发商的Web服务器便无法处理请求。切换到IIS作为开发者服务解决了这个问题。现在,这两个沃图的例子工程。
I'm trying to run a powershell script within my C# web application.
When i run the following in powershell, it works fine.
Import-Module 'C:\\Program Files\\Microsoft Dynamics NAV\\80\\Service\\NavAdminTool.ps1'
Get-NAVTenant -ServerInstance DynamicsHost
But when i'm running it using my web application, it tells me
Here is my c# code:
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { "C:\\Program Files\\Microsoft Dynamics NAV\\80\\Service\\NavAdminTool.ps1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.Commands.AddCommand("Get-NAVTenant -ServerInstance DynamicsHost");
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine(result.ToString());
}
Can someone point me in the right direction??
UPDATE:
Using runspace.SessionStateProxy.PSVariable.GetValue("Error") i could see the following error:
It turned out to be that the developer web server could't handle the request. Switching to IIS as developer server solved the problem. Now both of watto's examples works.
这篇关于从.NET推出的时候导入模块将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!