title: How do I get topshelf to return error code when command fails?
body: <p>I am trying to start my service with powershell but currently it fails. I don't know why it fails but that is not the point here. When trying to start the host all I don't get the correct exit code so my automatic deploy fails silently.</p>
  
  <p>What I'm trying to do is:</p>
  
  <pre><code>$cmd = "$folder" + "\MyService.exe"
  try
  {
      &amp; $cmd stop
      &amp; $cmd uninstall
      &amp; $cmd install
      &amp; $cmd start
  }
  catch
  {
      Write-Host "Error: Update of service failed"
      exit 1
  }
  </code></pre>
  
  <p>The <code>start</code> command fails with the following messge:</p>
  
  <pre><code>Topshelf.Hosts.StartHost Error: 0 : The service failed to start., System.InvalidOperationException: Cannot start service MyService on computer '.'. ---&gt; System.ComponentModel.Win32Exception: The service cannot be started, either because it is disabled or because it has no enabled devices associated with it
      --- End of inner exception stack trace ---
      at System.ServiceProcess.ServiceController.Start(String[] args)
      at System.ServiceProcess.ServiceController.Start()
      at Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(String serviceName)
      at Topshelf.Hosts.StartHost.Run()
  </code></pre>
  
  <p>and I never get into the catch statement of my powershell script.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Note that I am asking for how to get the method to the catch statement and not the solution to the actual exception. I have solved the actual exception but I want better feedback in the future if it fails, and that is want the catch statement to be executed which it isn't in case of error.</p>