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

C# C运行powershell脚本(远程启动/停止服务),c#,powershell,remote-access,C#,Powershell,Remote Access,在C语言中,我有一个远程停止和启动服务的脚本。 问题是,只有停工服务 我需要启动/停止服务,因为服务必须因其他原因停止。 它可能不是ServiceController,因为我已禁用WMI InitialSessionState initial = InitialSessionState.CreateDefault(); Runspace runspace = RunspaceFactory.CreateRunspace(initial); runspace.Open(); PowerShell

在C语言中,我有一个远程停止和启动服务的脚本。 问题是,只有停工服务

我需要启动/停止服务,因为服务必须因其他原因停止。 它可能不是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();

您可以像这样使用ServiceController类:

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

sc.Start();
sc.Stop();
学分分配给: 哦,我找到了解决办法: 只需要在命令之间添加.Add语句

            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");
引用问题:它可能不是ServiceController,因为我已禁用WMI。