Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从SharePoint以编程方式运行Powershell脚本文件_C#_Sharepoint_Powershell - Fatal编程技术网

C# 从SharePoint以编程方式运行Powershell脚本文件

C# 从SharePoint以编程方式运行Powershell脚本文件,c#,sharepoint,powershell,C#,Sharepoint,Powershell,我正在从SharePoint代码以编程方式运行powershell脚本文件(.ps1)。以下是源代码: using (new Impersonator("username", "domain", "password")) { using (RunspaceInvoke invoker = new RunspaceInvoke()) { invoker.Invoke("Set-ExecutionPolicy Unrestricted"); } string cmdArg

我正在从SharePoint代码以编程方式运行powershell脚本文件(.ps1)。以下是源代码:

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();
  }
}
使用(新的模拟程序(“用户名”、“域”、“密码”))
{
使用(RunspaceInvoke invoker=new RunspaceInvoke())
{
invoker.Invoke(“设置不受限制的ExecutionPolicy”);
}
string cmdArg=string.Format(@“C:\Office2Pdf\officetopdf.ps1{0}{1}”,
下载文件路径,上传文件路径);
Runspace Runspace=RunspaceFactory.CreateRunspace();
runspace.ApartmentState=System.Threading.ApartmentState.STA;
runspace.ThreadOptions=PSThreadOptions.UseCurrentThread;
Open();
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0]。MergeMyResults(
PipelineResultTypes.Error,PipelineResultTypes.Output);
收集结果=pipeline.Invoke();
var error=pipeline.error.ReadToEnd();
runspace.Close();
如果(error.Count>=1)
{
字符串错误=”;
foreach(var错误中的错误)
errors=errors+“”+Error.ToString();
}
}
我正在做的是

  • 从SharePoint文档库下载“.docx”文件
  • 调用运行可执行文件的powershell脚本以将“.docx”转换为“.pdf”文件
  • 将“.pdf”上载回SharePoint文档库
  • 示例代码用于第二部分。它在控制台应用程序中运行良好,但在SharePoint解决方案中不起作用。错误是“对象未设置为对象的实例”。但是没有办法分辨它指的是哪个对象


    最终使用了SharePoint Word自动化服务。效果很好。谢谢大家。

    听起来好像没有看到文件。您使用什么方法获取文件

    它是网络路径和webdav协议
    \\yoursite@ssl\davwwroot\site\library\filename
    将工作,但必须建立连接


    根据我使用powershell的经验,我必须让会话保持登录状态,这样才能正常工作。一旦该会话结束,它就会断开该连接

    powershell脚本很长吗?如果不是,您应该尝试在您认为可能为空的变量周围放置一个标记。让他们使用消息中的变量名重新引发新异常。在哪一行引发异常?收集结果=pipeline.Invoke();结果对象包含错误消息