none
c# - powerShell PSCmdlet - call funktion from runspace RRS feed

  • Question

  • Hello, i try develop a custom powerShell cmdlet in c#. The cmdlet should execute some custom powerShell function from the runspace where my cmdlet was imported. How can i achieve that?

    Everything i can find is how to execute a function inside of a newly created runspce inside of my custom cmdlet. But i need to call a function from an existing runspace.

    Thank you.

    Sunday, May 12, 2019 10:28 AM

All replies

  • Hi MD_Coding,

    Thank you for posting here.

    For your question, you could download the source file from the code project for reference.

    https://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

    It shows an article on embedding and/or launching PowerShell scripts from a C# program.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 13, 2019 3:27 AM
    Moderator
  • Hi Wendy Zang,

    thanks for your help.

    But i need something different. Here is an example powerShell script:

    importModule MyModule #Import of my cmdlets
    
    #a custom powerShell function
    function MyCustomPSFunction{
    
        return $something
    }
    
    #execute my custom cmdlet
    myCmdlet

    What i want is:

    The "myCmdlet" should call the "MyCustomPSFunction".
    But because this function lives in a different runspace, the myCmdlet cannot access this function.

    I think i need to call the Runspace.CreateNestedPipeline Method().

    But i am not sure how to do this...

    Best Regards

    Monday, May 13, 2019 8:25 AM
  • Hi MD_Coding,

    Thank you for feedback.

    It seems that your question is more related to powershell, you could get further help from the powershell forum.

    https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 14, 2019 7:03 AM
    Moderator