简体   繁体   English

C#运行Powershell脚本(远程启动/停止服务)

[英]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.

相关问题 从 C# 运行 PowerShell 6 中的 PowerShell 脚本 - Run PowerShell script in PowerShell 6 from C# 如何使用C#在远程计算机中启动或停止IIS以及Windows服务 - How to start or stop IIS and also a Windows Service in remote machine using C# 从 Websocket svia c# 执行 Powershell 脚本以停止/启动网站 - Executing Powershell script from Websocket svia c# to stop/start website 从C#运行的PowerShell启动/停止IIS应用程序池无效 - PowerShell run from C# to start/stop IIS application pools has no effect 从C#启动远程服务 - Start A Remote Service From C# 在C#中启动和停止Azure分析服务 - Start and stop azure analysis service in C# 通过C#启动/停止Windows服务,并以管理员身份运行,而没有UAC提示窗口 - start / stop windows service via C# with run as admin without UAC prompt window 强制 c# windows 服务始终作为系统运行,并且不允许非特权用户停止/启动 - Force c# windows service to always run as system and not allow stop/start by non-privileged users WCF 服务试图在另一台机器上运行 Powershell 脚本到远程 - WCF service trying to run a Powershell script to Remote in on a different machine 如何在C#中使用System.Diagnostics.Process.Start运行PowerShell脚本? - How to run PowerShell script using System.Diagnostics.Process.Start in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM