-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskpart_cmdline.psm1
392 lines (316 loc) · 10.9 KB
/
diskpart_cmdline.psm1
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
function diskpart_cmdline ([string]$para1,[string]$para2,[string]$para3,[string]$para4,[string]$para5){
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$wshell=New-Object -ComObject wscript.shell
$shell=New-Object -ComObject shell.application
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms,System.Drawing
$dpcmdline=$para1
$disktype=$para2
$raidtype=$para3
$assingletter=$para4
$nonlog_flag=$para5
$cSource = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Clicker
{
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public int type; // 0 = INPUT_MOUSE,
// 1 = INPUT_KEYBOARD
// 2 = INPUT_HARDWARE
public MOUSEINPUT mi;
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx ;
public int dy ;
public int mouseData ;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
//This covers most use cases although complex mice may have additional buttons
//There are additional constants you can use for those cases, see the msdn page
const int MOUSEEVENTF_MOVED = 0x0001 ;
const int MOUSEEVENTF_LEFTDOWN = 0x0002 ;
const int MOUSEEVENTF_LEFTUP = 0x0004 ;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008 ;
const int MOUSEEVENTF_RIGHTUP = 0x0010 ;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020 ;
const int MOUSEEVENTF_MIDDLEUP = 0x0040 ;
const int MOUSEEVENTF_WHEEL = 0x0080 ;
const int MOUSEEVENTF_XDOWN = 0x0100 ;
const int MOUSEEVENTF_XUP = 0x0200 ;
const int MOUSEEVENTF_ABSOLUTE = 0x8000 ;
const int screen_length = 0x10000 ;
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
public static void LeftClickAtPoint(int x, int y)
{
//Move the mouse
INPUT[] input = new INPUT[3];
input[0].mi.dx = x*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
input[0].mi.dy = y*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
input[0].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
//Left mouse button down
input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
//Left mouse button up
input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(3, input, Marshal.SizeOf(input[0]));
}
}
'@
Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}
else{
$scriptRoot=$PSScriptRoot
}
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$action=((get-content $tcpath).split(","))[2]
$picpath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\"
if(-not(test-path $picpath)){new-item -ItemType directory -path $picpath |out-null}
$logpath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\step$($tcstep)_diskpart.txt"
new-item $logpath -Force |Out-Null
$results="OK"
$index="check diskpart content"
function diskpcmd($diskpcmds){
$diskpcmds|Out-String|add-content -path $logpath
$diskpcmds|diskpart|add-content -path $logpath
}
#region get working diskid handling
#os disk id
$osdisk=((Get-partition)|Where-object{$_.DriveLetter -eq "C"}).Disknumber
$diskall=(Get-PhysicalDisk).DeviceId|Where-object{$_ -ne $osdisk}
$ssddisks = (Get-PhysicalDisk | Where-Object { $_.MediaType -eq 'SSD' }).DeviceId
#usbdiskid
$useuid=(Get-Disk | Where-Object -FilterScript {$_.Bustype -Eq "USB"}).UniqueId
if($useuid){
$useuid2=$useuid.substring($useuid.length -10,10)
$usbletter=(Get-Partition|Where-object{$_.uniqueid -match $useuid2}).DriveLetter|Where-object{$_}
$usedisk=((Get-partition)|Where-object{$_.DriveLetter -in $usbletter}).Disknumber|Get-Unique
$diskall=(Get-PhysicalDisk).DeviceId|%{
if(!($_ -in $osdisk -or $_ -in $usedisk)){
$_
}
}
}
write-host "All disk is $diskall"
$hddiskall=(Get-PhysicalDisk).DeviceId|%{
if(!($_ -in $osdisk -or $_ -in $usedisk -or $_ -in $ssddisks)){
$_
}
}
if($hddiskall.count -gt 0){write-host "HD disk is $hddiskall"}
$ssddiskall=$ssddisks|%{
if(!($_ -in $osdisk -or $_ -in $usedisk -or $_ -in $hddiskall)){
$_
}
}
if($ssddiskall.count -gt 0){write-host "SSD disk is $ssddiskall"}
if($disktype -match "HD"){
$diskall=$hddiskall
write-host "action disk ($disktype) is $diskall"
}
if($disktype -match "SSD"){
$diskall=$ssddiskall
write-host "action disk ($disktype) is $diskall"
}
if($diskall.count -gt 1){
$disk1=$diskall[0]
$disk2=$diskall[1]
$disk3=$diskall[2]
$disk4=$diskall[3]
$disk5=$diskall[4]
$disk6=$diskall[5]
$disk7=$diskall[6]
$disk8=$diskall[7]
}
if($diskall.count -eq 1){
$disk1=$diskall
}
#endregion
#region get vol # if need later
if($assingletter.Length -gt 0){
$diskmattext="\s"+$assingletter+"\s"
$volinfo="list volume"|diskpart
$volinfo1=$volinfo -match $diskmattext
$volno=(($volinfo1.split(" "))|Where-object{$_.length -gt 0})[1]
write-host "the disk $($assingletter) belongs to volume $($volno)"
}
#endregion
#region define command set
$ini_clngpt=@("select disk #", "clean","convert gpt")
$ini_clndynamic=@("select disk #", "clean","convert dynamic")
$ini_clndynamic=@("select disk #", "clean","convert dynamic")
#$fmtntfs=@("select disk #","create volume simple","select volume","format fs=ntfs quick")
$fmtntfs=@("select disk #","create partition primary","format fs=ntfs quick","assign letter=##")
$fmtntfs_512g=@("select disk #","create partition primary size=512000","format fs=ntfs quick","assign letter=##")
#$size1=([int64]((Get-CimInstance -ClassName Win32_LogicalDisk).Size/1GB)-7)*1024
$pardisk=@("select disk #","clean","convert gpt","list disk",`
"create partition primary size=#size","format fs=ntfs quick","assign letter=D",`
"create partition primary size=2048","format fs=fat quick","assign letter=E",`
"create partition primary size=4096","format fs=fat32 quick","assign letter=F",`
"create partition primary size=1024","format fs=ntfs quick","assign letter=G","list volume")
$add1v_ext23=@("select disk $($disk1)","create volume simple size=170667 disk=$($disk1) ",`
"select volume 3","extend disk $($disk2) size=170667","extend disk $($disk3) size=170667","assign letter=D",`
"format fs=ntfs quick","list volume")
$stripedisk=@("list volume","select volume $($volno)","format fs=ntfs quick",`
"delete volume","list disk","select disk $($disk1)",`
"create volume stripe disk=$($disk1),$($disk2),$($disk3) size= 170667",`
"assign letter=D","format fs=ntfs quick","list volume")
$detdisk=@("list disk","select disk #","det disk")
$diskpcmds=$dpcmdline
if( $dpcmdline -match "ini"){
$diskpcmds=$ini_clngpt
}
if( $dpcmdline -match "dynamic"){
$diskpcmds=$ini_clndynamic
}
if( $dpcmdline -match "ntfs\b"){
$diskpcmds=$fmtntfs
}
if( $dpcmdline -match "det"){
$diskpcmds=$detdisk
}
if( $dpcmdline -match "pardisk"){
$diskpcmds=$pardisk
}
if( $dpcmdline -match "add1v_ext23"){
$diskpcmds=$add1v_ext23
}
if( $dpcmdline -match "stripe"){
$diskpcmds=$stripedisk
}
if( $dpcmdline -match "ntfs512"){
$diskpcmds=$fmtntfs_512g
}
#endregion
#region raid type handling
if($raidtype.length -ne 0){
$raidmappinglog=(Get-ChildItem $picpath -Filter "*raidmapping.txt"|sort lastwritetime|select -Last 1).fullname
if($raidmappinglog){
$raidcontent=get-content $raidmappinglog
foreach($raidline in $raidcontent){
$diskraid=(($raidline.split(","))[3]) -replace "DISK ",""
if($raidline -match "non"){
$disknonraid=$disknonraid+@($diskraid)
}
if($raidline -match "RAID-1\b"){
$diskraid1=$diskraid1+@($diskraid)
}
if($raidline -match "RAID-0"){
$diskraid0=$diskraid0+@($diskraid)
}
if($raidline -match "RAID-5"){
$diskraid5=$diskraid5+@($diskraid)
}
if($raidline -match "RAID-10"){
$diskraidten=$diskraidten+@($diskraid)
}
}
$allraiddisk=$disknonraid+$diskraid1+$diskraid0+$diskraid5+$diskraidten
if($raidtype -match "non" -and $disknonraid.Count -gt 0){
$diskall=$disknonraid
write-host "NonRaid disk is $diskall"
}
if($raidtype -match "RAID" -and $raidtype -match "\-0\b" -and $diskraid0.Count -gt 0){
$diskall=$diskraid0
write-host "raid0 disk is $diskall"
}
if($raidtype -match "RAID" -and $raidtype -match "\-1\b" -and $diskraid1.Count -gt 0){
$diskall=$diskraid1
write-host "raid1 disk is $diskall"
}
if($raidtype -match "RAID" -and $raidtype -match "\-5\b" -and $diskraid5.Count -gt 0){
$diskall=$diskraid5
write-host "raid5 disk is $diskall"
}
if($raidtype -match "RAID" -and $raidtype -match "\-10\b" -and $diskraidten.Count -gt 0){
$diskall=$diskraidten
write-host "raid10 disk is $diskall"
}
<#
if($raidtype -match "non"){
if(($diskall|sort|Out-String) -eq ($allraiddisk|sort|Out-String)){
$diskall=""
$results="NG"
$index="No non-raid disk is found"
}
else{
$diskall=$diskall|Where-object{$_ -notin $allraiddisk}
write-host "non-raid disk is $diskall"
}
}
#>
}
else{
$results="NG"
$index="no raid mapping info"
}
}
#endregion
if($results -ne "NG"){
if($diskall.count -gt 0){
#get list disk#
diskpcmd "list disk"
# diskpart cmd start#
if($dpcmdline.length -gt 0){
if ($dpcmdline -match "add1v_ext23" -or $dpcmdline -match "stripe"){
diskpcmd $diskpcmds
}
else{
foreach($disknu in $diskall){
$diskpcmds_new=$diskpcmds.replace("##",$assingletter)
$size1 = (((Get-Disk -Number $disknu).Size/1GB)-8)*1024
$diskpcmds_new=$diskpcmds_new.replace("#size",$size1)
$diskpcmds_new=$diskpcmds_new.replace("#",$disknu)
diskpcmd $diskpcmds_new
}
}
#get list disk at the end again#
diskpcmd "list disk"
}
if($assingletter.Length -gt 0){
$actionfexp="filexplorer"
Get-Module -name $actionfexp|remove-module
$mdpath=(Get-ChildItem -path $scriptRoot -r -file |Where-object{$_.name -match "^$actionfexp\b" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
$openpath=$assingletter+":\"
&$actionfexp -para1 $openpath -para2 nonlog
}
}
else{
$results="NG"
$index="No matching disk is found"
}
}
Write-Host "$results, $index"
$dpcontents=get-content -path $logpath
if($dpcontents.Length -eq 0){
remove-item $logpath -Force
}
### close file explore windows
$shell.Windows() |Where-object{$_.name -eq "File Explorer"}| ForEach-Object { $_.Quit() }
######### write log #######
if($nonlog_flag.Length -eq 0){
Get-Module -name "outlog"|remove-module
$mdpath=(Get-ChildItem -path "C:\testing_AI\modules\" -r -file |Where-object{$_.name -match "outlog" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
#write-host "Do $action!"
outlog $action $results $tcnumber $tcstep $index
}
}
export-modulemember -Function diskpart_cmdline