Click here to Skip to main content
11,922,641 members (29,412 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# PowerShell

I am new powershell script in c#. I have a powershell script file ps.ps1 and powershell settingfile ConsoleSettings.psc1

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -psconsolefile "D:\e\ConsoleSettings.psc1" -noexit -command ". 'D:\e\ps.ps1'"
run it and get
Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled
my function result correctly.
Now, i want to get this result in c# . My code is;

private void button1_Click(object sender, EventArgs e)

           private string RunScript(string scriptText)
               PSConsoleLoadException x = null; ;
               RunspaceConfiguration rsconfig = RunspaceConfiguration.Create(@"d:\e\ConsoleSettings.psc1", out x);
               Runspace runspace = RunspaceFactory.CreateRunspace(rsconfig);
               RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
               runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
               Pipeline pipeline = runspace.CreatePipeline();
               pipeline.Commands.Add("Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60   -enabled");
   Collection<PSObject> results = pipeline.Invoke();
               StringBuilder stringBuilder = new StringBuilder();
               foreach (PSObject obj in results)
               return stringBuilder.ToString();
           private string LoadScript(string filename)
                   using (StreamReader sr = new StreamReader(filename))
                       StringBuilder fileContents = new StringBuilder();
                       string curLine;
                       while ((curLine = sr.ReadLine()) != null)
                           fileContents.Append(curLine + "\n");
                       return fileContents.ToString();
               catch (Exception e)
                   string errorText = "The file could not be read:";
                   errorText += e.Message + "\n";
                   return errorText;
And then i have a error : the term "Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled" is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How to solve this problem, or how to call powershell script with configfile, parameter like (Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled) in c#

please help me...
Posted 22-Aug-12 5:23am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Why don't you use the PowerShell class[^]? It does several things for you.

By the way, you dont pass parameters as parameters. Threat runspace pipeline as a process execution. You won't add parameters to the executable name, won't you? Use the CommandParameter[^] class. See here:[^]
Kuthuparakkal 22-Aug-12 12:46pm
nice reply 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151125.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100