C#实现远程机器管理

原文:C#实现远程机器管理

目前处于待离职状态,原先所有的工作都在进行交接,过程当中不乏有很多先前整理的和动手尝试实现的功能;我的主页中已经列出来一部分内容,有兴趣的可以前往看一看。

接下来的内容主要介绍另外一个工具,用于对远程主机进行远程控制、进程管理、服务管理以及WMI相关信息显示等;其中仍然存在部分问题还没有得到有效的解决,希望曾经参与过或者有关相关经验的前辈能够指导一下。

一、很搓很搓的主界面

1、配置菜单里面包括远程主机连接配置信息的添加和编辑,界面如下:

2、功能菜单里面包括对远程主机远程控制、远程管理、服务管理和WMI信息查询;

3、添加完成后,功能菜单会进行相应的变化,如下图:

二、主要功能实现部分

远程主机配置信息

    public class RemoteMachine
    {
        /// <summary>
        /// 桌面名称
        /// </summary>
        public string DesktopName { get { return this._DesktopName ?? this.Server; } set { this._DesktopName = value; } }
        private string _DesktopName = null;
        /// <summary>
        /// 远程桌面的IP地址或者域名
        /// </summary>
        public string Server { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// IP
        /// </summary>
        public string Domain { get; set; }
        /// <summary>
        /// 登录密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 允许查询进程
        /// </summary>
        public bool ShowProcess { get { return _ShowProcess; } set { _ShowProcess = value; } }
        private bool _ShowProcess = true;
        /// <summary>
        /// 允许远程
        /// </summary>
        public bool RemoteAble { get { return _RemoteAble; } set { _RemoteAble = value; } }
        private bool _RemoteAble = true;
        /// <summary>
        /// 允许查询服务
        /// </summary>
        public bool ShowService { get { return _ShowService; } set { _ShowService = value; } }
        private bool _ShowService = true;
        /// <summary>
        /// 共享磁盘驱动器
        /// </summary>
        public bool RedirectDrives { get { return _RedirectDrives; } set { _RedirectDrives = value; } }
        private bool _RedirectDrives = true;
        /// <summary>
        /// 共享打印机
        /// </summary>
        public bool RedirectPrinters { get { return _RedirectPrinters; } set { _RedirectPrinters = value; } }
        private bool _RedirectPrinters = false;
        /// <summary>
        /// 共享端口
        /// </summary>
        public bool RedirectPorts { get { return _RedirectPorts; } set { _RedirectPorts = value; } }
        private bool _RedirectPorts = false;
        /// <summary>
        /// 色彩深度
        /// </summary>
        public Colors ColorDepth { get { return _ColorDepth; } set { _ColorDepth = value; } }
        private Colors _ColorDepth = Colors.Color24;

        public override string ToString()
        {
            return string.Format("{0}:{1}", this.DesktopName, this.Server);
        }

        public void Save(IniFile iniFile)
        {
            Save(this, iniFile);
        }

        public void Delete(IniFile iniFile)
        {
            string section = string.Format("Remote({0})", this.DesktopName);
            iniFile.DeleteSection(section);
        }

        public void Load(IniFile iniFile, string desktopName)
        {
            string section = string.Format("Remote({0})", desktopName);
            this.DesktopName = desktopName;
            this.Server = iniFile.ReadString(section, "Server", "");
            this.UserName = iniFile.ReadString(section, "UserName", "");
            this.Domain = iniFile.ReadString(section, "Domain", "");
            this.Password = iniFile.ReadString(section, "Password", "");
            this.RemoteAble = iniFile.ReadInt(section, "RemoteAble", 0) == 1;
            this.ShowProcess = iniFile.ReadInt(section, "ShowProcess", 0) == 1;
            this.ShowService = iniFile.ReadInt(section, "ShowService", 0) == 1;
            this.RedirectDrives = iniFile.ReadInt(section, "RedirectDrives", 0) == 1;
            this.RedirectPrinters = iniFile.ReadInt(section, "RedirectPrinters", 0) == 1;
            this.RedirectPorts = iniFile.ReadInt(section, "RedirectPorts", 0) == 1;
            this.ColorDepth = (Colors)iniFile.ReadInt(section, "ColorDepth", 0);
        }

        public static RemoteMachine Load(string desktopName, IniFile iniFile)
        {
            string section = string.Format("Remote({0})", desktopName);
            RemoteMachine mac = new RemoteMachine();
            mac.DesktopName = desktopName;
            mac.Server = iniFile.ReadString(section, "Server", "");
            mac.UserName = iniFile.ReadString(section, "UserName", "");
            mac.Domain = iniFile.ReadString(section, "Domain", "");
            mac.Password = iniFile.ReadString(section, "Password", "");
            mac.RemoteAble = iniFile.ReadInt(section, "RemoteAble", 0) == 1;
            mac.ShowProcess = iniFile.ReadInt(section, "ShowProcess", 0) == 1;
            mac.ShowService = iniFile.ReadInt(section, "ShowService", 0) == 1;
            mac.RedirectDrives = iniFile.ReadInt(section, "RedirectDrives", 0) == 1;
            mac.RedirectPrinters = iniFile.ReadInt(section, "RedirectPrinters", 0) == 1;
            mac.RedirectPorts = iniFile.ReadInt(section, "RedirectPorts", 0) == 1;
            mac.ColorDepth = (Colors)iniFile.ReadInt(section, "ColorDepth", 0);
            return mac;
        }

        public static void Save(RemoteMachine machine, IniFile iniFile)
        {
            string section = string.Format("Remote({0})", machine.DesktopName);
            iniFile.WriteString(section, "DesktopName", machine.DesktopName);
            iniFile.WriteString(section, "Server", machine.Server);
            iniFile.WriteString(section, "UserName", machine.UserName);
            iniFile.WriteString(section, "Domain", machine.Domain);
            iniFile.WriteString(section, "Password", machine.Password);
            iniFile.WriteInt(section, "RemoteAble", machine.RemoteAble ? 1 : 0);
            iniFile.WriteInt(section, "ShowProcess", machine.ShowProcess ? 1 : 0);
            iniFile.WriteInt(section, "ShowService", machine.ShowService ? 1 : 0);
            iniFile.WriteInt(section, "RedirectDrives", machine.RedirectDrives ? 1 : 0);
            iniFile.WriteInt(section, "RedirectPrinters", machine.RedirectPrinters ? 1 : 0);
            iniFile.WriteInt(section, "RedirectPorts", machine.RedirectPorts ? 1 : 0);
            iniFile.WriteInt(section, "ColorDepth", (int)machine.ColorDepth);
        }

        public static List<RemoteMachine> Load(IniFile iniFile)
        {
            string[] infos = iniFile.ReadAllSectionNames();
            if (infos != null)
            {
                List<RemoteMachine> macs = new List<RemoteMachine>();
                foreach (string info in infos)
                {
                    string section = info.Substring(7, info.Length - 8);
                    RemoteMachine mac = RemoteMachine.Load(section, iniFile);
                    macs.Add(mac);
                }
                return macs;
            }
            return null;
        }
    }

    public enum Colors : int
    {
        Color8 = 8,
        Color16 = 16,
        Color24 = 24,
        Color32 = 32
    }

1、远程控制功能

工具内实现的远程控制功能需要引用AxInterop.MSTSCLib.dll和Interop.MSTSCLib.dll文件,具体代码如下:

    public partial class DesktopForm : Form, IRemote
    {
        private AxMsRdpClient4 rdpc = null;
        public RemoteMachine Machine { get; set; }
        public DesktopForm(RemoteMachine machine)
        {
            InitializeComponent();
            Text = string.Format("【远程】{0}", machine.DesktopName);
            this.Machine = machine;
        }

        private void DesktopForm_Load(object sender, EventArgs e)
        {
            this.rdpc = new AxMsRdpClient4() { Dock = DockStyle.Fill };
            this.rdpc.OnDisconnected += rdpc_OnDisconnected;
            this.Controls.Add(this.rdpc);
            this.SetRdpClientProperties(this.Machine);
            Connect();
        }

        private void rdpc_OnDisconnected(object sender, IMsTscAxEvents_OnDisconnectedEvent e)
        {

        }

        public bool Connect()
        {
            try
            {
                rdpc.Connect();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
        }

        public void Disconnect()
        {
            try
            {
                if (rdpc.Connected == 1)
                    rdpc.Disconnect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void SetRdpClientProperties(RemoteMachine machine)
        {
            rdpc.Server = machine.Server;
            rdpc.UserName = machine.UserName;
            rdpc.Domain = machine.Domain;
            if (!string.IsNullOrEmpty(machine.Password))
                rdpc.AdvancedSettings5.ClearTextPassword = machine.Password;
            rdpc.AdvancedSettings5.RedirectDrives = machine.RedirectDrives;
            rdpc.AdvancedSettings5.RedirectPrinters = machine.RedirectPrinters;
            rdpc.AdvancedSettings5.RedirectPorts = machine.RedirectPorts;
            rdpc.ColorDepth = (int)machine.ColorDepth;
            rdpc.Dock = DockStyle.Fill;
        }
    }

通过使用RemoteMachine参数实例化并进行远程控制;

                    RemoteMachine mac = item.Tag as RemoteMachine;
                    using (DesktopForm df = new DesktopForm(mac))
                    {
                        df.ShowDialog();
                    }

2、进程、服务管理等(WMI方式,需要引人System.Management.dll类库,以进程管理为例,ProcessInfo类包含的属性信息可查询MSDN,其他类似)

    public partial class ProcessForm : Form, IRemote
    {
        private ManagementScope scope = null;
        private ManagementClass managementClass = null;
        private string path = null;
        public RemoteMachine Machine { get; set; }
        public ProcessForm(RemoteMachine machine)
        {
            InitializeComponent();
            Text = string.Format("【进程】{0}", machine.DesktopName);
            this.Machine = machine;
            this.path = "\\\\" + this.Machine.Server + "\\root\\cimv2:Win32_Process";
            this.managementClass = new ManagementClass(this.path);
            ConnectionOptions options = null;
            if (this.Machine.Server != "." && this.Machine.UserName != null && this.Machine.UserName.Length > 0)
            {
                options = new ConnectionOptions();
                options.Username = this.Machine.UserName;
                options.Password = this.Machine.Password;
                //options.EnablePrivileges = true;
                //options.Impersonation = ImpersonationLevel.Impersonate;
                //options.Authority = "ntlmdomain:domain";
            }
            this.scope = new ManagementScope("\\\\" + this.Machine.Server + "\\root\\cimv2", options);
            this.managementClass.Scope = this.scope;
        }

        private DataGridView dataGrid;
        private ContextMenuStrip contextMenu;
        private ProcessInfo[] processes;
        private void ProcessForm_Load(object sender, EventArgs e)
        {
            this.contextMenu = new ContextMenuStrip();
            ToolStripItem item = this.contextMenu.Items.Add("停止");
            item.Click += item_Click;
            this.dataGrid = new DataGridView() { Dock = DockStyle.Fill, ContextMenuStrip = this.contextMenu, SelectionMode = DataGridViewSelectionMode.FullRowSelect, AllowUserToAddRows = false, AllowUserToDeleteRows = false, ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize, ReadOnly = true, RowHeadersWidth = 21 };
            this.Controls.Add(this.dataGrid);
            ReLoad();
        }

        private void ReLoad()
        {
            this.dataGrid.DataSource = null;
            this.processes = null;
            if (Connect())
            {
                processes = GetProcess();
                DataTable table = ProcessInfo.ConvertTo(processes);
                BindData(table);
            }
        }

        private void item_Click(object sender, EventArgs e)
        {
            if (this.dataGrid.SelectedRows.Count == 1)
            {
                try
                {
                    UInt32 processId = Convert.ToUInt32(this.dataGrid.SelectedRows[0].Cells[0].Value);
                    string processName = this.dataGrid.SelectedRows[0].Cells[1].Value.ToString();
                    if (processName != null && MessageBox.Show(string.Format("是否停止\"{0}\"进程?", processName), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        ProcessInfo process = Array.Find(processes, (pro) => { return pro.Name == processName && pro.ProcessId == processId; });
                        KillProcess(process);
                        ReLoad();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                }
            }
        }

        public bool Connect()
        {
            try
            {
                scope.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return scope.IsConnected;
        }

        public void Disconnect()
        {

        }

        public void BindData(DataTable table)
        {
            this.dataGrid.DataSource = table;
        }

        public ProcessInfo[] GetProcess()
        {
            ManagementObjectCollection queryCollection = this.managementClass.GetInstances();
            List<ProcessInfo> processes = new List<ProcessInfo>();
            foreach (ManagementObject m in queryCollection)
            {
                ProcessInfo process = new ProcessInfo(m);
                processes.Add(process);
            }
            return processes.ToArray();
        }

        public void KillProcess(ProcessInfo process)
        {
            ObjectQuery query = new ObjectQuery(string.Format("select * from Win32_Process where ProcessId={0}", process.ProcessId));
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection queryCollection = searcher.Get();
            foreach (ManagementObject m in queryCollection)
            {
                string[] args = new string[] { "0" };
                object obj = m.InvokeMethod("Terminate", args);
                Console.WriteLine(obj.ToString());
                return;
            }
        }
    }

可以通过KillProcess来终止进程;

三、所遇到的问题

(未完待续...)

此处附上源码

时间: 2024-09-30 06:45:06

C#实现远程机器管理的相关文章

远程桌面管理工具比较(转)

一 RemoteDesktopManager (windows到windows的remote的管理)主页:http://sourceforge.net/projects/tscm/特点:开源免费,只能用来管理远程的Windows机器连接.真正连接的时候还是调用mstsc.exe进程.截图: 二RemoteDesktopManager (windows到windows和Linux的remote的管理)下载:http://remotedesktopmanager.com/remotedesktopm

VS编译完成后自动复制到远程机器

最近在调试网络通信,每次一有点小修改,都要将程序从开发机复制到测试机,不胜烦扰.既然我们程序猿,为什么要那么死板呢,能够用代码解决的问题,就不要用手去解决. 先说复制,我们除了手工复制就没有其他办法了吗?在命令提示符下面,我们用于管理文件和文件夹的命令多的是,比如RENAME.DEL.Copy用于文件,MKDIR.RMDIR.XCOPY用于目录.查看命令提示符的常用命令,我们可以通过在命令提示符中输入Help进行查看,如下图: 一般情况,我们都需要将整个目录复制过去,所以我用了XCOPY命令(如

Remote Desktop Organizer远程桌面管理软件的基本使用和介绍

<Remote Desktop Organizer>是一款用于远程桌面管理的软件.软件支持windows平台运行. Remote Desktop Organizer 是一款 Windows 远程桌面管理软件,让你在同一个窗口内浏览到多个远程桌面的信息,方便 Windows 远程管理. 软件信息 名称:Remote Desktop Organizer 版本:1.4.7 下载:http://www.softpedia.com/get/Internet/Remote-Utils/Remote-Des

利用powershell进行远程服务器管理(命令行模式)

Pssession,Pssession是Windows Powershell会话的意思,一个会话,可以共享数据,提供交互式的对话,我们可以为某些命令例如Invoke-Command 制定会话来远程作业.当然我们还能利用Enter-Pssession来直接和远程计算机连接,直接建立一个持续安全的远程对话,来执行我们的所有命令. 我们分三个部分来讲述Powershell Session,首先是域环境下的远程连接,WAN/LAN的远程连接(Trusthost方式),WAN/LAN的远程连接(SSL加密

XAMPP无法让远程机器连接

服务器端装好XAMPP并启动服务后,在机器浏览器上输入localhost可以打开XAMPP管理页面,但是在我的笔记本上输入服务器IP后却无法打开,提示:New XAMPP security concept:Access to the requested object is only available from the local network. This setting can be configured in the file "httpd-xampp.conf". 于是找到XA

2012远程桌面管理工具下载新功能

与升级到Windows 8相比,企业用户使用Windows Server 2012时相对比较简单.Windows Server 2012最大的改变并不是在风格上,除了Modern UI界面之外,而是那些基于微软三年前发布Windows Server 2008 第二版本时打造的组件.值得一提的是,Windows Server 2012保留并极大地扩展了两个Windows Server 2008 R2网管们很熟悉的两个管理功能:Server manager 和Powershell.iis7远程桌面连

windows远程连接工具 远程桌面管理

现在还有很多小伙伴的电脑是win7系统,如果过遇到问题需要解决.或者想远程操作控制系统.可以通过远程桌面开实现.本经验以win7旗舰版为例演示,如何开启设置win7远程桌面. 时代在发展,所以办公模式也在不断的更新.越来越多的人都会用到远程桌面连接工具了,今天就来给大家介绍一下IIS7 远程桌面管理,让大家在工作学习中更加的方便. iis7 远程桌面连接工具,又叫做iis7 远程桌面管理软件,是一款绿色小巧,功能实用的远程桌面管理工具,其界面简洁,操作便捷,能够同时远程操作多台服务器,并且多台服

COVID-19:保护远程用户的远程补丁管理斗争和解决方案

无论危机发生与否,实时保持终端的补丁更新和发现新出现的漏洞并修复仍然是IT管理员面临的最大挑战之一.随着COVID-19的全球肆虐而兴起的新的远程办公热潮,对于许多IT管理员来说,补丁管理听起来几乎是不可能的. 不给终端打补丁可能会对网络安全产生严重影响,即使终端位于受控的公司范围内也是如此.既然您的远程用户依赖于Internet来进行远程工作,那么就必须为机器打补丁. 您是否准备好在遍布全球的远程工作人员中保持完全的可见性? 您用于扫描补丁并将其部署到登录和注销网络的远程用户的策略是什么? 您

Spark学习笔记——在远程机器中运行WordCount

1.通过realy机器登录relay-shell ssh [email protected] 2.登录了跳板机之后,连接可以用的机器 XXXX.bj 3.在本地的idea生成好程序的jar包(word-count_2.11-1.0.jar)之后,把jar包和需要put到远程机器的hdfs文件系统中的文件通过scp命令从开发机传到远程的机器中 scp 开发机用户名@开发机ip地址:/home/XXXXX/文件 . #最后一个.表示cd的根目录下 object WordCount { def mai