title: C# -- Windows Server 2012 installing Features and Roles using Powershell
body: <p>I have written an application that installs Windows Roles and Features using the Powershell API.  It works just fine in Windows 2008 R2, but nothing happens in Windows 2012; the program just moves on as if everything happened just fine, but nothing is installed.</p>
  
  <p>I've tried making the program .NET v4.5 (it was .NET v2.0), but that didn't help.  I've been all over Google about this and I can't find a solution that works.  In fact, most say to use the sort of implementation that works in Windows 2008 R2.  Here is my code:</p>
  
  <pre><code>        public bool runPowerShell(string command, string args)
      {
          mLogger myLogger = mLogger.instance;  //How I log stuff in my application.
          bool done = false; //default Return value.
          const string path = @"C:\\XMPLogs\\Roles and Features"; //Where Powershell output will go.
  
          //Make sure Powershell log directory is there.
          if (!Directory.Exists(path))
              Directory.CreateDirectory(path);
  
          //Start a new Powershell instance.
          PowerShell powershell = PowerShell.Create();
          System.Collections.ObjectModel.Collection&lt;PSObject&gt; output = new System.Collections.ObjectModel.Collection&lt;PSObject&gt;();
          StringBuilder strBuilder = new StringBuilder(); //Used to examine results (for testing)
          powershell.AddScript(@"Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted");
          powershell.AddScript(@"Import-Module ServerManager");
          //powershell.Invoke();
  
          powershell.AddScript(command + " " + args);
          try
          {
  
              output = powershell.Invoke();
  
              // Construct a StringBuilder to examine the output of Invoke()
              foreach (PSObject obj in output)
                  strBuilder.AppendLine(obj.ToString());
  
              // Show the StringBuilder to see results (always empty!)
              MessageBox.Show(strBuilder.ToString());
              done = true;
          }
          catch (Exception ex)
          {
              string test = ex.ToString();
              MessageBox.Show(test);
              myLogger.output("ERRO", "PowerShell command " + command
                  + "failed to run with arguments \"" + args + "\".  Message:  " + ex.ToString());
              done = false;
          }
  
          powershell.Dispose();
          return done;
      }
  </code></pre>
  
  <p>I would call the method like this:</p>
  
  <pre><code>runPowerShell("add-windowsfeature", "-name FS-FileServer -logpath \"c:\\XMPLogs\\Roles and Features\\File Services.log\"");
  </code></pre>
  
  <p>The "output" object never has any data in it nor does the log file.  So, I have no idea what is going on.  I do know if I take the two parameters in the method call and enter them into a Powershell prompt manually, the install runs flawlessly.</p>
  
  <p>Can anyone see what I'm doing wrong with this implementation on Windows Server 2012?</p>
  
  <p>Thank you.</p>