The term `` is not recognized as the name of the cmdlet,

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

# 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();
}
+5
source share
1 answer

Can you simply install the module OpenXmlPowerToolsin the module path of the PowerShell system:

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

All Articles