前言:有时候远程服务器的进程你想偷偷去围观一下有哪些,或者对一些比较调皮的进程进行封杀,或者对一些自己研发的服务进行远程手动启动或者重启等,又不想打开远程桌面,只想悄咪咪地执行,那也许下面的文章会对你有启发。 前提条件
- 确保远程服务器(服务端)已启用WinRM。在远程服务器上运行以下命令可以启用和配置WinRM:
- 确保执行远程命令的客户端和服务器之间的防火墙规则允许WinRM通信(默认使用端口5985和5986)。
- 本地客户端网络,需要设置为专用网络
1 public static void ExecuteRemoteCommand(string remoteComputer, string userName, string password, string command) 2 { 3 WSManConnectionInfo connectionInfo = new WSManConnectionInfo(); 4 connectionInfo.ComputerName = remoteComputer; 5 connectionInfo.Credential = new PSCredential(userName, ConvertToSecureString(password)); 6 7 using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) 8 { 9 runspace.Open(); 10 11 using (PowerShell ps = PowerShell.Create()) 12 { 13 ps.Runspace = runspace; 14 ps.AddScript(command); 15 16 Collection<PSObject> results = ps.Invoke(); 17 18 foreach (var result in results) 19 { 20 Console.WriteLine(result); 21 } 22 } 23 runspace.Close(); 24 } 25 }
command参数可选: Get-Process:获取进程列表 Stop-Process:关闭进程 Start-Process:启动进程 传入用户名密码和IP,并提供获取进程列表命令。 打开远程桌面,并启动程序,设置断点,可以看到获取到的进程列表。 远程通过进程ID关闭指定名称的进程
public static void StopRemoteProcessByName(string remoteComputer, string userName, string password, string processName) { WSManConnectionInfo connectionInfo = new WSManConnectionInfo(); connectionInfo.ComputerName = remoteComputer; connectionInfo.Credential = new PSCredential(userName, ConvertToSecureString(password)); using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) { runspace.Open(); using (PowerShell ps = PowerShell.Create()) { // 获取进程ID ps.Runspace = runspace; ps.AddScript($"$proc = Get-Process -Name {processName}; if ($proc) {{$proc.Id}}"); // 执行并获取进程ID var results = ps.Invoke(); if (ps.HadErrors) { Console.WriteLine("Error retrieving process ID"); return; } // 如果找到了进程,关闭它 if (results.Count > 0) { int processId = (int)results[0].BaseObject; ps.Commands.Clear(); ps.AddScript($"Stop-Process -Id {processId} -Force"); ps.Invoke(); if (ps.HadErrors) { Console.WriteLine("Error stopping process"); } else { Console.WriteLine($"Process {processName} with ID {processId} stopped successfully."); } } else { Console.WriteLine("No such process found."); } } runspace.Close(); } }
远程服务器打开一个记事本,查看记事本进程名称和id分别是notepad 和 2724 传入参数,执行程序 获取到notepad进程ID为2724,并且成功被关闭 远程启动进程,通过WinRM启动的进程通常运行在一个服务会话中,而不是在启动它的用户的会话中。因此,这限制了图形用户界面(GUI)程序的显示。但是针对于远程启动一些后台服务,确是可以的。但是具有GUI交互的程序,却没办法打开GUI界面,会在一小段时间以后自动销毁。
public static void StartRemoteProcess(string remoteComputer, string userName, string password, string processName) { WSManConnectionInfo connectionInfo = new WSManConnectionInfo(); connectionInfo.ComputerName = remoteComputer; connectionInfo.Credential = new PSCredential(userName, ConvertToSecureString(password)); using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) { runspace.Open(); using (PowerShell ps = PowerShell.Create()) { ps.Runspace = runspace; ps.AddScript($"$process = Start-Process {processName} -PassThru; $process.Id"); var results = ps.Invoke(); if (ps.HadErrors) { Console.WriteLine("Error starting process"); } else if (results.Count > 0) { Console.WriteLine($"Process started successfully. Process ID: {results[0]}"); } } runspace.Close(); } }
如果以上代码还不能满足你的好奇心,想要我本地测试的源码demo,可以在我的公众号【Dotnet Dancer】后台回复:【进程操控】 即可获取我的本地demo源码自行调试和把玩。
【备注】最近园子里面图片容易挂,如果挂掉了,可以查阅我发表其他地方的文章:https://mp.weixin.qq.com/s/-r9QQkvtBrTrReL5AQDNGQ
标签:ps,connectionInfo,string,C#,WinRM,runspace,进程,远程,奇技淫巧 From: https://www.cnblogs.com/weskynet/p/18213135