Note that there are some explanatory texts on larger screens.

plurals
  1. PORunning powershell in C#, not having much luck!
    text
    copied!<p>I have created a asp.net web application for internal use that allows certain users to start and stop Virtual machines that are linked to there QA testing environments, the code behind page runs a powershell script that starts the selected server once a button is pressed on an ASP.net page.</p> <p>I have reserched and implimented alot of the code from this site but i am coming up against a few problems.</p> <p>everytime i click the button on the main web page the error that is fed back from the powershell script says"You cannot call a method on a null-valued expression." the only problem is if i run it from a powershell prompt like this ". \script\test.ps1 'W7EA9'" it works fine.</p> <p>This is the class that calls the powershell script.</p> <pre><code> public String Startserver(String Servername) { String scriptText =". \\scripts\\test.ps1 " + Servername + ""; // create Powershell runspace Runspace runspace = RunspaceFactory.CreateRunspace(); // open it runspace.Open(); // create a pipeline and feed it the script text Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(scriptText); // execute the script Collection&lt;PSObject&gt; results = new Collection&lt;PSObject&gt;(); try { results = pipeline.Invoke(); } catch (Exception ex) { results.Add(new PSObject((object)ex.Message)); } // close the runspace runspace.Close(); // convert the script result into a single string StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { stringBuilder.AppendLine(obj.ToString()); } return stringBuilder.ToString(); //return scriptText; } </code></pre> <p>and here is the powershell script it is trying to run</p> <pre><code> Param ($server = $arg[0]) $Core = get-wmiobject -namespace root\virtualization -class Msvm_Computersystem -filter "ElementName = '$server'" $status = $Core.RequestStateChange(2) ` </code></pre> <p>It may be somthing really obvious but im just not seeing it and any help would be great.</p> <p>thanks </p> <p>Chris</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload