sh_*_*alh 5 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
Run Code Online (Sandbox Code Playgroud)
现在的问题是我收到错误"术语'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
Run Code Online (Sandbox Code Playgroud)
Web服务.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();
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试OpenXmlPowerTools在 PowerShell 系统模块路径中安装模块吗:
C:\Windows\system32\WindowsPowerShell\v1.0\Modules
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9323 次 |
| 最近记录: |