从.NET推出的时候导入模块将无法正常工作无法正常、模块、时候、工作

2023-09-03 06:49:59 作者:要麽留要麽滾

我想我的C#的Web应用程序中运行PowerShell脚本。

当我在PowerShell中运行以下,它工作正常。

 导入模块C:\\ Program Files文件\\的Microsoft Dynamics NAV \\ \\ 80 \\服务NavAdminTool.ps1

获取-NAVTenant -ServerInstance DynamicsHost
 

但是,当我用我的web应用程序中运行它,它告诉我

  

术语GET-NAVTenant -ServerInstance DynamicsHost'未被识别为cmdlet,函数,脚本文件或可操作的程序的名称。检查名称的拼写,或者是否包含路径,验证路径是否正确,然后重试。

OneNET 项目列表模块使用教学

下面是我的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(错误),我可以看到以下错误:

  

无法绑定参数参数'名',因为它为空。

     

无法绑定参数参数'路径',因为它为空。

     

找不到路径。HKLM:\ SOFTWARE \微软\ Microsoft动态NAV \ 80 \服务',因为它不存在。

     

这是提示用户的命令失败,因为主机程序或命令类型不支持用户交互。尝试支持用户交互,如Windows PowerShell控制台或Windows PowerShell ISE中的主机方案,并从不支持用户交互命令的类型,如Windows PowerShell的工作流程,及时删除相关的命令。

     

这是提示用户的命令失败,因为主机程序或命令类型不支持用户交互。尝试支持用户交互,如Windows PowerShell控制台或Windows PowerShell ISE中的主机方案,并从不支持用户交互命令的类型,如Windows PowerShell的工作流程,及时删除相关的命令。

     

这是提示用户的命令失败,因为主机程序或命令类型不支持用户交互。尝试支持用户交互,如Windows PowerShell控制台或Windows PowerShell ISE中的主机方案,并从不支持用户交互命令的类型,如Windows PowerShell的工作流程,及时删除相关的命令。

解决方案

这竟然是,开发商的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

The term 'Get-NAVTenant -ServerInstance DynamicsHost' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

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:

Cannot bind argument to parameter 'Name' because it is null.

Cannot bind argument to parameter 'Path' because it is null.

Cannot find path 'HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\80\Service' because it does not exist.

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

解决方案

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.