Getting information from the current state (or context) of PowerShell from C #

I saw several code examples that let you load specific PowerShell scripts from C # and then call special functions from scripts:

// "dot-source my functions" ps.AddScript(". .\MyFunctions.ps1", $false) ps.Invoke() 

My question is: now that this has been done, all the functions inside this file are loaded globally; is there any way to access them as objects from c # code?

I would like to see from the code which functions were loaded into the global context, and then analyze them (see their list of parameters, etc.).

Is it possible?

Thanks lior

+4
source share
2 answers

Sure. There are many ways. Perhaps the easiest way is to call "dir function: \". Now this last sentence may bake your noodles a bit, but this is what I will do. It returns a collection of FunctionInfo instances that has a lot of metadata on it.

+3
source

Another similar option, if you are using v2, was to turn your PS1 file into PSM1 (PowerShell module). Then from C # you can use the command

 import-module path\to\module\myfunctions.psm1 

From there you can execute the command

 get-command -module myfunctions 
+2
source

Source: https://habr.com/ru/post/1346467/


All Articles