Powershell Cmdlet used to invoke a Script Block or function in a .ps1 file RRS feed

  • Question

  • Hi,

    Say I have a .ps1 file with some PowerShell script functions in them. I need to invoke a particular function from a C# cmdlet. 

    This link ( https://msdn.microsoft.com/en-us/library/ms714819(v=vs.85).aspx ) says how to invoke a script block but it does not provide a complete sample. Its not clear what the parameters are in the call 

    script.Invoke(line, simpleMatch, caseSensitive );

    Is there a complete example available which shows how to call a function in a Powershell script file?

    Is it mandatory to have a script block in such a case or can a function be invoked directly given a file path?



    Wednesday, April 25, 2018 1:54 PM

All replies

  • Hello Jy_13,

    I started Powershell command from below blog.

    Executing PowerShell scripts from C#

    and you could refer the below link.

    How to call Powershell Script function from C# ?

    Best Regards,

    Neil Hu

    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.

    Thursday, April 26, 2018 6:58 AM
  • This is only a snippet from the complete Code Sample listed on 'Creating a Cmdlet to Access a Data Store'. Unfortunately, the .NET PowerShell documentation is sometimes a tad too concise! 

    But, there might be better ways of running a PowerShell script from C# code, as shown on the links referred to by Fei Hu, esp. in the blog by Keith Babinec, or in the examples listed in the Microsoft documentation 'Runspace Samples'.


    • Proposed as answer by Fei Hu Wednesday, May 16, 2018 9:41 AM
    Thursday, April 26, 2018 2:11 PM