Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# PowerShell
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;
 
 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 22-Aug-12 4:23am
erdener272

1 solution

Rate this: bad
good
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: http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments[^]
  Permalink  
v3
Comments
Kuthuparakkal at 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
0 Maciej Los 450
1 OriginalGriff 403
2 Sergey Alexandrovich Kryukov 239
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,972
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2014
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