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;
}
}
}
}
查看全文
相关阅读:
linux系统调用之系统控制
linux系统调用之文件系统操作
使用EF框架实现MVC的增删改查功能
MVC+EF快速弄出一个CRUD
Entity Framework 全面教程详解(转)
微信小程序学习
为Bootstrap模态对话框添加拖拽移动功能
Razor语法大全
EXCEL怎么打20位以上的数字?
C# SQLite 数据库操作学习
原文地址:https://www.cnblogs.com/pdfw/p/938706.html
最新文章
【网易官方】极客战记(codecombat)攻略-地牢-轰轰
【网易官方】极客战记(codecombat)攻略-地牢-Kithmaze 二度历险
【网易官方】极客战记(codecombat)攻略-地牢- 迷一般的 Kithmaze
【网易官方】极客战记(codecombat)攻略-地牢-再下一步
畅谈 | 从“软件”到“服务“——【对象存储】的发展历程(上)
干货 | VPC之间的网络连通实践
运用京东云代码托管、云编译、云部署等产品进行蓝绿部署简单实践
从概念到架构——详细解析态势感知系统
白皮书 | 京东发布区块链技术实践白皮书
干货 | SSMS客户端连接京东云RDS SQL Server配置方法
热门文章
京东云专业安全服务介绍
对话产品 | 3分钟带你了解京东云【云硬盘】
vim 替换
2019-1-17 script(1)
win10开机自启动
进程间通信之信号
linux系统调用之用户管理
linux系统调用之网络管理2
linux系统调用之网络管理1
Linux系统调用之内存管理
Copyright © 2011-2022 走看看