0
我以编程方式从我的SharePoint代码运行PowerShell脚本文件(.ps1)。这里是源代码:以编程方式从SharePoint运行Powershell脚本文件
using (new Impersonator("username", "domain", "password"))
{
using (RunspaceInvoke invoker = new RunspaceInvoke())
{
invoker.Invoke("Set-ExecutionPolicy Unrestricted");
}
string cmdArg = String.Format(@"C:\Office2Pdf\officetopdf.ps1 {0} {1}",
DOWNLOAD_FILE_PATH, UPLOAD_FILE_PATH);
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(
PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = pipeline.Invoke();
var error = pipeline.Error.ReadToEnd();
runspace.Close();
if (error.Count >= 1)
{
string errors = "";
foreach (var Error in error)
errors = errors + " " + Error.ToString();
}
}
我在做什么是
- 下载从SharePoint文档库
- 一“的.docx”文件调用运行在可执行文件进行转换PowerShell脚本“.DOCX” 到 “.PDF” 文件
- 上载 “.PDF” 回SharePoint文档库
在S充足的代码是第二部分。它在控制台应用程序中完美工作,但不适用于SharePoint解决方案。错误是“对象未设置为对象的实例”。但是没有办法知道它指向哪个对象。
结束使用SharePoint Word Automation Services。工作得很好。多谢你们。
PowerShell脚本很长吗?如果没有,你应该尝试把[try/catch](http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx)围绕你认为可能为空的变量。让他们重新抛出新消息中带有变量名称的新例外。 – skeletank 2013-03-13 13:14:26
在哪一行发生异常? – Machinegon 2013-03-14 15:12:52
集合 results = pipeline.Invoke();结果对象包含错误消息 –
ronma
2013-03-15 00:37:35