术语''不被识别为cmdlet的名称,

时间:2012-04-22 18:49:59

标签: c# asp.net powershell

我有一个存储在文件MergeDocuments.ps1中的PowerShell脚本。当我从Windows PowerShell命令提示符运行脚本时,它运行正常 .\MergeDocuments.ps1 1.docx 2.docx merge.docx

从Windows控制台应用程序调用脚本也运行良好。

当我尝试从Asp.Net Web服务调用脚本时,我遇到了一些有关注册表访问的问题。我使用模拟并将网络服务帐户的权限授予注册表项HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell以解决此问题

接下来我遇到了关于PowerShell无法创建OpenXmlPowerTools.DocumentSource []类型对象的问题,所以我在脚本中添加了以下内容

Add-Type -Path "C:\Users\Administrator\Documents\WindowsPowerShell\Modules\OpenXmlPowerTools\OpenXmlPowerTools.dll"
Import-Module OpenXmlPowerTools

现在的问题是我收到错误“术语'Merge-OpenXmlDocument'未被识别为cmdlet的名称,...”

我该如何解决?

PowerShell脚本

Add-Type -Path "C:\Users\Administrator\Documents\WindowsPowerShell\Modules\OpenXmlPowerTools\OpenXmlPowerTools.dll"

Import-Module OpenXmlPowerTools

# The last argument is the path of the merged document
$noOfSourceDocs = ($($args.length) - 1)

# Create an array to hold all the source documents
[OpenXmlPowerTools.DocumentSource[]] $docs = New-Object OpenXmlPowerTools.DocumentSource[] $noOfSourceDocs

for ($i = 0; $i -lt $noOfSourceDocs; $i++)
{
    $docs[$i] = New-Object -TypeName OpenXmlPowerTools.DocumentSource -ArgumentList $args[$i]
    $docs[$i].KeepSection = 1
}

Merge-OpenXmlDocument -OutputPath $args[-1] -Sources $docs

Webservice .Net Code

using (new Impersonator(username, domain, password))
{
   // create Powershell runspace
   Runspace runspace = RunspaceFactory.CreateRunspace();
   runspace.Open();

   RunspaceInvoke invoker = new RunspaceInvoke(runspace);
   invoker.Invoke("Set-ExecutionPolicy Unrestricted");

   // create a pipeline and feed it the script file
   Pipeline pipeline = runspace.CreatePipeline();
   Command command = new Command(ConfigurationManager.AppSettings["PowerShellScript"]);
   foreach (var file in filesToMerge)
   {
      command.Parameters.Add(null, file);
   }
   command.Parameters.Add(null, outputFilename);
   pipeline.Commands.Add(command);

   pipeline.Invoke();
   runspace.Close();
}

1 个答案:

答案 0 :(得分:1)

您是否可以尝试在PowerShell系统模块路径中安装OpenXmlPowerTools模块:

C:\Windows\system32\WindowsPowerShell\v1.0\Modules
相关问题