2017-08-24 3 views
0

C#ではリモートでサービスを停止してサービスを開始するスクリプトがあります。 問題は、作業停止サービスだけです。C#でPowerShellスクリプトを実行する(リモート起動/停止サービス)

サービスは何かのために停止する必要があるため、開始/停止サービスが必要です。 WMIが無効になっているため、ServiceControllerではない可能性があります。

InitialSessionState initial = InitialSessionState.CreateDefault(); 
Runspace runspace = RunspaceFactory.CreateRunspace(initial); 
runspace.Open(); 
PowerShell ps = PowerShell.Create(); 
ps.Runspace = runspace; 
ps.AddCommand("Get-Service"); //STOP 
ps.AddParameter("ComputerName", textBox7.Text); 
ps.AddParameter("Name", "spooler*"); 
ps.AddCommand("Stop-Service"); 
ps.AddParameter("force"); 

//ps.AddCommand("Remove-Item"); //QUEUE 

ps.AddCommand("Get-Service"); //START 
ps.AddParameter("ComputerName", textBox7.Text); 
ps.AddParameter("Name", "spooler*"); 
ps.AddCommand("Start-Service"); 

ps.Invoke(); 

答えて

0

あなたはとても似ServiceControllerのクラスを使用することができます。

ServiceController sc = new ServiceController("ArcGIS Server", "192.168.36.22"); 

sc.Start(); 
sc.Stop(); 

クレジットに行く: How to restart service remotely?

+0

引用:「**私はWMIを無効にしているため、ServiceController **でない可能性があります。」 – Fildor

1

ああ、私は解決策を見つけた: のみコマンド間Add.Statementを必要としています。

  ps.AddCommand("Get-Service"); 
      ps.AddParameter("ComputerName", textBox7.Text); 
      ps.AddParameter("Name", "spooler*"); 
      ps.AddCommand("Stop-Service"); 
      ps.AddParameter("force"); 

      ps.AddStatement(); 
      ps.AddCommand("Get-Service"); 
      ps.AddParameter("ComputerName", textBox7.Text); 
      ps.AddParameter("Name", "spooler*"); 
      ps.AddCommand("Start-Service"); 
関連する問題