This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
VHD.js
167 lines (122 loc) · 6.15 KB
/
VHD.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//-----------------------------------------------------------------------
// <copyright>
// Copyright (c) 2010 Garrett Serack. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
var VHD = {
VirtualizationService : GetObject("winmgmts:\\\\.\\root\\virtualization"),
VHDService: GetObject("winmgmts:\\\\.\\root\\virtualization").ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0),
Mount : function(path) {
path = Assert.File(FormatArguments(arguments));
this.VHDService.Mount(path)
},
UnMount : function(path) {
path = Assert.File(FormatArguments(arguments));
this.VHDService.UnMount(path)
},
Compact : function(path ) {
path = Assert.File(FormatArguments(arguments));
var method = this.VHDService.Methods_.Item("CompactVirtualHardDisk");
var inParams = method.InParameters.SpawnInstance_();
inParams.Path = path;
var outParams = this.VHDService.ExecMethod_(method.Name, inParams, null);
if( outParams.ReturnValue > 4096 ) {
Print("Compact Disk Failed: result: {0}", outParams.ReturnValue );
return false;
}
if( outParams.ReturnValue == 4096 )
VHD.WaitForJob(outParams.Job.split('=')[1]);
return true;
},
Fork : function(originalPath, destinationPath) {
originalPath = Assert.File(FormatArguments(originalPath, arguments));
destinationPath = $$.fso.GetAbsolutePathName(FormatArguments(destinationPath, arguments));
var method = this.VHDService.Methods_.Item("CreateDifferencingVirtualHardDisk");
var inParams = method.InParameters.SpawnInstance_();
inParams.ParentPath = originalPath;
inParams.Path= destinationPath;
var outParams = this.VHDService.ExecMethod_(method.Name, inParams, null);
if( outParams.ReturnValue > 4096 ) {
Print("Fork Disk Failed: result: {0}", outParams.ReturnValue );
return false;
}
if( outParams.ReturnValue == 4096 )
VHD.WaitForJob(outParams.Job.split('=')[1]);
return true;
},
Duplicate : function(originalPath, destinationPath) {
originalPath = Assert.File(FormatArguments(originalPath, arguments));
destinationPath = $$.fso.GetAbsolutePathName(FormatArguments(destinationPath, arguments));
var method = this.VHDService.Methods_.Item("ConvertVirtualHardDisk");
var inParams = method.InParameters.SpawnInstance_();
inParams.SourcePath = originalPath;
inParams.DestinationPath = destinationPath;
inParams.Type = 3;
var outParams = this.VHDService.ExecMethod_(method.Name, inParams, null);
if( outParams.ReturnValue > 4096 ) {
Print("Duplicate Disk Failed: result: {0}", outParams.ReturnValue );
return false;
}
if( outParams.ReturnValue == 4096 )
VHD.WaitForJob(outParams.Job.split('=')[1]);
return true;
},
CreateDynamic : function(path, sizeInMegabytes) {
path = Assert.File(FormatArguments(arguments));
var method = this.VHDService.Methods_.Item("CreateDynamicVirtualHardDisk");
var inParams = method.InParameters.SpawnInstance_();
inParams.Path = path;
inParams.MaxInternalSize = sizeInMegabytes * 1024*1024;
var outParams = this.VHDService.ExecMethod_(method.Name, inParams, null);
if( outParams.ReturnValue > 4096 ) {
Print("CreateDynamic Disk Failed: result: {0}", outParams.ReturnValue );
return false;
}
if( outParams.ReturnValue == 4096 )
VHD.WaitForJob(outParams.Job.split('=')[1]);
return true;
},
CreateFixed : function(path, sizeInMegabytes) {
path = Assert.File(FormatArguments(arguments));
var method = this.VHDService.Methods_.Item("CreateFixedVirtualHardDisk");
var inParams = method.InParameters.SpawnInstance_();
inParams.Path = path;
inParams.MaxInternalSize = sizeInMegabytes * 1024*1024;
var outParams = this.VHDService.ExecMethod_(method.Name, inParams, null);
if( outParams.ReturnValue > 4096 ) {
Print("CreateFixed Disk Failed: result: {0}", outParams.ReturnValue );
return false;
}
if( outParams.ReturnValue == 4096 )
VHD.WaitForJob(outParams.Job.split('=')[1]);
return true;
},
Expand : function(path, sizeInMegabytes) {
path = Assert.File(FormatArguments(arguments));
var method = this.VHDService.Methods_.Item("ExpandVirtualHardDisk");
var inParams = method.InParameters.SpawnInstance_();
inParams.Path = path;
inParams.MaxInternalSize = sizeInMegabytes * 1024*1024;
var outParams = this.VHDService.ExecMethod_(method.Name, inParams, null);
if( outParams.ReturnValue > 4096 ) {
Print("Expand Disk Failed: result: {0}", outParams.ReturnValue );
return false;
}
if( outParams.ReturnValue == 4096 )
VHD.WaitForJob(outParams.Job.split('=')[1]);
return true;
VHD.WaitForJob( jobID );
},
WaitForJob: function(jobID) {
var job;
print (jobID)
job = this.VirtualizationService.ExecQuery("SELECT * FROM msvm_storagejob where InstanceID = " + jobID) .ItemIndex(0) ;
while(job.JobState < 5 ) {
job = this.VirtualizationService.ExecQuery("SELECT * FROM msvm_storagejob where InstanceID = " + jobID) .ItemIndex(0);
Print("Processing..." + job.JobState );
$$.WScript.Sleep(500);
}
Print("Completed.");
}
};
VHD;