[英]C# run powershell script (remote start/stop service)
In C# I have a script, that remotely stops and start service. 在C#中,我有一个脚本,该脚本可以远程停止并启动服务。 The problem is, only work stop service.
问题是,只有停工服务。
I need start/stop service, because service must be stopped for something else. 我需要启动/停止服务,因为必须停止其他服务。 It may not be ServiceController, because I have disabled WMI.
它可能不是ServiceController,因为我已禁用WMI。
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();
Oh, I found the solution: Only need Add.Statement between commands. 哦,我找到了解决方案:命令之间只需要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");
You could use the ServiceController class like so: 您可以像这样使用ServiceController类:
ServiceController sc = new ServiceController("ArcGIS Server", "192.168.36.22");
sc.Start();
sc.Stop();
credits goes to: How to restart service remotely? 积分转至: 如何远程重启服务?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.