• 远程重启服务器的程序


    今天通过远程桌面重启服务器失败了,远程桌面就登录不进去了,不过还是能ping通的,为了能接着工作,就写了下面的代码去重启服务器。使用System.Mangement命名空间需要引用System.Mangement。
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Management;

    namespace Test
    {
        
    public partial class RebootServer : Form
        
    {
            
    public RebootServer()
            
    {
                InitializeComponent();
            }


            
    /// <summary>
            
    /// 获取服务器信息
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>

            private void button2_Click(object sender, EventArgs e)
            
    {
                
    string strComputerName = string.Empty;
                StringBuilder strTemp 
    = new StringBuilder();
                ConnectionOptions options 
    = new ConnectionOptions();

                
    if (textBox2.Text != "Computer Name or IP" && textBox2.Text.Trim() != string.Empty)
                
    {
                    strComputerName 
    = textBox2.Text.Trim();
                }

                
    else
                
    {
                    MessageBox.Show(
    "Please enter computer name.");
                    
    return;
                }


                options.Username 
    = @"ENT\MSI Drone";
                options.Password 
    = "aaa";
                ManagementScope scope 
    = new ManagementScope("\\\\" + strComputerName + "\\root\\cimv2", options);
                
    //用给定管理者用户名和口令连接远程的计算机 
                try
                
    {
                    scope.Connect();

                    
    //Query system for Operating System information
                    ObjectQuery query = new ObjectQuery(
                        
    "SELECT * FROM Win32_OperatingSystem");
                    ManagementObjectSearcher searcher 
    =
                        
    new ManagementObjectSearcher(scope, query);

                    ManagementObjectCollection queryCollection 
    = searcher.Get();
                    textBox1.Text 
    = "";
                    
    foreach (ManagementObject m in queryCollection)
                    
    {
                        strTemp.Append(
    "\r\n======================\r\n");
                        
    // Display the remote computer information
                        strTemp.Append("Computer Name : " + m["csname"].ToString() + "\r\n");
                        strTemp.Append(
    "Windows Directory : " + m["WindowsDirectory"].ToString() + "\r\n");
                        strTemp.Append(
    "Operating System: " + m["Caption"].ToString() + "\r\n");
                        strTemp.Append(
    "Version: " + m["Version"].ToString() + "\r\n");
                        strTemp.Append(
    "Manufacturer : " + m["Manufacturer"].ToString() + "\r\n");
                        strTemp.AppendLine();
                    }


                    textBox1.Text 
    = strTemp.ToString();
                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(
    "Connection unsuccessfully!");
                    textBox1.Text 
    = ex.Message;
                }

            }


            
    /// <summary>
            
    /// 重启服务器
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>

            private void button3_Click(object sender, EventArgs e)
            
    {
                
    string strComputerName = string.Empty;
                StringBuilder strTemp 
    = new StringBuilder();
                ConnectionOptions options 
    = new ConnectionOptions();

                
    if (textBox2.Text != "Computer Name or IP" && textBox2.Text.Trim() != string.Empty)
                
    {
                    strComputerName 
    = textBox2.Text.Trim();
                }

                
    else
                
    {
                    MessageBox.Show(
    "Please enter computer name.");
                    
    return;
                }


                options.Username 
    = @"ENT\MSI Drone";
                options.Password 
    = "aaa";
                ManagementScope scope 
    = new ManagementScope("\\\\" + strComputerName + "\\root\\cimv2", options);
                
    try
                
    {
                    
    //用给定管理者用户名和口令连接远程的计算机 
                    scope.Connect();

                    
    //Query system for Operating System information
                    ObjectQuery query = new ObjectQuery(
                        
    "SELECT * FROM Win32_OperatingSystem");
                    ManagementObjectSearcher searcher 
    =
                        
    new ManagementObjectSearcher(scope, query);

                    ManagementObjectCollection queryCollection 
    = searcher.Get();
                    textBox1.Text 
    = "";
                    
    foreach (ManagementObject mo in queryCollection)
                    
    {
                        
    string[] ss = "" };
                        
    //重启远程计算机 
                        mo.InvokeMethod("Reboot", ss);
                    }


                    textBox1.Text 
    = "Reboot successfully!";
                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(
    "Connection unsuccessfully!");
                    textBox1.Text 
    = ex.Message;
                }

            }

        }

    }
  • 相关阅读:
    合并两个ICON
    测试用例模板/测试报告模板/测试计划模板
    测试用例的评审和变更
    编写测试用例的基本方法
    禅道的使用
    测试团队对禅道的使用
    需求变动
    禅道的安装
    测试 计划
    微信注册页面密码的测试用例编写
  • 原文地址:https://www.cnblogs.com/pdfw/p/938706.html
Copyright © 2020-2023  润新知