Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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#代码运行powershell脚本_C#_Powershell - Fatal编程技术网

使用C#代码运行powershell脚本

使用C#代码运行powershell脚本,c#,powershell,C#,Powershell,当我试图用我的C代码运行脚本时。我得到以下错误: "You cannot call a method on a null-valued expression." 这是我错的C代码吗 Runspace runSpace = RunspaceFactory.CreateRunspace(); runSpace.Open(); RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace

当我试图用我的C代码运行脚本时。我得到以下错误:

  "You cannot call a method on a null-valued expression."
这是我错的C代码吗

        Runspace runSpace = RunspaceFactory.CreateRunspace();
        runSpace.Open();

        RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
        Pipeline pipeLine = runSpace.CreatePipeline();
        pipeLine.Commands.AddScript(@"D:\NewSites\test-new\ConfigureIIS7.ps1");
        try
        {
            pipeLine.Invoke();
        }
        catch (Exception ex)
        {
            throw ex;
        }

        pipeLine.Stop();

        runSpace.Close();
还是应该在powershell脚本中查找问题?shell脚本在正常执行时工作。
提前谢谢。

我根本不是专家,但您的错误似乎是POWERSHELL错误

例如,见

我认为问题出在您的PowerShell脚本中

您可以尝试:

System.Diagnostics.Process.Start(@"D:\NewSites\test-new\ConfigureIIS7.ps1");

这通常是您从中得到的错误,而不是从.NET得到的错误。我调试你的脚本以查看什么是空的,也许你需要加载一个PSSnapin才能让你的脚本工作。当你直接运行PS脚本时,它可以工作,因为你有适当的权限(可能是管理员?)。我猜你的.NET应用程序没有相同的权限,因此会创建空对象,并尝试使用这些对象。我的powershell脚本中出现错误的原因是C#停止执行该脚本。现在我看到“SEHException未处理。外部组件引发了异常”。