-1

我像这样编写了函数 PowerShell 模块:

    Function docConvert2{
         param ([string]$sourceFile, [string]$sourceFilePath)
         ....
         ....
    }

我已经成功导入模块

在此处输入图像描述

我可以在 powershell cmdlet 中使用模块

在此处输入图像描述

当我在 C# 中尝试调用函数时,我得到了这样的异常

术语“docConvert2”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。

C# 代码

        PowerShell pShell = PowerShell.Create();
        pShell.Commands.AddCommand("import-module").AddParameter("Name", "DocConverter2");
        pShell.Invoke();//works correctly
        pShell.AddCommand("docConvert2");
        pShell.AddParameter("sourceFile", "'addendum no-3_PREP.doc'");
        pShell.AddParameter("sourceFilePath", @"'D:\New\wordler'");
        pShell.Invoke();//throw exception

我的错误是什么?

4

2 回答 2

1

尝试:

PowerShell pShell = PowerShell.Create();
pShell.Commands.AddCommand("import-module").AddParameter("Name","DocConverter2");
pShell.Invoke();
...
rest of you code here
...

您还可以使用 InitialSessionState 来预加载模块:请阅读此处

于 2013-09-18T10:41:02.547 回答
0

实例化你的pShell对象后,你需要导入你的模块,就像你从命令行做的一样。

尝试在第 1 行和第 2 行之间执行以下操作。

        ps.AddScript(@"import-module DocConverter2");
        ps.Invoke();

并确保您的 C# 应用程序可以访问 PSM 所在的路径。

于 2013-09-18T10:39:21.353 回答