-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriveOptimizer.ps1
145 lines (127 loc) · 6.31 KB
/
DriveOptimizer.ps1
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
###############################################################################################################################################
###############################################################################################################################################
#### ------------------------------------------------------------------------------------------------------------------------------------- ####
#### | | ####
#### | | This script optimizes your SSD-tiered drives by drive letter. Since drive | ####
#### | Drive Optimizer | Written by Connor Kennedy | drive optimization has to be run with Administrator privileges, the script | ####
#### | | will self-elevate if it was not run with Administrator permissions. | ####
#### | | ####
#### ------------------------------------------------------------------------------------------------------------------------------------- ####
#### ------------------------------------------------------------------------------------------------------------------------------------- ####
#### ####
#### CONFIGURATION OF DEFAULT DRIVES ####
#### ------------------------------- ####
#### ####
<##> $defaultDrives = @("K") ####
#### ####
#### ------------------------------------------------------------------------------------------------------------------------------------- ####
###############################################################################################################################################
###############################################################################################################################################
# Initializations
$version = 4.3
$defDrives = ""
$numDrives = 0
$driveNum = 1
$arrayString = ""
$underline = ""
$scriptName = "SSD Tiered Drive Optimizer v$version"
## Self-Elevation of script (if script was not run as Administrator, start PowerShell again as Administrator and run script)
# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole)){ # We are running "as Administrator"
clear-host
}
else{ # We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell"
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition
# Indicate that the process should be elevated
$newProcess.Verb = "runas"
# Start the new process
[System.Diagnostics.Process]::Start($newProcess)
# Exit from the current, unelevated, process
exit
}
# Initialize window
$Host.UI.RawUI.WindowTitle = $scriptName + " (Administrator)"
""
""
$scriptName.Remove($scriptName.LastIndexOf(" ")).ToUpper() + $scriptName.Substring($scriptName.LastIndexOf(" "))
for ($i = 0; $i -lt $scriptName.Length; $i++){
$underline += "-"
}
$underline
""
### Optimization of drives
## Initialize program
# Build string of default drives for use when asking user if they want to use default drives
foreach($i in $defaultDrives){
If($defDrives -eq ""){
$defDrives = $i
}
Else{
$defDrives = $defDrives + "," + $i
}
}
## Run program
# Loop until negative ('N',"NO','0') or affirmative ('Y','YES','1') is received from user
while(1){
$userOption = Read-Host "Use default drive(s) [$defDrives]? "
If($userOption -eq "Y" -or $userOption -eq "YES" -or $userOption -eq 1){ # Affirmative input options (sends $option = 1)
$option = 1
break
}
ElseIf($userOption -eq "N" -or $userOption -eq "NO" -or $userOption -eq 0){ # Negative input options (sends $option = 0)
$option = 0
break
}
ElseIf($userOption -eq ""){}
Else{
""
"INVALID INPUT!"
""
}
}
# If user's answer is affirmative [$option = 1]
If($option -eq "1"){
foreach($i in $defaultDrives){
If($arrayString -eq ""){
$arrayString = $i
}
Else{
$arrayString = $arrayString + "," + $i
}
}
$numDrives = $defaultDrives.Count
$driveLets = $arrayString
}
# If user's option is not affirmative (which means it has to be negative) [$option = 0]
Else{
$userDriveLets = ""
# Loop until user enters drive letters
while($userDriveLets -eq ""){
$userDriveLets = Read-Host "└-> Letters of drives to optimize"
}
$userDriveLets.Split(",") | ForEach {
$numDrives++
}
$driveLets = $userDriveLets
}
# Perform drive optimization
$driveLets.Split(",") | ForEach {
$letter = $_.ToUpper()
""
[String]::Format("Optimizing Drive {0}: ... [{1}/{2}]", $letter, $driveNum, $numDrives)
Optimize-Volume -DriveLetter $letter -TierOptimize
[String]::Format("Drive {0}: Optimized", $letter)
$driveNum++
}
""
"DRIVE OPTIMIZATION COMPLETE!"
"Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")