Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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;

C#
private void button1_Click(object sender, EventArgs e)
           {
               RunScript(LoadScript(@"d:\e\ps.ps1"));
           }


           private string RunScript(string scriptText)
           {
               PSConsoleLoadException x = null; ;
               RunspaceConfiguration rsconfig = RunspaceConfiguration.Create(@"d:\e\ConsoleSettings.psc1", out x);
               Runspace runspace = RunspaceFactory.CreateRunspace(rsconfig);
               runspace.Open();
               RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
               runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
               Pipeline pipeline = runspace.CreatePipeline();
               pipeline.Commands.AddScript(scriptText);

               pipeline.Commands.Add("Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60   -enabled");
   Collection<PSObject> results = pipeline.Invoke();

               runspace.Close();
               StringBuilder stringBuilder = new StringBuilder();
               foreach (PSObject obj in results)
               {
                   stringBuilder.AppendLine(obj.ToString());
               }
               return stringBuilder.ToString();
           }

           private string LoadScript(string filename)
           {
               try
               {
                   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

1 solution

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: http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments[^]
 
Share this answer
 
v3
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900