vmopt 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/devcon +0 -0
- data/bin/vmdrive +97 -0
- data/bin/vmdvd +193 -0
- data/bin/vmnetwork +172 -0
- data/bin/vmnotepad +144 -0
- data/bin/vmopt +22 -0
- data/bin/vmpower +73 -0
- data/bin/vmserial +92 -0
- data/bin/vmsysres +58 -0
- data/lib/vmopt.rb +14 -0
- data/lib/vmopt/disk_operation.rb +188 -0
- data/lib/vmopt/dvd_operation.rb +120 -0
- data/lib/vmopt/ext/string_ext.rb +104 -0
- data/lib/vmopt/network.rb +128 -0
- data/lib/vmopt/notepad.rb +145 -0
- data/lib/vmopt/power_operation.rb +57 -0
- data/lib/vmopt/serialport_operation.rb +62 -0
- data/lib/vmopt/system_resource.rb +44 -0
- data/lib/vmopt/utils/ip.rb +287 -0
- data/lib/vmopt/utils/registry.rb +13 -0
- data/lib/vmopt/utils/wmi.rb +19 -0
- data/lib/vmopt/version.rb +3 -0
- data/lib/vmopt/windows/win_net.rb +295 -0
- data/lib/vmopt/windows/win_winutils.rb +200 -0
- data/vmopt.gemspec +37 -0
- metadata +170 -0
data/bin/vmnotepad
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
require "vmopt/notepad"
|
4
|
+
require "optparse"
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
option = {}
|
8
|
+
option_parser = OptionParser.new do | opts |
|
9
|
+
opts.banner = %q/
|
10
|
+
Help:
|
11
|
+
Description:
|
12
|
+
vmnotepad tool, use for notepade management.
|
13
|
+
function:
|
14
|
+
1.use notepad to create a txt file
|
15
|
+
2.query an existed txt file msg
|
16
|
+
3.query an opened notepad.exe msg
|
17
|
+
|
18
|
+
Example:
|
19
|
+
vmnotepad --create --path filepath --msg msg
|
20
|
+
vmnotepad --query --existed --path filepath
|
21
|
+
vmnotepad --query --opened --path filepath
|
22
|
+
vmnotepad --close --titile txt_windows_titile
|
23
|
+
vmnotepad --unpack --path filepath
|
24
|
+
vmnotepad --delete --path filepath
|
25
|
+
/
|
26
|
+
#删除文档
|
27
|
+
opts.on('-d', '--delete', 'delete a txt file') do
|
28
|
+
option[:cmd] = "delete"
|
29
|
+
end
|
30
|
+
|
31
|
+
#创建txt文档
|
32
|
+
opts.on('-c', '--create', 'create a txt file') do
|
33
|
+
option[:cmd] = "create"
|
34
|
+
end
|
35
|
+
#查询txt
|
36
|
+
opts.on('-q', '--query', 'query txt file content or opened notepad window') do
|
37
|
+
option[:cmd] = "query"
|
38
|
+
end
|
39
|
+
#txt文档路径
|
40
|
+
opts.on('-p filepath', '--path filepath', 'specify a txt file with full path') do|value|
|
41
|
+
option[:filepath] = value
|
42
|
+
end
|
43
|
+
#文档信息
|
44
|
+
opts.on('-m msg', '--msg msg', 'fill a txt file with content') do|value|
|
45
|
+
option[:msg] = value
|
46
|
+
end
|
47
|
+
#已经存在但是没有打开的文档
|
48
|
+
option[:existed] = false
|
49
|
+
opts.on('-e', '--existed', 'specify a existed txt file') do
|
50
|
+
option[:existed] = true
|
51
|
+
end
|
52
|
+
#已经打开着的txt文档
|
53
|
+
option[:opened] = false
|
54
|
+
opts.on('-o', '--opened', 'specify a opened txt file') do
|
55
|
+
option[:opened] = true
|
56
|
+
end
|
57
|
+
#关闭打开着的txt文档
|
58
|
+
opts.on('-k', '--close', 'close a opened txt file') do
|
59
|
+
option[:cmd] = "close"
|
60
|
+
end
|
61
|
+
opts.on('-t txt_titile', '--titile txt_titile', 'specify a opened txt window titile') do|value|
|
62
|
+
option[:titile] = value
|
63
|
+
end
|
64
|
+
#关闭打开着的txt文档
|
65
|
+
opts.on('-u', '--unpack', 'open a txt file and hold on') do
|
66
|
+
option[:cmd] = "unpack"
|
67
|
+
end
|
68
|
+
|
69
|
+
end.parse!
|
70
|
+
|
71
|
+
$result_json = {}
|
72
|
+
|
73
|
+
def param_error
|
74
|
+
$result_json = {:status => "-1",:data=>"Please check the params..."}
|
75
|
+
end
|
76
|
+
|
77
|
+
begin
|
78
|
+
case option[:cmd]
|
79
|
+
when "delete"
|
80
|
+
if !option[:filepath].nil?
|
81
|
+
if FileTest::exist?(option[:filepath])
|
82
|
+
File.delete(option[:filepath])
|
83
|
+
$result_json = {:status => "0",:data=>"delete file #{option[:filepath]} ok"}
|
84
|
+
else
|
85
|
+
param_error
|
86
|
+
end
|
87
|
+
else
|
88
|
+
param_error
|
89
|
+
end
|
90
|
+
|
91
|
+
when "create"
|
92
|
+
if !option[:filepath].nil? and !option[:msg].nil?
|
93
|
+
File.delete(option[:filepath]) if FileTest::exist?(option[:filepath])
|
94
|
+
notepad = Vmopt::NotePad.new({:open_window => true})
|
95
|
+
notepad.set_text(option[:msg])
|
96
|
+
notepad.save(option[:filepath])
|
97
|
+
notepad.close
|
98
|
+
$result_json = {:status => "0",:data=>"create file #{option[:filepath].to_utf8} success"}
|
99
|
+
else
|
100
|
+
param_error
|
101
|
+
end
|
102
|
+
|
103
|
+
when "query"
|
104
|
+
if !option[:filepath].nil? and option[:existed]
|
105
|
+
notepad = Vmopt::NotePad.new({:open_window => true, :txt_path => option[:filepath]})
|
106
|
+
msg = notepad.read_text
|
107
|
+
notepad.close
|
108
|
+
$result_json[:status]="0";
|
109
|
+
$result_json[:data]=msg
|
110
|
+
elsif !option[:filepath].nil? and option[:opened]
|
111
|
+
notepad = Vmopt::NotePad.new({:open_window => false, :txt_path => option[:filepath]})
|
112
|
+
msg = notepad.read_text
|
113
|
+
$result_json[:status]="0";
|
114
|
+
$result_json[:data]=msg
|
115
|
+
else
|
116
|
+
param_error
|
117
|
+
end
|
118
|
+
|
119
|
+
when "close"
|
120
|
+
if !option[:titile].nil?
|
121
|
+
notepad = Vmopt::NotePad.new({:open_window => false, :txt_path => option[:titile]})
|
122
|
+
notepad.close
|
123
|
+
$result_json = {:status => "0", :data=>"close txt #{option[:titile].to_utf8} success"}
|
124
|
+
else
|
125
|
+
param_error
|
126
|
+
end
|
127
|
+
|
128
|
+
when "unpack"
|
129
|
+
if !option[:filepath].nil?
|
130
|
+
Vmopt::NotePad.new({:open_window => true, :txt_path => option[:filepath]})
|
131
|
+
$result_json = {:status => "0", :data=>"open txt #{option[:filepath].to_utf8} success"}
|
132
|
+
else
|
133
|
+
param_error
|
134
|
+
end
|
135
|
+
|
136
|
+
else
|
137
|
+
system("vmnotepade -help");
|
138
|
+
end
|
139
|
+
rescue Exception => e
|
140
|
+
$result_json = {:status => "-1", :data=>"Catch exception #{e}"}
|
141
|
+
end
|
142
|
+
|
143
|
+
puts JSON.generate($result_json) unless option[:cmd].nil?
|
144
|
+
|
data/bin/vmopt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
banner = %q/
|
4
|
+
Help:
|
5
|
+
Description:
|
6
|
+
vmopt is a tool box, use for windows system management.
|
7
|
+
|
8
|
+
Tools:
|
9
|
+
1.vmdrive
|
10
|
+
2.vmdvd
|
11
|
+
3.vmnetwork
|
12
|
+
4.vmnotepad
|
13
|
+
5.vmpower
|
14
|
+
6.vmserial
|
15
|
+
7.vmsysres
|
16
|
+
|
17
|
+
Example:
|
18
|
+
vmdrive -help
|
19
|
+
/
|
20
|
+
|
21
|
+
puts banner
|
22
|
+
|
data/bin/vmpower
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
require "vmopt/power_operation"
|
4
|
+
require "optparse"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
option = {}
|
8
|
+
option_parser = OptionParser.new do | opts |
|
9
|
+
opts.banner = %q/
|
10
|
+
Help:
|
11
|
+
Description:
|
12
|
+
vmpower tool, use for system power management.
|
13
|
+
function:
|
14
|
+
1.shutdown
|
15
|
+
2.reboot
|
16
|
+
3.logoff
|
17
|
+
4.lock_user
|
18
|
+
5.sleep
|
19
|
+
|
20
|
+
Example:
|
21
|
+
vmpower --do shutdown
|
22
|
+
vmpower --do reboot
|
23
|
+
vmpower --do logoff
|
24
|
+
vmpower --do lock_user
|
25
|
+
vmpower --do sleep
|
26
|
+
/
|
27
|
+
opts.on('-d popt', '--do popt', 'System power operation') do |value|
|
28
|
+
option[:popt] = value
|
29
|
+
option[:cmd] = "do"
|
30
|
+
end
|
31
|
+
|
32
|
+
end.parse!
|
33
|
+
|
34
|
+
resultok = {"status"=>"0"}
|
35
|
+
resultfail = {"status"=>"-1"}
|
36
|
+
data_value={}
|
37
|
+
$pow = Vmopt::PowerOperation.new()
|
38
|
+
|
39
|
+
if option[:cmd] == "do"
|
40
|
+
if option[:popt] == "shutdown"
|
41
|
+
ret = $pow.shutdown
|
42
|
+
data_value ="system is going to shutdown"
|
43
|
+
elsif option[:popt] == "reboot"
|
44
|
+
ret = $pow.reboot
|
45
|
+
data_value = "system is going to reboot"
|
46
|
+
elsif option[:popt] == "logoff"
|
47
|
+
ret = $pow.logoff
|
48
|
+
data_value = "system is going to logoff"
|
49
|
+
elsif option[:popt] == "lock_user"
|
50
|
+
ret = $pow.lock_user
|
51
|
+
data_value = "system is going to lock_user"
|
52
|
+
elsif option[:popt] == "sleep"
|
53
|
+
ret = $pow.sleep
|
54
|
+
data_value = "system is going to sleep"
|
55
|
+
else
|
56
|
+
system("vmpower -help");
|
57
|
+
option.clear
|
58
|
+
end
|
59
|
+
else
|
60
|
+
system("vmpower -help");
|
61
|
+
option.clear
|
62
|
+
end
|
63
|
+
|
64
|
+
if ret==true
|
65
|
+
resultok["data"]=data_value
|
66
|
+
retjson = JSON.generate resultok
|
67
|
+
else
|
68
|
+
resultfail["data"]="power operation fail"
|
69
|
+
retjson = JSON.generate resultfail
|
70
|
+
end
|
71
|
+
puts retjson unless option[:cmd].nil?
|
72
|
+
|
73
|
+
|
data/bin/vmserial
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
require "vmopt/serialport_operation"
|
4
|
+
require "optparse"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
option = {}
|
8
|
+
option_parser = OptionParser.new do | opts |
|
9
|
+
opts.banner = %q/
|
10
|
+
Help:
|
11
|
+
Description:
|
12
|
+
vmserial tool, use for vm serialport operation,including query info, write data, and read data.
|
13
|
+
function:
|
14
|
+
1.get_serial_port
|
15
|
+
2.write 串口号,写入的字符串
|
16
|
+
3.read 串口号
|
17
|
+
|
18
|
+
Example:
|
19
|
+
vmserial --query serialport
|
20
|
+
vmserial --write --serialnumber com1 --msg helloworld
|
21
|
+
vmserial --read --serialnumber com2
|
22
|
+
/
|
23
|
+
|
24
|
+
opts.on('-q serialport', '--query serialport', 'Query serialport information') do |value|
|
25
|
+
option[:serialport] = value
|
26
|
+
option[:cmd] = "query"
|
27
|
+
end
|
28
|
+
|
29
|
+
opts.on('-w', '--write', 'write serialport information') do |value|
|
30
|
+
option[:cmd] = "write"
|
31
|
+
end
|
32
|
+
|
33
|
+
opts.on('-r', '--read', 'Read serialport information') do |value|
|
34
|
+
option[:cmd] = "read"
|
35
|
+
end
|
36
|
+
|
37
|
+
opts.on('-s com', '--serialnumber com ', 'serialnumber') do |value|
|
38
|
+
option[:serialnumber] = value
|
39
|
+
end
|
40
|
+
|
41
|
+
opts.on('-m msgstr', '--msg msgstr', 'serialport data to trans') do |value|
|
42
|
+
option[:msg] = value
|
43
|
+
end
|
44
|
+
|
45
|
+
end.parse!
|
46
|
+
|
47
|
+
resultok = {"status"=>"0"}
|
48
|
+
resultfail = {"status"=>"-1"}
|
49
|
+
data_value={}
|
50
|
+
$sys = Vmopt::SerialPortOperation.new
|
51
|
+
case option[:cmd]
|
52
|
+
when "query"
|
53
|
+
if option[:serialport] == 'serialport'
|
54
|
+
data_value = $sys.get_serial_port
|
55
|
+
else
|
56
|
+
system("vmserial -help")
|
57
|
+
option.clear
|
58
|
+
end
|
59
|
+
|
60
|
+
when "write"
|
61
|
+
if !option[:serialnumber].nil? and !option[:msg].nil?
|
62
|
+
ret = $sys.write(option[:serialnumber],option[:msg])
|
63
|
+
data_value="write serialport success"
|
64
|
+
else
|
65
|
+
system("vmserial -help")
|
66
|
+
option.clear
|
67
|
+
end
|
68
|
+
|
69
|
+
when "read"
|
70
|
+
unless option[:serialnumber].nil?
|
71
|
+
ret = $sys.read(option[:serialnumber])
|
72
|
+
data_value=ret
|
73
|
+
else
|
74
|
+
system("vmserial -help")
|
75
|
+
option.clear
|
76
|
+
end
|
77
|
+
else
|
78
|
+
system("vmserial -help")
|
79
|
+
option.clear
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
if ret==false
|
84
|
+
resultfail["data"]="serialport operation fail"
|
85
|
+
retjson = JSON.generate resultfail
|
86
|
+
else
|
87
|
+
resultok["data"]=data_value
|
88
|
+
retjson = JSON.generate resultok
|
89
|
+
end
|
90
|
+
|
91
|
+
puts retjson unless option[:cmd].nil?
|
92
|
+
|
data/bin/vmsysres
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
require "vmopt/system_resource"
|
4
|
+
require "optparse"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
option = {}
|
8
|
+
option_parser = OptionParser.new do | opts |
|
9
|
+
opts.banner = %q/
|
10
|
+
Help:
|
11
|
+
Description:
|
12
|
+
vmsysres tool, use for query system resource information, including cpu and memery.
|
13
|
+
function:
|
14
|
+
1.get_cpu
|
15
|
+
2.get_memory
|
16
|
+
|
17
|
+
Example:
|
18
|
+
vmsysres --query cpu
|
19
|
+
vmsysres --query memory
|
20
|
+
|
21
|
+
/
|
22
|
+
opts.on('-q queryopt', '--query queryopt', 'Query system resource information') do |value|
|
23
|
+
option[:queryopt] = value
|
24
|
+
option[:cmd] = "query"
|
25
|
+
end
|
26
|
+
|
27
|
+
end.parse!
|
28
|
+
|
29
|
+
resultok = {"status"=>"0"}
|
30
|
+
resultfail = {"status"=>"-1"}
|
31
|
+
data_value={}
|
32
|
+
ret = true
|
33
|
+
$sys = Vmopt::SystemResource.new
|
34
|
+
if option[:cmd] == "query"
|
35
|
+
if option[:queryopt] == 'cpu'
|
36
|
+
data_value = $sys.get_cpu
|
37
|
+
elsif option[:queryopt] == 'memory'
|
38
|
+
data_value = $sys.get_memory
|
39
|
+
else
|
40
|
+
system("vmsysres -help")
|
41
|
+
option.clear
|
42
|
+
end
|
43
|
+
else
|
44
|
+
system("vmsysres -help")
|
45
|
+
option.clear
|
46
|
+
end
|
47
|
+
|
48
|
+
if ret==false
|
49
|
+
resultfail["data"]="system resource operation fail"
|
50
|
+
retjson = JSON.generate resultfail
|
51
|
+
else
|
52
|
+
resultok["data"]=data_value
|
53
|
+
retjson = JSON.generate resultok
|
54
|
+
end
|
55
|
+
|
56
|
+
puts retjson unless option[:cmd].nil?
|
57
|
+
|
58
|
+
|
data/lib/vmopt.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "win32ole"
|
2
|
+
WIN32OLE.codepage = WIN32OLE::CP_UTF8
|
3
|
+
require "Win32API"
|
4
|
+
|
5
|
+
require "vmopt/notepad"
|
6
|
+
require "vmopt/network"
|
7
|
+
require "vmopt/power_operation"
|
8
|
+
require "vmopt/serialport_operation"
|
9
|
+
require "vmopt/system_resource"
|
10
|
+
require "vmopt/disk_operation"
|
11
|
+
require "vmopt/dvd_operation"
|
12
|
+
|
13
|
+
require "vmopt/version"
|
14
|
+
|
@@ -0,0 +1,188 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require "vmopt/utils/wmi"
|
3
|
+
require "json"
|
4
|
+
module Vmopt
|
5
|
+
class DiskOperation
|
6
|
+
|
7
|
+
=begin
|
8
|
+
参数:无
|
9
|
+
作用:查找物理磁盘的基本信息,并打印出来
|
10
|
+
返回值:默认
|
11
|
+
=end
|
12
|
+
def get_disk_information
|
13
|
+
data_value={}
|
14
|
+
colItems = WMI.execquery ("select * from Win32_DiskDrive")
|
15
|
+
for colItem in colItems do
|
16
|
+
size=colItem.size
|
17
|
+
size=size.to_i/1048576
|
18
|
+
str={ "磁盘ID" => colItem.DeviceID,
|
19
|
+
"磁盘索引" => colItem.Index,
|
20
|
+
"接口类型" => colItem.InterfaceType,
|
21
|
+
"磁盘容量" => size
|
22
|
+
}
|
23
|
+
data_value["diks#{colItem.Index}"] = str
|
24
|
+
end
|
25
|
+
return data_value
|
26
|
+
end
|
27
|
+
=begin
|
28
|
+
参数:无
|
29
|
+
作用:查找磁盘分区的基本信息,并打印出来
|
30
|
+
返回值:默认
|
31
|
+
=end
|
32
|
+
def get_partition_information
|
33
|
+
data_value={}
|
34
|
+
colItems = WMI.execquery ("select * from Win32_LogicalDisk where DriveType=3")
|
35
|
+
for colItem in colItems do
|
36
|
+
size=colItem.size
|
37
|
+
size=size.to_i/1048576
|
38
|
+
freeSpace=colItem.FreeSpace
|
39
|
+
freeSpace=freeSpace.to_i/1048576
|
40
|
+
|
41
|
+
str={"逻辑驱动器号" => colItem.DeviceID,
|
42
|
+
"文件系统" => colItem.FileSystem ,
|
43
|
+
"分区容量" => size,
|
44
|
+
"剩余容量" => freeSpace
|
45
|
+
}
|
46
|
+
|
47
|
+
data_value["partition#{colItem.DeviceID}"] = str
|
48
|
+
end
|
49
|
+
return data_value
|
50
|
+
end
|
51
|
+
|
52
|
+
=begin
|
53
|
+
参数:无
|
54
|
+
功能:判断哪些磁盘没有格式化
|
55
|
+
返回值:没有格式化的磁盘索引号
|
56
|
+
=end
|
57
|
+
def unformat_disk
|
58
|
+
data_value = []
|
59
|
+
colItems = WMI.execquery ("select * from Win32_DiskDrive")
|
60
|
+
for colItem in colItems do
|
61
|
+
if colItem.Partitions==0
|
62
|
+
#str="#{colItem.Index}"
|
63
|
+
#data_value["index#{colItem.Index}"]= str
|
64
|
+
data_value << colItem.Index
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
return {"diskIndex" => data_value}
|
69
|
+
end
|
70
|
+
=begin rdoc
|
71
|
+
参数:无
|
72
|
+
功能:格式化磁盘,供其他方法调用
|
73
|
+
返回值:默认
|
74
|
+
=end
|
75
|
+
def format_disk(volumename)
|
76
|
+
ret1= system("diskpart /s c:/1.txt > null 2>&1 ")
|
77
|
+
ret2 = system("format /FS:NTFS /force /Q #{volumename} >null 2>&1" )
|
78
|
+
return ret1 && ret2
|
79
|
+
end
|
80
|
+
=begin
|
81
|
+
参数:无
|
82
|
+
功能:扫描整个系统磁盘,若存在没有格式化的磁盘则进行格式化
|
83
|
+
返回值:默认
|
84
|
+
=end
|
85
|
+
def chk_format_disk
|
86
|
+
colItemindexs = WMI.execquery ("select * from Win32_DiskDrive")
|
87
|
+
index = []
|
88
|
+
for colItemindex in colItemindexs do
|
89
|
+
if colItemindex.Partitions==0
|
90
|
+
index.push(colItemindex.Index)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
if index.empty?
|
94
|
+
return false
|
95
|
+
end
|
96
|
+
for i in index do
|
97
|
+
colItems = WMI.execquery ("select * from Win32_DiskDrive where index=#{i}")
|
98
|
+
for colItem in colItems do
|
99
|
+
size=colItem.size
|
100
|
+
size=(size.to_i/1048576).to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
logicalcolItems = WMI.execquery ("select * from Win32_LogicalDisk")
|
104
|
+
str=[]
|
105
|
+
for colItem in logicalcolItems do
|
106
|
+
str.push(colItem.DeviceID)
|
107
|
+
volumename=str.sort!.last.next
|
108
|
+
end
|
109
|
+
|
110
|
+
File.open("c:/1.txt","w")do|file|
|
111
|
+
file.puts "select disk=#{i}"
|
112
|
+
file.puts "create partition primary size=#{size}"
|
113
|
+
file.puts "assign letter=#{volumename}"
|
114
|
+
end
|
115
|
+
unless format_disk(volumename)
|
116
|
+
return false
|
117
|
+
end
|
118
|
+
File.delete("c:/1.txt")
|
119
|
+
end
|
120
|
+
return true
|
121
|
+
end
|
122
|
+
=begin
|
123
|
+
参数:磁盘索引号
|
124
|
+
功能:根据指定的磁盘索引号
|
125
|
+
返回值:指定的磁盘已经被格式化返回false,成功格式化返回true
|
126
|
+
=end
|
127
|
+
def format_disk_by_index(index)
|
128
|
+
|
129
|
+
indexEffectives = WMI.execquery("select * from Win32_DiskDrive ")
|
130
|
+
indexarr=[]
|
131
|
+
for indexeff in indexEffectives do
|
132
|
+
indexarr.push(indexeff.index)
|
133
|
+
end
|
134
|
+
if index<0 or index > indexarr.sort!.last
|
135
|
+
return false
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
colItems = WMI.execquery ("select * from Win32_DiskDrive where index=#{index}")
|
140
|
+
for colItem in colItems do
|
141
|
+
if colItem.Partitions==0
|
142
|
+
size=colItem.size
|
143
|
+
size=(size.to_i/1048576).to_s
|
144
|
+
else
|
145
|
+
return false
|
146
|
+
end
|
147
|
+
end
|
148
|
+
logicalcolItems = WMI.execquery ("select * from Win32_LogicalDisk")
|
149
|
+
str=[]
|
150
|
+
for colItem in logicalcolItems do
|
151
|
+
str.push(colItem.DeviceID)
|
152
|
+
volumename=str.sort!.last.next
|
153
|
+
end
|
154
|
+
|
155
|
+
File.open("c:/1.txt","w") do|file|
|
156
|
+
file.puts "select disk=#{index}"
|
157
|
+
file.puts "create partition primary size=#{size}"
|
158
|
+
file.puts "assign letter=#{volumename}"
|
159
|
+
end
|
160
|
+
return false unless format_disk(volumename)
|
161
|
+
#File.open("c:/1.txt","w+")
|
162
|
+
File.delete("c:/1.txt")
|
163
|
+
return true
|
164
|
+
end
|
165
|
+
def delete_partition(index)
|
166
|
+
indexEffectives = WMI.execquery("select * from Win32_DiskDrive ")
|
167
|
+
indexarr=[]
|
168
|
+
for indexeff in indexEffectives do
|
169
|
+
indexarr.push(indexeff.index)
|
170
|
+
end
|
171
|
+
if index<0 or index > indexarr.sort!.last
|
172
|
+
return false
|
173
|
+
end
|
174
|
+
|
175
|
+
File.open("c:/2.txt","w") do|file|
|
176
|
+
file.puts "select disk=#{index}"
|
177
|
+
file.puts "select partition 1"
|
178
|
+
file.puts "delete partition override"
|
179
|
+
end
|
180
|
+
ret = system("diskpart /s c:/2.txt > null 2>&1 ")
|
181
|
+
File.delete("c:/2.txt")
|
182
|
+
if ret == false
|
183
|
+
return false
|
184
|
+
end
|
185
|
+
return true
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end #module vmopt
|