setup-final.ps1 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Run once, elevated, on the installed lab VM.
  2. # Configures 100% DPI, UAC off, lab PoC files, and a disabled logon task.
  3. # The logon task is enabled manually just before the final recorded boot.
  4. $ErrorActionPreference = 'Stop'
  5. $Root = $PSScriptRoot
  6. $Ev = Join-Path $Root 'evidence'
  7. New-Item -ItemType Directory -Force -Path $Ev | Out-Null
  8. try { Start-Transcript -Path (Join-Path $Ev 'setup-final.txt') -Append | Out-Null } catch { }
  9. function Log([string]$Message) {
  10. $line = '[{0:HH:mm:ss}] {1}' -f (Get-Date), $Message
  11. Write-Host $line
  12. try { Add-Content -LiteralPath (Join-Path $Ev 'setup-final.log') -Value $line -Encoding UTF8 } catch { }
  13. }
  14. try {
  15. Log 'setup-final: timezone'
  16. & tzutil.exe /s 'China Standard Time' | Out-Null
  17. Log 'setup-final: 100% DPI'
  18. & reg.exe add 'HKCU\Control Panel\Desktop' /v LogPixels /t REG_DWORD /d 96 /f | Out-Null
  19. & reg.exe add 'HKCU\Control Panel\Desktop' /v Win8DpiScaling /t REG_DWORD /d 0 /f | Out-Null
  20. & reg.exe add 'HKCU\Control Panel\Desktop' /v DesktopDPIOverride /t REG_SZ /d 0 /f | Out-Null
  21. Log 'setup-final: UAC off'
  22. & reg.exe add 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' /v EnableLUA /t REG_DWORD /d 0 /f | Out-Null
  23. & reg.exe add 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f | Out-Null
  24. Log 'setup-final: PCL EULA bookkeeping'
  25. & reg.exe add 'HKCU\Software\PCL' /v SystemEulaVersion /t REG_SZ /d 2 /f | Out-Null
  26. & reg.exe add 'HKCU\Software\PCL' /v HintNotice /t REG_SZ /d 0 /f | Out-Null
  27. Log 'setup-final: defender exclusions'
  28. try {
  29. Add-MpPreference -ExclusionPath @('C:\PCLPoC', (Join-Path $env:USERPROFILE 'Desktop\PCL'), 'C:\Users\Public') -ErrorAction SilentlyContinue
  30. Set-MpPreference -DisableRealtimeMonitoring $true -ErrorAction SilentlyContinue
  31. } catch {
  32. Log ('setup-final: defender warning: ' + $_.Exception.Message)
  33. }
  34. Log 'setup-final: copy PoC to C:\PCLPoC'
  35. if (Test-Path -LiteralPath 'C:\PCLPoC') {
  36. Remove-Item -LiteralPath 'C:\PCLPoC' -Recurse -Force -ErrorAction SilentlyContinue
  37. }
  38. Copy-Item -LiteralPath (Join-Path $Root 'PCLPoC') -Destination 'C:\PCLPoC' -Recurse -Force
  39. Log 'setup-final: remove old retry task / startup entry'
  40. Unregister-ScheduledTask -TaskName 'PCLPoCRetry' -Confirm:$false -ErrorAction SilentlyContinue | Out-Null
  41. $startup = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\Startup\PCLDemo.cmd'
  42. Remove-Item -LiteralPath $startup -Force -ErrorAction SilentlyContinue
  43. Log 'setup-final: create C:\pcl-demo.cmd'
  44. $wrapper = @'
  45. @echo off
  46. for %%d in (C D E F G H I J K) do (
  47. if exist "%%d:\pcl-test\demo.ps1" (
  48. powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%%d:\pcl-test\demo.ps1"
  49. exit /b 0
  50. )
  51. )
  52. '@
  53. Set-Content -LiteralPath 'C:\pcl-demo.cmd' -Value $wrapper -Encoding ASCII
  54. Log 'setup-final: register disabled logon task'
  55. $user = $env:COMPUTERNAME + '\' + $env:USERNAME
  56. $action = New-ScheduledTaskAction -Execute 'C:\pcl-demo.cmd'
  57. $trigger = New-ScheduledTaskTrigger -AtLogOn -User $user
  58. $principal = New-ScheduledTaskPrincipal -UserId $user -LogonType Interactive -RunLevel Highest
  59. Register-ScheduledTask -TaskName 'PCLPoCDemo' -Action $action -Trigger $trigger -Principal $principal -Force | Out-Null
  60. Disable-ScheduledTask -TaskName 'PCLPoCDemo' | Out-Null
  61. Log 'setup-final: done; rebooting'
  62. } catch {
  63. Log ('setup-final: ERROR ' + $_.Exception.Message)
  64. try { Add-Content -LiteralPath (Join-Path $Ev 'setup-final-error.txt') -Value $_.Exception.ToString() -Encoding UTF8 } catch { }
  65. } finally {
  66. try { Stop-Transcript | Out-Null } catch { }
  67. }
  68. & shutdown.exe /r /t 8 /c 'PCL PoC final setup'