title: Invoke Powershell Command from .net framework 2.0 / 3.5 project
body: <p>I'm trying to invoke a Powershell Command <code>Get-DSCLocalConfigurationManager</code> from a windows service project (.net framework 2.0). <code>using System.Management.Automation</code></p>
  
  <pre><code>using (Runspace runspace = RunspaceFactory.CreateRunspace())
  {
      try
      {
          runspace.Open();
  
          var ps = PowerShell.Create();
          ps.Runspace = runspace;
          ps.AddCommand("Get-DSCLocalConfigurationManager");
  
          var results = ps.Invoke();
  
          foreach (PSObject result in ps.Invoke())
          {
              _eventLog.WriteEntry(result.Members["AgentId"].Value.ToString());
          }
      }
      catch (Exception e)
      {
          _eventLog.WriteEntry($"Powershell Command failed with: {e.Message}", EventLogEntryType.Error);
      }
  }
  </code></pre>
  
  <p>Invoking the command in a powershell window works as expected.</p>
  
  <blockquote>
    <p>The problem is that pretty much every command i try to invoke this way returns an exception:<br>
    <em>"The term 'Get-DSCLocalConfigurationManager' is not recognized as the name of a cmdlet,..."</em></p>
  </blockquote>
  
  <p>I tried the commands <code>Get-Module -ListAvailable</code> <code>$PSVersionTable.PSVersion</code> with the same result. There are commands that work like <code>Get-Process</code>, so i assume there are missing modules.</p>
  
  <p>Importing the module and invoking the command like this</p>
  
  <pre><code>ps.AddScript(@"Import -Module 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1'; Get-DSCLocalConfigurationManager");
  </code></pre>
  
  <p>also doesn't work as it returns the following error:</p>
  
  <blockquote>
    <p>The 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ('ModuleToProcess', 'NestedModules', 'GUID', 'Author', 'CompanyName', 'Copyright', 'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName', 'PowerShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitecture', 'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'PrivateData', 'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsToExport', 'VariablesToExport', 'AliasesToExport', 'CmdletsToExport'). Remove the members that are not valid ('DscResourcesToExport', 'HelpInfoURI'), then try to import the module again.</p>
  </blockquote>
  
  <p>What Powershell version is used when invoking commands like this? Is it tied to the .net framework version I'm using or is it the installed version of the system? What Modules are loaded? What can i do to invoke this command?</p>
  
  <p><strong>Edit: I created a simple console application and found out, that this is connected with the framework version. With .net framework 4.0 this works as expected, but as soon as i use 3.5 or lower the problem i describe arises.</strong></p>