-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeploy-WebpagetestAgent.ps1
More file actions
394 lines (365 loc) · 19.5 KB
/
deploy-WebpagetestAgent.ps1
File metadata and controls
394 lines (365 loc) · 19.5 KB
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
393
394
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
# Deployment of Web Page Test
Function Deploy-WebPagetest(){
[CmdletBinding()]
Param(
[String]$DomainName = "localhost",
[String]$Logfile = "C:\Windows\Temp\Deploy-WebPageTest.log",
[String]$wpt_host = $env:COMPUTERNAME,
[String]$wpt_user = "Administrator",
[String]$driver_installer_file = "mindinst.exe",
[String]$driver_installer_cert_file = "WPOFoundation.cer",
[String]$wpt_agent_dir = "C:\webpagetest",
[String]$wpt_temp_dir = "C:\wpt-temp",
[String]$wpt_password = "p@ssword",
[String]$wpt_url = "http://www.webpagetest.com/",
[String]$wpt_location = "wpt_location",
[String]$wpt_key = "wpt_key",
[String]$windows_licenseKey = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"
)
# Create Log File
if (!( Test-Path $Logfile)){
New-Item -Path "C:\Windows\Temp\Deploy-WebPageTest.log" -ItemType file
}
Function Write-Log{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
Write-Output $logstring
}
# External Dependencies
$wpt_zip_url = "https://github.com/WPO-Foundation/webpagetest/releases/download/WebPageTest-17.12/webpagetest_17.12.zip"
$wpt_zip_file = "webpagetest_17.12.zip"
# Github Dependencies
$driver_installer_cert_url = "https://github.com/fasterize/webpagetest-installation/raw/master/files/WPOFoundation.cer"
$driver_installer_url = "https://raw.githubusercontent.com/fasterize/webpagetest-installation/master/files/mindinst.exe"
$wpt_urlBlast_ini = "https://raw.githubusercontent.com/fasterize/webpagetest-installation/master/files/urlBlast.ini"
$wpt_wptdriver_ini = "https://raw.githubusercontent.com/fasterize/webpagetest-installation/master/files/wptdriver.ini"
# Scripts
$DefaultUserNameURL = "https://raw.githubusercontent.com/fasterize/webpagetest-installation/master/files/DefaultUserName.ps1"
$FirstRebootURL = "https://raw.githubusercontent.com/fasterize/webpagetest-installation/master/files/FirstReboot.ps1"
$DnsResolver1 = "8.8.8.8"
$DnsResolver2 = "8.8.4.4"
$lang = "fr-FR"
function Set-WptFolders(){
$wpt_folders = @($wpt_agent_dir,$wpt_temp_dir)
foreach ($wpt_folder in $wpt_folders){
New-Item $wpt_folder -type directory -Force *>> $Logfile
}
}
function Download-File ($url, $localpath, $filename){
if(!(Test-Path -Path $localpath)){
New-Item $localpath -type directory *>> $Logfile
}
Write-Log "[$(Get-Date)] Downloading $filename"
$webclient = New-Object System.Net.WebClient;
$webclient.Headers.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
$webclient.DownloadFile($url, $localpath + "\" + $filename)
}
function Unzip-File($fileName, $sourcePath, $destinationPath){
Write-Log "[$(Get-Date)] Unzipping $filename to $destinationPath"
$shell = new-object -com shell.application
if (!(Test-Path "$sourcePath\$fileName")){
throw "$sourcePath\$fileName does not exist"
}
New-Item -ItemType Directory -Force -Path $destinationPath -WarningAction SilentlyContinue *>> $Logfile
$shell.namespace($destinationPath).copyhere($shell.namespace("$sourcePath\$fileName").items()) *>> $Logfile
}
function Set-WindowsLicense ($LicenseKey) {
Write-Log "[$(Get-Date)] Set Windows License."
DISM /online /Set-Edition:ServerStandard /ProductKey:$LicenseKey /AcceptEula
}
function Activate-Windows-Update () {
Write-Log "[$(Get-Date)] Activate Windows Update."
$AUSettigns = (New-Object -com "Microsoft.Update.AutoUpdate").Settings
$AUSettigns.NotificationLevel = 4
$AUSettigns.Save()
}
function Replace-String ($filePath, $stringToReplace, $replaceWith){
(get-content $filePath) | foreach-object {$_ -replace $stringToReplace, $replaceWith} | set-content $filePath *>> $Logfile
}
function Set-Keyboard ($Lang) {
Write-Log "[$(Get-Date)] Set Keyboard to $lang."
Set-WinUserLanguageList -LanguageList $Lang -Force
}
function ExtendPartition () {
Write-Log "[$(Get-Date)] Extend partition of drive C."
$MaxSize = (Get-PartitionSupportedSize -DriveLetter c).sizeMax
Resize-Partition -DriveLetter c -Size $MaxSize
}
function Disable-MouseShadow () {
Write-Log "[$(Get-Date)] Disable mouse shadow for RPC program"
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects" -Name "VisualFXSetting" -Value 00000003
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects" -Name "CursorShadow" -Value 00000000
Set-ItemProperty "HKCU:\Control Panel\Desktop" -Name 'UserPreferencesMask' -Value ([byte[]](0x9E,0x1E,0x07,0x80,0x12,0x00,0x00,0x00))
}
function Set-DnsResolver ($Resolver1, $Resolver2) {
Write-Log "[$(Get-Date)] Set DNS resolver to $resolver1 and $resolver2"
netsh interface ip add dns name="Ethernet" addr="$Resolver1" index=1
netsh interface ip add dns name="Ethernet" addr="$Resolver2" index=2
}
function Set-WebPageTestUser ($Username, $Password){
$Exists = [ADSI]::Exists("WinNT://./$Username")
if ($Exists) {
Write-Log "[$(Get-Date)] $Username user already exists."
} Else {
net user /add $Username *>> $Logfile
net localgroup Administrators /add $Username *>> $Logfile
Write-Log "[$(Get-Date)] $Username created."
}
$user = [ADSI]("WinNT://./$Username")
$user.SetPassword($Password)
$user.SetInfo()
Write-Log "[$(Get-Date)] Password updated."
}
function Set-AutoLogon ($Username, $Password){
$LogonPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
$CurrentVal = Get-ItemProperty -Path $LogonPath -Name AutoAdminLogon
If ($CurrentVal.AutoAdminLogon -eq 1) {
$CurrentUser = Get-ItemProperty -Path $LogonPath -Name DefaultUserName
$CurrentPass = Get-ItemProperty -Path $LogonPath -Name DefaultPassword
If ($CurrentUser.DefaultUserName -ne $Username -Or $CurrentPass.DefaultPassword -ne $Password) {
Set-ItemProperty -Path $LogonPath -Name DefaultUserName -Value $Username
Set-ItemProperty -Path $LogonPath -Name DefaultPassword -Value $Password
Write-Log "[$(Get-Date)] Credentials Updated."
}Else {
Write-Log "[$(Get-Date)] AutoLogon already enabled."
}
}Else {
Set-ItemProperty -Path $LogonPath -Name AutoAdminLogon -Value 1
New-ItemProperty -Path $LogonPath -Name DefaultUserName -Value $Username
New-ItemProperty -Path $LogonPath -Name DefaultPassword -Value $Password
Write-Log "[$(Get-Date)] AutoLogon enabled."
}
}
function Set-DisableServerManager (){
$CurrentState = Get-ScheduledTask -TaskName "ServerManager"
If ($CurrentState.State -eq "Ready") {
Get-ScheduledTask -TaskName "ServerManager" | Disable-ScheduledTask *>> $Logfile
Write-Log "[$(Get-Date)] Server Manager disabled at logon."
} Else {
Write-Log "[$(Get-Date)] Server Manager already disabled at logon."
}
}
function Set-MonitorTimeout (){
$CurrentVal = POWERCFG /QUERY SCHEME_BALANCED SUB_VIDEO VIDEOIDLE | Select-String -pattern "Current AC Power Setting Index:"
If ($CurrentVal -like "*0x00000000*") {
Write-Log "[$(Get-Date)] Display Timeout already set to Never."
} Else {
POWERCFG /CHANGE -monitor-timeout-ac 0
Write-Log "[$(Get-Date)] Display Timeout set to Never."
}
}
function Set-DisableScreensaver (){
$Path = 'HKCU:\Control Panel\Desktop'
Try {
$CurrentVal = Get-ItemProperty -Path $Path -Name ScreenSaveActive
Write-Log "[$(Get-Date)] $CurrentVal"
} Catch {
$CurrentVal = False
} Finally {
if ($CurrentVal.ScreenSaveActive -ne 0) {
Set-ItemProperty -Path $Path -Name ScreenSaveActive -Value 0 *>> $Logfile
Write-Log "[$(Get-Date)] Screensaver Disabled."
} Else {
Write-Log "[$(Get-Date)] Screensaver Already Disabled."
}
}
}
function Set-DisableUAC (){
$Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
$CurrentVal = Get-ItemProperty -Path $Path -Name ConsentPromptBehaviorAdmin
if ($CurrentVal.ConsentPromptBehaviorAdmin -ne 00000000) {
Set-ItemProperty -Path $Path -Name "ConsentPromptBehaviorAdmin" -Value 00000000 *>> $Logfile
Write-Log "[$(Get-Date)] UAC Disabled."
} Else {
Write-Log "[$(Get-Date)] UAC Already Disabled."
}
}
function Set-DisableIESecurity (){
$AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
$UserKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}"
$CurrentVal = Get-ItemProperty -Path $AdminKey -Name "IsInstalled"
if ($CurrentVal.IsInstalled -ne 0) {
Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0 *>> $Logfile
Set-ItemProperty -Path $UserKey -Name "IsInstalled" -Value 0 *>> $Logfile
Write-Log "[$(Get-Date)] IE ESC Disabled."
} Else {
Write-Log "[$(Get-Date)] IE ESC Already Disabled."
}
}
function Set-StableClock (){
$useplatformclock = bcdedit | Select-String -pattern "useplatformclock Yes"
if ($useplatformclock) {
Write-Log "[$(Get-Date)] Platform Clock Already Enabled."
} Else {
bcdedit /set useplatformclock true *>> $Logfile
Write-Log "[$(Get-Date)] Platform Clock Enabled."
}
}
function Set-DisableShutdownTracker (){
$Path = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Reliability'
Try {
$CurrentVal = Get-ItemProperty -Path $Path -Name ShutdownReasonUI -ErrorAction SilentlyContinue
Write-Log "[$(Get-Date)] $CurrentVal"
} Catch {
$CurrentVal = False
} Finally {
if ($CurrentVal.ShutdownReasonUI -ne 0) {
New-ItemProperty -Path $Path -Name ShutdownReasonUI -Value 0
Write-Log "[$(Get-Date)] Shutdown Tracker Disabled."
}Else{
Write-Log "[$(Get-Date)] Shutdown Tracker Already Disabled."
}
}
}
Function Set-WebPageTestInstall ($tempDir,$AgentDir,$wwwDir){
Copy-Item -Path $AgentDir\agent\* -Destination $wpt_agent_dir -Recurse -Force *>> $Logfile
}
function Set-InstallDummyNet ($InstallDir){
Download-File -url $driver_installer_url -localpath $InstallDir -filename $driver_installer_file
Download-File -url $driver_installer_cert_url -localpath $InstallDir -filename $driver_installer_cert_file
$testsigning = bcdedit | Select-String -pattern "testsigning Yes"
if ($testsigning) {
Write-Log "[$(Get-Date)] Test Signing Already Enabled."
} Else {
bcdedit /set TESTSIGNING ON *>> $Logfile
Write-Log "[$(Get-Date)] Test Signing Enabled."
$dummynet = Get-NetAdapterBinding -Name Ethernet
If ($dummynet.DisplayName -match "dummynet") {
Write-Log "[$(Get-Date)] DummyNet already enabled."
} Else {
Write-Log "[$(Get-Date)] Installation of DummyNet."
Copy-Item -Path $InstallDir\dummynet\64bit\* -Destination $InstallDir\dummynet -Recurse -Force *>> $Logfile
Import-Certificate -FilePath $InstallDir\WPOFoundation.cer -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
cd $InstallDir
.\mindinst.exe $InstallDir\dummynet\netipfw.inf -i -s
Enable-NetAdapterBinding -Name Ethernet -DisplayName ipfw+dummynet
}
}
}
function Set-WebPageTestScheduledTask ($User,$InstallDir){
# for interactive session
$startupFolder = "C:\Users\$User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$startupFolder\wptdriver.exe.lnk")
$Shortcut.TargetPath = "$InstallDir\wptdriver.exe"
$Shortcut.Save()
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$startupFolder\urlBlast.exe.lnk")
$Shortcut.TargetPath = "$InstallDir\urlBlast.exe"
$Shortcut.Save()
# for background session
$GetTask = Get-ScheduledTask
if ($GetTask.TaskName -match "wptdriver") {
Write-Log "[$(Get-Date)] Task (wptdriver) already scheduled."
} Else {
$A = New-ScheduledTaskAction -Execute "$InstallDir\wptdriver.exe"
$T = New-ScheduledTaskTrigger -AtLogon -User $User
$S = New-ScheduledTaskSettingsSet
$P = New-ScheduledTaskPrincipal -UserId "$ThisHost\$User" -LogonType ServiceAccount
Register-ScheduledTask -TaskName "wptdriver" -Action $A -Trigger $T -Setting $S -Principal $P *>> $Logfile
Write-Log "[$(Get-Date)] Task (wptdriver) scheduled."
}
$GetTask = Get-ScheduledTask
if ($GetTask.TaskName -match "urlBlast") {
Write-Log "[$(Get-Date)] Task (urlBlast) already scheduled."
} Else {
$A = New-ScheduledTaskAction -Execute "$InstallDir\urlBlast.exe"
$T = New-ScheduledTaskTrigger -AtLogon -User $User
$S = New-ScheduledTaskSettingsSet
$P = New-ScheduledTaskPrincipal -UserId "$ThisHost\$User" -LogonType ServiceAccount
Register-ScheduledTask -TaskName "urlBlast" -Action $A -Trigger $T -Setting $S -Principal $P *>> $Logfile
Write-Log "[$(Get-Date)] Task (urlBlast) scheduled."
}
}
function Set-ScheduleDefaultUserName ($ThisHost, $User, $Password, $InstallDir) {
Invoke-WebRequest $DefaultUserNameURL -OutFile "$InstallDir\DefaultUserName.ps1" *>> $Logfile
Replace-String -filePath "$InstallDir\DefaultUserName.ps1" -stringToReplace "%%USERNAME%%" -replaceWith $User
$A = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -File $InstallDir\DefaultUserName.ps1"
$T = New-ScheduledTaskTrigger -AtStartup
$S = New-ScheduledTaskSettingsSet
$D = New-ScheduledTask -Action $A -Trigger $T -Settings $S
Register-ScheduledTask -TaskName "DefaultUserName Fix" -InputObject $D -User $User -Password $Password *>> $Logfile
}
function Clean-Deployment{
# Remove Automation initial firewall rule opener
if((Test-Path -Path 'C:\Cloud-Automation')){
Remove-Item -Path 'C:\Cloud-Automation' -Recurse *>> $Logfile
}
# Schedule Task to remove the Psexec firewall rule
$DeletePsexec = {
Remove-Item $MyINvocation.InvocationName
$find_rule = netsh advfirewall firewall show rule "PSexec Port"
if ($find_rule -notcontains 'No rules match the specified criteria.') {
Write-Host "Deleting firewall rule"
netsh advfirewall firewall delete rule name="PSexec Port" *>> $Logfile
}
}
$Cleaner = "C:\Windows\Temp\cleanup.ps1"
Set-Content $Cleaner $DeletePsexec
$ST_Username = "autoadmin"
net user /add $ST_Username $FtpPassword *>> $Logfile
net localgroup administrators $ST_Username /add *>> $Logfile
$ST_Exec = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$ST_Arg = "-NoLogo -NonInteractive -WindowStyle Hidden -ExecutionPolicy ByPass C:\Windows\Temp\cleanup.ps1"
$ST_A_Deploy_Cleaner = New-ScheduledTaskAction -Execute $ST_Exec -Argument $ST_Arg
$ST_T_Deploy_Cleaner = New-ScheduledTaskTrigger -Once -At ((Get-date).AddMinutes(2))
$ST_S_Deploy_Cleaner = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -WakeToRun -DontStopIfGoingOnBatteries -StartWhenAvailable -MultipleInstances Parallel
Register-ScheduledTask -TaskName "Clean Automation" -TaskPath \ -RunLevel Highest -Action $ST_A_Deploy_Cleaner -Trigger $ST_T_Deploy_Cleaner -Settings $ST_S_Deploy_Cleaner -User $ST_Username -Password $FtpPassword *>> $Logfile
}
function Set-WptConfig ($Location, $Url, $Key){
Download-File -url $wpt_urlBlast_ini -localpath $wpt_agent_dir -filename "urlBlast.ini"
Replace-String -filePath "$wpt_agent_dir\urlBlast.ini" -stringToReplace "%%URL%%" -replaceWith $Url
Replace-String -filePath "$wpt_agent_dir\urlBlast.ini" -stringToReplace "%%KEY%%" -replaceWith $Key
Download-File -url $wpt_wptdriver_ini -localpath $wpt_agent_dir -filename "wptdriver.ini"
Replace-String -filePath "$wpt_agent_dir\wptdriver.ini" -stringToReplace "%%URL%%" -replaceWith $Url
Replace-String -filePath "$wpt_agent_dir\wptdriver.ini" -stringToReplace "%%KEY%%" -replaceWith $Key
Replace-String -filePath "$wpt_agent_dir\wptdriver.ini" -stringToReplace "%%LOCATION%%" -replaceWith $Location
}
function Set-ClosePort445 (){
$CurrentVal = Get-NetFirewallRule
if ($CurrentVal.InstanceID -match "PSexec Port" -and $CurrentVal.Enabled -eq "true") {
Disable-NetFirewallRule -Name "PSexec Port" *>> $Logfile
Write-Log "[$(Get-Date)] Port PSexec Port Disabled."
} Elseif($CurrentVal.InstanceID -match "PSexec Port" -and $CurrentVal.Enabled -eq "false"){
Write-Log "[$(Get-Date)] Port PSexec Port Already Disabled."
}Else {
Write-Log "[$(Get-Date)] Port PSexec Port rules does not exist."
}
}
function Disable-FindNetDevices(){
Set-Service fdPHost -StartupType Manual
Stop-Service fdPHost -force
}
# => Main
Set-WindowsLicense -LicenseKey $windows_licenseKey
Activate-Windows-Update
Set-Keyboard -Lang $lang
ExtendPartition
Set-DnsResolver -Resolver1 $DnsResolver1 -Resolver2 $DnsResolver2
Disable-MouseShadow
Set-WebPageTestUser -Username $wpt_user -Password $wpt_password
Set-AutoLogon -Username $wpt_user -Password $wpt_password
Set-DisableServerManager
Set-MonitorTimeout
Set-DisableScreensaver
Set-DisableUAC
Set-DisableIESecurity
Set-StableClock
Set-DisableShutdownTracker
Set-WptFolders
Download-File -url $wpt_zip_url -localpath $wpt_temp_dir -filename $wpt_zip_file
Download-File -url $driver_installer_url -localpath $wpt_agent_dir -filename $driver_installer_file
Download-File -url $driver_installer_cert_url -localpath $wpt_temp_dir -filename $driver_installer_cert_file
Unzip-File -fileName $wpt_zip_file -sourcePath $wpt_temp_dir -destinationPath $wpt_agent_dir
Set-WebPageTestInstall -tempDir $wpt_temp_dir -AgentDir $wpt_agent_dir
Set-InstallDummyNet -InstallDir $wpt_agent_dir
Set-WebPageTestScheduledTask -User $wpt_user -InstallDir $wpt_agent_dir
Set-ScheduleDefaultUserName -ThisHost $wpt_host -User $wpt_user -Password $wpt_password -InstallDir $wpt_agent_dir
Set-WptConfig -Location $wpt_location -Url $wpt_url -Key $wpt_key
Disable-FindNetDevices
Set-ClosePort445
}
# MAIN : Deploy Web Pagge Test
Deploy-WebPagetest
#Deploy-WebPagetest -DomainName "%wptdomain%" -wpt_user "%wptusername%" -wpt_password "%wptpassword%"