2012-04-22 61 views
5

我有一個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

回答

1

你能不能嘗試安裝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

相關問題