Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# loading Powershellv2 modules

C# loading Powershellv2 modules

Scheduled Pinned Locked Moved C#
helpcsharpwindows-admin
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Planker
    wrote on last edited by
    #1

    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); }

    J 2 Replies Last reply
    0
    • P Planker

      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); }

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      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?

      P 1 Reply Last reply
      0
      • J jschell

        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?

        P Offline
        P Offline
        Planker
        wrote on last edited by
        #3

        I edited the Project file to add the Reference

        P 1 Reply Last reply
        0
        • P Planker

          I edited the Project file to add the Reference

          P Offline
          P Offline
          Planker
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • P Planker

            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); }

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            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.

            P 1 Reply Last reply
            0
            • J jschell

              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.

              P Offline
              P Offline
              Planker
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups