【发布时间】:2023-12-21 04:41:02
【问题描述】:
在 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();
【问题讨论】:
标签: c# powershell remote-access