我有这段代码运行powershell脚本,如果我的服务正在启动或停止。ServiceController状态不能正确反映实际的服务状态
Timer timer1 = new Timer();
ServiceController sc = new ServiceController("MyService");
protected override void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer1.Interval = 10000;
timer1.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
if ((sc.Status == ServiceControllerStatus.StartPending) || (sc.Status == ServiceControllerStatus.Stopped))
{
StartPs();
}
}
private void StartPs()
{
PSCommand cmd = new PSCommand();
cmd.AddScript(@"C:\windows\security\dard\StSvc.ps1");
PowerShell posh = PowerShell.Create();
posh.Commands = cmd;
posh.Invoke();
}
它的正常工作,当我杀了从命令提示符 但我的服务,即使我的服务启动并运行,PowerShell脚本继续执行本身(它附加在计算机上的文件) 任何想法,为什么?
要说PowerShell与这个问题是正交的,真正的问题是:为什么我的'StartPending' /'Stopped'检查不能正常工作? –
你有没有试过把断点看看究竟发生了什么? –