简体   繁体   中英

Running powershell script with C# code

When I try to run the script with my C# code. I get the following error:

  "You cannot call a method on a null-valued expression."

Would this be my C# code that is wrong

        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();

Or should I look for the problem in my powershell script? The shell script works when executed normally. Thanks in advance.

I'm not a specialist at all but it seems your error is a POWERSHELL error.

See This for example.

I think the problem is located into your PowerShell script

您可以尝试:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM