We want to store and manage PowerShell scripts in a database an execute them via C#. How can we discover the parameters of such a script before executing it? so we can set them to known values or prompt a user for values.
Some clarification:
We create a management system MS.
- An admin stores a PowerShell script in the MS database.
- Later a different admin selects this script from a list offered by the MS.
- MS discovers the parameters of the script.
- MS prompts the admin for values.
MS executes the script with the parameters supplied.
string testScript = @" { param( [ValidateNotNullOrEmpty()] [string]$Name ) get-process $name "; Dictionary<string,object> DiscoverParameters() { using (PowerShell psi = PowerShell.Create()) { psi.AddScript(testScript); var pars = new Dictionary<string,object>(); //How do we get at the parameters return pars; } } void ExecuteScript(Dictionary<string,object> pars) { using (PowerShell psi = PowerShell.Create()) { psi.AddScript(testScript); pars.ToList().ForEach(p => psi.AddParameter(p.Key, p.Value)); Collection<PSObject> PSOutput = psi.Invoke(); //... } }