我有這段代碼運行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'檢查不能正常工作? –
你有沒有試過把斷點看看究竟發生了什麼? –