使用powershell监控命令行console程序并在停止时启动

有一种C#命令行console程序,为了能看到console台的输出所以不能做成服务。为了防止这些程序自己死掉,使用powershell监控程序并重启

#利用程序名来进行重启
if (!(get-process | where-object {$_.mainwindowtitle -eq '管理员:  "tsiphone"'}))
{ cd C:lengshan
  C:lengshan siphone.bat }
  
  

#如果是在命令行参数里面执行的话,那就应该适应 gwmic 来获得comandline的内容来进行区分了
#因为正常情况下,get-process的操作是没有comandline这个属性的


if (!(Get-WmiObject win32_process | where {$_.CommandLine -eq "`"C:phpphp.exe`"  `"C:Program Files (x86)Apache Software FoundationApache2.2htdocsdeamonigetui_deamon.php`""}))


{ cmd /c start "" "C:lengshan sgetui.bat"  }
  
#将命令行做成bat,之后利用powershell判断进程是否存在,不存在的话,先结束对应的cmd进程,之后再利用start开启新的cmd窗口来启动php进程
if (!(Get-WmiObject win32_process | where {$_.CommandLine -eq "`"c:phpphp.exe`"  `"C:Program Files (x86)Apache Software FoundationApache2.2htdocsdeamonapns_deamon.php`""}))
{ get-process cmd |where {$_.MainWindowTitle -eq "管理员:  `"tsiphone`""} | stop-process
  cmd /c start "" "C:lengshan siphone.bat"}

原文地址:https://www.cnblogs.com/firtree/p/3894608.html