0

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.

Trustos
  • 41
  • 3
  • 11
  • 2
    That's generally an error you get from [PowerShell](http://stackoverflow.com/questions/12326038/powershell-if-then-cannot-call-a-method-on-a-null-valued-expression), not from .NET. My debug your script to see what's null, perhaps you need to load a PSSnapin for your script to work. – vcsjones Apr 12 '13 at 13:47
  • When you run the PS script directly it works because you have proper privileges (probably Admin?). I am guessing your .NET app does not have the same privileges and therefore creating objects that are null, and trying to work with those. – D3vtr0n Apr 12 '13 at 13:56
  • It was cause of errors in my powershell script that C# stopped executing the script. Now i get "SEHException was unhandled. External component has thrown an exception". – Trustos Apr 12 '13 at 14:03

2 Answers2

0

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

bAN
  • 13,375
  • 16
  • 60
  • 93
-1

You could try:

System.Diagnostics.Process.Start(@"D:\NewSites\test-new\ConfigureIIS7.ps1");
Daniel Lane
  • 2,575
  • 2
  • 18
  • 33