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
最新文章
LINUX核心命令实战总结十二——系统管理命令
LINUX核心命令实战总结十一——网络管理命令
LINUX核心命令实战总结十——进程管理命令
linux服务三——目录与启动过程
linux服务二——软硬链接、文件删除原理、linux中的三种时间
linux服务一——系统优化
LINUX核心命令实战总结九——磁盘与文件系统管理命令
webpack配置文件说明
javascript正则表达式
js 如何实现面向对象
热门文章
javascript 数据去重
KingAOP拦截方案
css记录
人人开源后台管理路由
路由 组件
this.$router
jdk linux
linux rpm
centos yum 配置 iso开机自动挂载
career for mylife
Copyright © 2011-2022 走看看