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;
}
}
}
}
查看全文
相关阅读:
找到关注点
c中printf必须在所有的变量申明之后才能用?
在eclipse里面运行项目,并未出现中文乱码的问题;但是打成war包运行, tomcat运行startup.bat后控制台中文乱码
《分工与贸易》笔记
《范围:为什么通才能在专业化的世界中取胜》笔记
《不充分均衡》笔记
”苦“没有价值
《为什么佛学是真的》笔记
《强力瞬间》笔记
你和你的渴望
原文地址:https://www.cnblogs.com/pdfw/p/938706.html
最新文章
Racket, SICP stream learning
FireFox 脑残的安全设定
也做了一下腾讯前端面试题
Select prototyping tools
Lisp in Small Parts
下载 infoq 网站视频
时隔3年,再做双倍超立方数的题目,这次用Lisp
Common Lisp 在 Windows 上的开发环境比较
翻译英文技术文章是一件很可耻的事情吗?
TechTalk by Peter Seibel on Common Lisp
热门文章
写了个博客备份的 ruby 程序
SQL Server 事务自动回滚
英文文法学习笔记(16)助动词
小知识:RMAN备份当前控制文件报错ORA245
您好,2022!
如何快速搭建springboot项目?
springboot 文件上传示例(webuploader插件)
springboot 数据库配置信息加密处理
Windows系统下22个整洁的代码编辑器推荐
最难面试的公司以及常见面试问题
Copyright © 2011-2022 走看看