powershell - How do I get topshelf to return error code when command fails? -
i trying start service powershell fails. don't know why fails not point here. when trying start host don't correct exit code automatic deploy fails silently.
what i'm trying is:
$cmd = "$folder" + "\myservice.exe" try { & $cmd stop & $cmd uninstall & $cmd install & $cmd start } catch { write-host "error: update of service failed" exit 1 }
the start
command fails following messge:
topshelf.hosts.starthost error: 0 : service failed start., system.invalidoperationexception: cannot start service myservice on computer '.'. ---> system.componentmodel.win32exception: service cannot started, either because disabled or because has no enabled devices associated --- end of inner exception stack trace --- @ system.serviceprocess.servicecontroller.start(string[] args) @ system.serviceprocess.servicecontroller.start() @ topshelf.runtime.windows.windowshostenvironment.startservice(string servicename) @ topshelf.hosts.starthost.run()
and never catch statement of powershell script.
update:
note asking how method catch statement , not solution actual exception. have solved actual exception want better feedback in future if fails, , want catch statement executed isn't in case of error.
try/catch
in powershell doesn't work exe. after myservice.exe
calls need check automatic variable $lastexitcode
. try this:
$out = & $cmd start if ($lastexitcode -ne 0) # if exe returns 0 on success, if not change condition accordingly { "error: $out" return # exit script or else. }
Comments
Post a Comment