-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.ps1
185 lines (161 loc) · 5.89 KB
/
Config.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
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
#Requires -RunAsAdministrator
param(
[Switch]$Finish
)
#-------------------------------------------------------------------------------------------------#
# Environment Prep :
$Date = Get-Date -Format yyyyMMdd
$LogParam = @{
FilePath = "$PSScriptRoot\$Env:COMPUTERNAME`_$Date.txt"
Append = $true
}
# Generates the logfile at the script root if it doesn't exist.
if(!(Test-Path $LogParam.FilePath)){
$Date = Get-Date -Format 'dd/MM/yyyy hh:MM'
"[ Script start: $Date ]" | Out-File @LogParam
}
# Preparing reboot script.
$Path = "$Env:APPDATA\Microsoft\Windows\Start Menu\Programs\Startup\Restart.bat"
$LinkTest = Test-Path $Path
if(!$LinkTest){
$Command = "net session >nul 2>&1
if %errorLevel% == 0 (
goto Continue
) else (
powershell -command `"Start-Process %~dpnx0 -Verb runas`"
)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoExit -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Auto"
Set-Content -Path $Path -Value $Command
}
# Preparing Ending script.
$Path = "$env:PUBLIC\Desktop\Finish.bat"
$LinkTest = Test-Path $Path
if(!$LinkTest){
$Command = "net session >nul 2>&1
if %errorLevel% == 0 (
goto Continue
) else (
powershell -command `"Start-Process %~dpnx0 -Verb runas`"
)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoExit -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Finish"
Set-Content -Path $Path -Value $Command
}
# Verify if this is the first run.
$Flag = Get-Content -Path $LogParam.FilePath | Where-Object{$_ -match '#UAC_Reboot#'}
if(!$Flag){
# Restart the computer in order to apply the UAC changes.
'#UAC_Reboot#' | Out-File @LogParam
Restart-Computer -Force
}
# Export current power configuration.
$PerformanceGUID = powercfg /LIST | Where-Object{$_ -match "Performance"}
$CurrentGUID = powercfg /GETACTIVESCHEME
if($PerformanceGUID -ne $CurrentGUID){
"#Powercfg:$($CurrentGUID.Split()[3])#"
powercfg /SETACTIVE $($PerformanceGUID.Split()[3])
}
#-------------------------------------------------------------------------------------------------#
# Defining Functions :
function Set-Environment {
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if(!(Get-PackageProvider -Name NuGet)){
Install-PackageProvider -Name NuGet -Force -ErrorAction Stop
}
if(!(Get-Module -Name PSWindowsUpdate -ListAvailable)){
Install-Module -Name PSWindowsUpdate -Force -ErrorAction Stop
}
if((Get-ExecutionPolicy) -eq 'Restricted'){
Set-ExecutionPolicy RemoteSigned -Force -ErrorAction Stop
}
}
catch {
Return $false
}
return $true
}
function Update-Computer {
param(
[Switch]$AutoReboot
)
if(!(Set-Environment)){
throw 'Failed to configure environment.'
}else{
$Continue = $true
$Tries = 1
while($Continue -and $Tries -lt 4){
Write-Host 'Checking updates.'
Get-WindowsUpdate -OutVariable UpdateTest | Out-Host
if($UpdateTest.Status -contains '-------'){
Write-Host 'Installing updates.'
Install-WindowsUpdate -AcceptAll -IgnoreReboot -OutVariable InstallResult | Out-Host
if($AutoReboot -and $InstallResult.ReboorRequired -contains 'True'){
Restart-Computer
}
}else{
$Continue = $false
}
if($InstallResult.Result -contains 'Failed'){
$Tries++
if($Tries -eq 3){
Throw "Failed to installed updates 3 times."
}
}
}
Write-Host 'SuccessFully installed updates.'
}
<#
.SYNOPSIS
Installs Windows updates.
.DESCRIPTION
Downloads and installs available Windows updates.
.PARAMETER AutoReboot
Will restart the computer once the updates installed if one of them require it.
.INPUTS
None. You can't pipe objects to Update-Computer.
.OUTPUTS
Host prompts regarding the state of the update installation.
#>
}
#-------------------------------------------------------------------------------------------------#
# Script :
# Future use facilitation
## Numlock at boot
$RKey = Get-ItemProperty 'registry::HKEY_USERS\.DEFAULT\Control Panel\Keyboard\'
if($RKey.InitialKeyboardIndicators -ne 2147483650){
Set-ItemProperty 'registry::HKEY_USERS\.DEFAULT\Control Panel\Keyboard\' -Name 'InitialKeyboardIndicators' -Value 2147483650
}
# Update installation
$Flag = Get-Content $FilePath | Where-Object{$_ -match '#Windows_Update_Skip#|#Windows_Update_Done#'}
if(!$Flag){
try{
Update-Computer -AutoReboot
}catch{
'Failed to install Windows updates.','#Windows_Update_Skip#' | Out-File @LogParam
}
}
#-------------------------------------------------------------------------------------------------#
# End & Cleanup :
if($Finish){
$Logs = Get-Content -Path $LogParam.FilePath
# Copies the log file back to the original start location.
$OriginFile = 'C:\Setup\ScriptOrigin.txt'
if(Test-Path -Path $OriginFile){
try{
$Origin = Get-Content -Path $OriginFile
# Skips the log copy if the origin is the same device.
if($Origin -notmatch 'C:'){
Copy-Item -Path $LogParam.FilePath -Destination $Origin -ErrorAction Stop
}
}catch{
Write-Host "Failed to copy logs back to : $Origin"
}
}
# Re-enables UAC.
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' -Name 'EnableLUA' -Value 1
# Restores power settings.
$PowerCfg = $Logs | Where-Object{$_ -match '#Powercfg:'}
if($PowerCfg){
powercfg /SETACTIVE $($PowerCfg.Replace('#','').Split(':')[1])
}
}