-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_partitions.psm1
261 lines (190 loc) · 7.54 KB
/
add_partitions.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
function add_partitions ([string]$para1,[string]$para2, [string]$para3,[string]$para4,[string]$para5) {
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$wshell=New-Object -ComObject wscript.shell
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
function Set-WindowState {
<#
.LINK
https://gist.github.com/Nora-Ballard/11240204
#>
[CmdletBinding(DefaultParameterSetName = 'InputObject')]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[Object[]] $InputObject,
[Parameter(Position = 1)]
[ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE',
'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED',
'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
[string] $State = 'SHOW'
)
Begin {
$WindowStates = @{
'FORCEMINIMIZE' = 11
'HIDE' = 0
'MAXIMIZE' = 3
'MINIMIZE' = 6
'RESTORE' = 9
'SHOW' = 5
'SHOWDEFAULT' = 10
'SHOWMAXIMIZED' = 3
'SHOWMINIMIZED' = 2
'SHOWMINNOACTIVE' = 7
'SHOWNA' = 8
'SHOWNOACTIVATE' = 4
'SHOWNORMAL' = 1
}
$Win32ShowWindowAsync = Add-Type -MemberDefinition @'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
if (!$global:MainWindowHandles) {
$global:MainWindowHandles = @{ }
}
}
Process {
foreach ($process in $InputObject) {
if ($process.MainWindowHandle -eq 0) {
if ($global:MainWindowHandles.ContainsKey($process.Id)) {
$handle = $global:MainWindowHandles[$process.Id]
} else {
Write-Error "Main Window handle is '0'"
continue
}
} else {
$handle = $process.MainWindowHandle
$global:MainWindowHandles[$process.Id] = $handle
}
$Win32ShowWindowAsync::ShowWindowAsync($handle, $WindowStates[$State]) | Out-Null
Write-Verbose ("Set Window State '{1} on '{0}'" -f $MainWindowHandle, $State)
}
}
}
$paracheck1=$PSBoundParameters.ContainsKey('para1')
$paracheck2=$PSBoundParameters.ContainsKey('para2')
$paracheck2=$PSBoundParameters.ContainsKey('para3')
$paracheck2=$PSBoundParameters.ContainsKey('para4')
$paracheck2=$PSBoundParameters.ContainsKey('para5')
if($paracheck1 -eq $false -or $para1.Length -eq 0){
$fromdiskid=((Get-Disk)|Where-object{$_.BootFromDisk -eq $true}).Number
$para1 = ((Get-partition)|Where-object{$_.DiskId -eq ((Get-Disk)|Where-object{$_.BootFromDisk -eq $true}).path}|Where-object{$_.type -eq "Basic"}).DriveLetter
}
else{
$fromdiskid=((get-disk)|Where-object{$_.path -eq (((Get-partition)|Where-object{$_.DriveLetter -eq $para1}).DiskId)}).Number
}
if($paracheck2 -eq $false -or $para2.Length -eq 0){
$para2="FAT32"
}
if($paracheck3 -eq $false -or $para3.Length -eq 0){
$para3="Z"
}
if($paracheck4 -eq $false -or $para4.Length -eq 0){
$para4=[uint64]1GB
}
if($paracheck5 -eq $false -or $para5.Length -eq 0){
$para5="NewData"
}
$fromdisklt=$para1
$formattype=$para2
$dletter=$para3
$size=($para4 / 1GB) * 1GB
$label=$para5
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules\"
}
else{
$scriptRoot=$PSScriptRoot
}
$actionss="screenshot"
Get-Module -name $actionss|remove-module
$mdpath=(Get-ChildItem -path $scriptRoot -r -file |Where-object{$_.name -match "^$actionss\b" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
$action="add new partition_disk-$($dletter)_format-$($formattype)_ size-$($para4)_label_$($label)"
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$picpath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\"
if(-not(test-path $picpath)){new-item -ItemType directory -path $picpath |out-null}
if( -not ((Get-Partition|Where-object{$_.DriveLetter -eq $dletter}).DriveLetter -eq $dletter )){
#1. Get-Disk
# Get-Disk
#2. Choose a Disk and Clear Data Using Clear-Disk
# clear-disk -number x -removedata
#3. Create a New Partition, Format the Volume, and Add a Drive Letter
# new-partition -disknumber X -usemaximumsize | format-volume -filesystem NTFS -newfilesystemlabel newdrive
# get-partition -disknumber X | set-partition -newdriveletter X
# Creating Multiple Partitions or Partitions of Different Sizes
# new-partition -disknumberX -size XXgb - driveletter X | format-volume -filesystem NTFS -new filesystemlabel newdrive1
# new-partition -disknumberX -size $MaxSize - driveletter Y | format-volume -filesystem NTFS -new filesystemlabel newdrive2
# New-Partition -DiskNumber 0 -Size 100GB -MbrType IFS -IsActive -DriveLetter Z
# Get-Disk | Where-Object PartitionStyle -Eq "RAW" | Initialize-Disk -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume
$resizeC= (Get-Volume -Drive $fromdisklt).Size - $size
#$sysd=((Get-Disk)|Where-object{$_.BootFromDisk -eq $true}).Number
get-partition -DriveLetter $fromdisklt| resize-partition -size $resizeC
new-partition -disknumber $fromdiskid -Size $size -driveletter $dletter | format-volume -FileSystem $formattype -newfilesystemlabel $label -Full -Confirm:$false -Force
start-sleep -s 10
[Microsoft.VisualBasic.interaction]::AppActivate("Microsoft")|out-null
start-sleep -s 3
$wshell.SendKeys("{tab}")
start-sleep -s 3
$wshell.SendKeys("~")
[Microsoft.VisualBasic.interaction]::AppActivate("Location")|out-null
start-sleep -s 2
$shell = New-Object -ComObject Shell.Application
foreach ($window in $shell.windows()){$window.quit()}
start-sleep -s 2
$wshell.SendKeys("~")
start-sleep -s 3
start-process explorer file:\\ -WindowStyle Maximized
start-sleep -s 5
## screenshot###
&$actionss -para3 nonlog -para5 "check1"
$picfile1=(Get-ChildItem $picpath |Where-object{$_.name -match ".jpg" -and $_.name -match "check1" }).FullName
$shell = New-Object -ComObject Shell.Application
foreach ($window in $shell.windows()){$window.quit()}
## screenshot disk management###
diskmgmt.msc
start-sleep -s 5
[Microsoft.VisualBasic.interaction]::AppActivate("Disk Management")|out-null
start-sleep -s 2
$wshell.SendKeys("% ")
start-sleep -s 2
$wshell.SendKeys("x")
start-sleep -s 2
&$actionss -para3 nonlog -para5 "diskmgmt"
$picfile2=(Get-ChildItem $picpath |Where-object{$_.name -match ".jpg" -and $_.name -match "diskmgmt" }).FullName
start-sleep -s 2
[Microsoft.VisualBasic.interaction]::AppActivate("Disk Management")|out-null
start-sleep -s 2
$wshell.SendKeys("% ")
start-sleep -s 2
$wshell.SendKeys("c")
start-sleep -s 2
####
$index="check screenshots"
if( (Get-Partition|Where-object{$_.DriveLetter -eq $dletter}).DriveLetter -eq $dletter){
$results= "OK"
}
else{
$results= "NG"
}
}
else{
$results= "NG"
$index= "disk $dletter already exists"
}
######### write log #######
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}
else{
$scriptRoot=$PSScriptRoot
}
$action="add partitions"
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 add_partitions