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

问题描述:

我有一个PowerShell脚本存储在文件MergeDocuments.ps1中。当我从Windows PowerShell命令提示符下运行该脚本运行良好
.\MergeDocuments.ps1 1.docx 2.docx merge.docx术语“'不被识别为cmdlet的名称,

调用从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 

现在目前的问题是,我得到错误“‘合并-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代码

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

我很好奇为什么你想首先从asp.net执行powershell脚本。 – 2012-04-22 19:05:29

+0

我不确定,但是'OpenXmlPowerTools.dll'可能会定位到'v2.0.50727' - 检查如何构建C#DLL?如果它是.NET 4.0,我已经看到了类似的问题 - 您需要定位到3.5。试着让我知道。 – NSGaga 2012-04-22 21:45:14

+0

@seth我需要合并多个Word文档,OpenXmlPowerTools是我能找到的最好的工具。这就是为什么我需要从Web服务运行PowerShell脚本。他们有任何其他建议吗? – 2012-04-23 06:38:15

你能不能尝试安装OpenXmlPowerTools模块中的PowerShell的系统模块路径:

C:\Windows\system32\WindowsPowerShell\v1.0\Modules 
+0

我已将OpenXmlPowerTool文件夹及其中的DLL放入您指定的文件夹中。代码正在工作,它不再抱怨Merge-OpenXmlDoucment。但是,合并文档不会生成! – 2012-04-23 14:35:41

+0

这看起来像是Merge-OpenXmlDocument cmdlet本身的一个问题。你的答案应该被标记为答案。 – 2012-04-23 14:40:28