title: How to discover PowerShell Script parameters in c#
body: <p>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.</p>
  
  <p>Some clarification:</p>
  
  <p>We create a management system MS.</p>
  
  <ol>
  <li>An admin stores a PowerShell script in the MS database.</li>
  <li>Later a different admin selects this script from a list offered by the MS.</li>
  <li>MS discovers the parameters of the script.</li>
  <li>MS prompts the admin for values.</li>
  <li><p>MS executes the script with the parameters supplied.</p>
  
  <pre><code>    string testScript = @"
          {
              param(
                  [ValidateNotNullOrEmpty()]
                  [string]$Name
              )
              get-process $name
          ";
  
  Dictionary&lt;string,object&gt; DiscoverParameters()
  {
      using (PowerShell psi = PowerShell.Create())
      {
          psi.AddScript(testScript);
          var pars = new Dictionary&lt;string,object&gt;();
          //How do we get at the parameters
          return pars;
      }
  }
  
  void ExecuteScript(Dictionary&lt;string,object&gt; pars)
  {
      using (PowerShell psi = PowerShell.Create())
      {
          psi.AddScript(testScript);
          pars.ToList().ForEach(p =&gt; psi.AddParameter(p.Key, p.Value));
          Collection&lt;PSObject&gt; PSOutput = psi.Invoke();
          //...
      }
  }
  </code></pre></li>
  </ol>