I have a PowerShell script stored in the file MergeDocuments.ps1. When I run the script from the Windows PowerShell command prompt, it works fine
.\MergeDocuments.ps1 1.docx 2.docx merge.docx
Calling a script from a Windows console application is also great.
When I tried to call the script from the Asp.Net web service, I had some problems with accessing the registry. I used impersonation and gave permission to the network service account in the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShellto solve this problem
Next, I ran into the problem that PowerShell was unable to create objects of type OpenXmlPowerTools.DocumentSource [], so I added the following to my script
Add-Type -Path "C:\Users\Administrator\Documents\WindowsPowerShell\Modules\OpenXmlPowerTools\OpenXmlPowerTools.dll"
Import-Module OpenXmlPowerTools
Now the current problem is that I get the error "The term" Merge-OpenXmlDocument "is not recognized as the name of the cmdlet ..."
How can i solve this?
Powerhell script
Add-Type -Path "C:\Users\Administrator\Documents\WindowsPowerShell\Modules\OpenXmlPowerTools\OpenXmlPowerTools.dll"
Import-Module OpenXmlPowerTools
$noOfSourceDocs = ($($args.length) - 1)
[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))
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke invoker = new RunspaceInvoke(runspace);
invoker.Invoke("Set-ExecutionPolicy Unrestricted");
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();
}
source
share