title: SQL Server named instance with Visual Studio 2017 Installer project
body: <p>Software:</p>
  
  <ol>
  <li>SQL Server Express 2016</li>
  <li>Visual Studio 2017 </li>
  </ol>
  
  <p>We've been trying without success to get a named instance of SQL Server Express 2016 installed as part of VS Installer Setup Project.</p>
  
  <p>We've tried calling <code>InstallNamedInstance()</code> as follows with given results:</p>
  
  <ol>
  <li>Run <code>SQLEXPR_x64_ENU.exe</code> with same commandline args from Administrator cmd window: Succeeds</li>
  <li>Call <code>InstallNamedInstance()</code> from a Console app and run Console app from Administrator cmd window: Succeeds</li>
  <li>Install custom action (Both Everyone and Just Me): Fails</li>
  <li>BeforeInstall event (Both Everyone and Just Me): Fails</li>
  </ol>
  
  <p>I notice that current user when msi runs is <code>NT AUTHORITY\SYSTEM</code>. Whenever it fails from Installer project, it fails with below message:</p>
  
  <blockquote>
    <p>The account that is running SQL Server Setup does not have one or all
    of the following rights: the right to back up files and directories,
    the right to manage auditing and the security log and the right to
    debug programs. To continue, use an account with both of these rights.
    For more information, see
    <a href="http://msdn.microsoft.com/en-us/library/ms813696.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms813696.aspx</a>,
    <a href="http://msdn.microsoft.com/en-us/library/ms813959.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms813959.aspx</a> and
    <a href="http://msdn.microsoft.com/en-us/library/ms813847.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms813847.aspx</a>.</p>
  </blockquote>
  
  <p>Is this a limitation of Installer project or am I missing something? Will we have better luck with AdvancedInstaller?</p>
  
  <p>Note that Installer Project's pre-requisite is not working for us because we're having to create a named instance of SQL Server Express and we're not able to see how we can pass commandline arguments to pre-requisite.</p>
  
  <pre><code>private void InstallNamedInstance()
  {
      // NOTE: Change below instance name to get unique instances (or uninstall previous instance)
      var InstanceName = "TFPICDATABASES2";
      var proc = new Process();
      // NOTE:
      //  1. Download "SQLServer2016-SSEI-Expr.exe" web installer from https://www.microsoft.com/en-us/download/details.aspx?id=54284
      //  2. Run the web installer and choose 3rd option "Download Media". This will give "SQLEXPR_x64_ENU.exe"
      proc.StartInfo.FileName = @"c:\temp\sql\SQLEXPR_x64_ENU.exe ";
      proc.StartInfo.Arguments = " /Action=Install";
      proc.StartInfo.Arguments += $" /INSTANCEID={InstanceName}";
      proc.StartInfo.Arguments += $" /InstanceName={InstanceName}";
      proc.StartInfo.Arguments += " /ROLE=AllFeatures_WithDefaults";
      proc.StartInfo.Arguments += " /QS";
      proc.StartInfo.Arguments += " /INDICATEPROGRESS=True";
      proc.StartInfo.Arguments += " /IAcceptSQLServerLicenseTerms=True";
      proc.StartInfo.WorkingDirectory = @"c:\temp\sql";
  
      WriteLog($"FielName: {proc.StartInfo.FileName}; Arguments: {proc.StartInfo.Arguments}; WorkingDir: {proc.StartInfo.WorkingDirectory}");
  
      proc.StartInfo.UseShellExecute = false;
      proc.OutputDataReceived += (s, e) =&gt; WriteLog($"Info: {e.Data}");
      proc.ErrorDataReceived += (s, e) =&gt; WriteLog($"Error: {e.Data}");
  
      var ok = proc.Start();
      // NOTE: Log files are in C:\Program Files\Microsoft SQL Server\130\Setup Bootstrap\Log
      // Summary.txt gives log of latest installer run. It also creates one folder for each installer attempt
      // and gathers more detailed logs in those folders.
      proc.WaitForExit();
      WriteLog($"{proc.StartInfo.FileName} exited with {proc.ExitCode}");
      if (proc.ExitCode != 0)
      {
          throw new Exception($"SQL Server Express installation failed. Check log file for more details");
      }
  }
  </code></pre>