C# loading Powershellv2 modules
-
I am having a problem being able to run commandlets that are in modules from C#. when I Invoke the import-module command via ps.AddScript or ps.Addcommand I do not get an error thrown on the second Invoke. can anyone help me figure out what I am doing wrong. PowerShell ps; ps = PowerShell.Create(); ps.AddScript(@"import-module GroupPolicy"); ps.Invoke(); ps.AddCommand("Get-GPO"); try { ps.Invoke(); } catch (RuntimeException runtimeException) { System.Console.WriteLine("Runtime exception: {0}: {1}\n{2}", runtimeException.ErrorRecord.InvocationInfo.InvocationName, runtimeException.Message, runtimeException.ErrorRecord.InvocationInfo.PositionMessage); }
-
I am having a problem being able to run commandlets that are in modules from C#. when I Invoke the import-module command via ps.AddScript or ps.Addcommand I do not get an error thrown on the second Invoke. can anyone help me figure out what I am doing wrong. PowerShell ps; ps = PowerShell.Create(); ps.AddScript(@"import-module GroupPolicy"); ps.Invoke(); ps.AddCommand("Get-GPO"); try { ps.Invoke(); } catch (RuntimeException runtimeException) { System.Console.WriteLine("Runtime exception: {0}: {1}\n{2}", runtimeException.ErrorRecord.InvocationInfo.InvocationName, runtimeException.Message, runtimeException.ErrorRecord.InvocationInfo.PositionMessage); }
-
Planker wrote:
PowerShell ps;
And where exactly did you get the class PowerShell? And do you expect an exception or are you just noting that one does not occur?
-
according to this MSDN article http://msdn.microsoft.com/en-us/library/ff458115(v=vs.85).aspx the assembiles are in c:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\V1.0 but when I add these as a reference I get the same unknown command when trying to add the Grouppolicy module any ideas why this is not working?
-
I am having a problem being able to run commandlets that are in modules from C#. when I Invoke the import-module command via ps.AddScript or ps.Addcommand I do not get an error thrown on the second Invoke. can anyone help me figure out what I am doing wrong. PowerShell ps; ps = PowerShell.Create(); ps.AddScript(@"import-module GroupPolicy"); ps.Invoke(); ps.AddCommand("Get-GPO"); try { ps.Invoke(); } catch (RuntimeException runtimeException) { System.Console.WriteLine("Runtime exception: {0}: {1}\n{2}", runtimeException.ErrorRecord.InvocationInfo.InvocationName, runtimeException.Message, runtimeException.ErrorRecord.InvocationInfo.PositionMessage); }
First step in mixed language programming is to create the functionality, at least a framework, in one language. In your case create a PowerShell script first that runs only as a PowerShell script. Other than that several things as pure guesses. 1. Invoke starts a process. It might be the case that you shouldn't be invoking until everything is in place. 2. AddScript/AddCommand returns a PowerShell instance. That suggests to me that it might be acting like String.Replace() in that the original instance does not have the command.
-
First step in mixed language programming is to create the functionality, at least a framework, in one language. In your case create a PowerShell script first that runs only as a PowerShell script. Other than that several things as pure guesses. 1. Invoke starts a process. It might be the case that you shouldn't be invoking until everything is in place. 2. AddScript/AddCommand returns a PowerShell instance. That suggests to me that it might be acting like String.Replace() in that the original instance does not have the command.
my issues seem to be that I am doing something incorectly to load the Group policy Powershell module. I am calling invoke after the import-Module command so I can call a cmdlet that is not loaded until the Group policy Module has been loaded. I am getting an exception on the import-Module command that it can't be found so either I am calling the wrong powershell classes or I am setting up my runspace incorectly.