zoukankan      html  css  js  c++  java
  • 远程重启服务器的程序

    今天通过远程桌面重启服务器失败了,远程桌面就登录不进去了,不过还是能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;
                }

            }

        }

    }
  • 相关阅读:
    The 4 Most Important Skills for a Software Developer
    Youth is not a time of life, it is a state of mind——青春不是一段年华,而是一种心境
    英雄所见略同——每个人都有的一套价值体系观念
    28法则————10分钟休息胜过半小时努力
    离职员工心声
    员工必备素质、能力——职场精英
    安卓sqlite数据库的使用
    安卓adb命令的使用
    windows使用命令行,提高效率
    命令行编译java文件(含第三方jar包)
  • 原文地址:https://www.cnblogs.com/pdfw/p/938706.html
Copyright © 2011-2022 走看看