WizardKit/.bin/Scripts/reset_browsers.ps1
Alan Mason b180b42ec9 2016-10: Retroactive Updates
# Migration to Python started #
  * PoSH has an extreme slowdown for some systems while it runs an optimization
  ** pass for .NET on the first PoSH script execution.
  ** This is reason enough to move to an alternative.

* New additions:
  * User Data Transfer script
    * Will extract from a WIM or copy from a folder
    * Uses wimlib-imagex for images and FastCopy for folders
    * Removes undesired items after transfer/extraction
  * HWiNFO
  * Missing ODD repair registry patch
  * Q-Dir
  * SW Bundle install script

* ConEmu
  * Moving back to ConEmu for better performance.

* Copy-WizardKit
  * Now uses FastCopy

* functions.py
  * Ported init.ps1 to Python using functions.py (from WinPE) as a base

* Launch.cmd
  * Elevating programs/scripts now done using a temp VBScript file
  * Can run Python scripts (using either the 32 or 64 bit runtime)

* transferred_keys.cmd
  * Expanded searched paths

* Misc
  * Lots of variables and files renamed
  * Lots of hard-coded paths are now in variables
    * Should only be set in scripts in %bin%\Scripts
  * Moved a subset of the Diagnostics launchers to a new 'Extras' folder
    * The launchers moved are those that are less-often used
  * Refactored FindBin code to be more concise
  * Renamed "KitDir" "ClientDir" to indicate that it is on the client's system
  * Removed GeForce Experience launcher as it now requires an account
  * Added link to NVIDIA's driver webpage to download the correct driver
  * Removed AMD's Gaming Evolved launcher
    * This is usually bundled with the GPU driver anyway
  * Switched back to ConEmu
  * Variable and script names are now more descriptive
    * i.e. checklist -> final_checklist, and HH -> %kit_dir%
    * (not happy with %kit_dir%, will probably change again)
2017-11-17 00:53:08 -07:00

388 lines
13 KiB
PowerShell

# Wizard Kit: Reset web browsers to safe default while saving user data
## Init ##
$wd = $(Split-Path $MyInvocation.MyCommand.Path)
pushd "$wd"
. .\init.ps1
clear
$host.UI.RawUI.WindowTitle = "Wizard Kit: Browser Reset Tool"
$backup_path = "$ClientPath\Backups\$username\$date"
$logpath = "$ClientPath\Info\$date"
md "$backup_path" 2>&1 | out-null
md "$logpath" 2>&1 | out-null
$log = "$logpath\Browsers.log"
$bin = (Get-Item $wd).Parent.FullName
$sz = "$bin\7-Zip\7za.exe"
# Vars
$ff_appdata = "$appdata\Mozilla\Firefox"
$ff_clean = $false
$ff_exe = "$programfiles86\Mozilla Firefox\firefox.exe"
if (test-path "$programfiles\Mozilla Firefox\firefox.exe") {
$ff_exe = "$programfiles\Mozilla Firefox\firefox.exe"
}
$ff_profile_list = @(gci "$ff_appdata\Profiles" 2> $null | ?{ $_.PSIsContainer })
$ff_profile_list = $ff_profile_list -inotmatch '\.WKBak'
$chrome_appdata = "$localappdata\Google\Chrome"
$chrome_clean = $false
$chrome_exe = "$programfiles86\Google\Chrome\Application\chrome.exe"
$chrome_profile_list = @(gci "$chrome_appdata\User Data" 2> $null | ?{ $_.PSIsContainer })
$chrome_profile_list = $chrome_profile_list -inotmatch '\.WKBak' -imatch '^(Default|Profile)'
# OS Check
. .\check_os.ps1
if ($arch -eq 64) {
$sz = "$bin\7-Zip\7za64.exe"
}
# Functions
function gen-backup-name {
param ([String]$name)
# Add .WKBak to name
$newname = "$name.WKBak"
# Check if the new name exists
if (test-path "$newname") {
# Change name to avoid overwriting any backups
$x = 2
$newname = "$name.WKBak$x"
while (test-path "$newname") {
$x += 1
$newname = "$name.WKBak$x"
}
}
return $newname
}
function ff-create-default-profile {
WK-write " Creating new default profile" "$log"
# Check for existing profiles
if ($ff_profile_list.length -gt 0) {
WK-error " Firefox profile folders found. Possibly corrupt?" "$log"
return $false
}
# Backup profiles.ini if necessary
## NOTE: While you can use the profiles.ini to recreate the default profile,
## it is better to create a new ini to better ensure the profile dir
## will be in "$ff_appdata\Profiles\"
if (test-path "$ff_appdata\profiles.ini") {
mv "$ff_appdata\profiles.ini" (gen-backup-name "$ff_appdata\profiles.ini")
}
# Create default profile
if (test-path "$ff_exe") {
$ff_args = @(
"-createprofile",
"default")
start -wait "$ff_exe" -argumentlist $ff_args -windowstyle minimized
} else {
WK-error " firefox.exe not found. Please verify installation." "$log"
return $false
}
return $true
}
## Internet Explorer ##
WK-write "==== Internet Explorer ====" "$log"
# Cleanup
if (test-path ".bin\Bleachbit") {
WK-write " Removing temporary files" "$log"
pushd ".bin\Bleachbit"
start -wait "bleachbit_console.exe" -argumentlist @("-c", "internet_explorer.forms", "internet_explorer.temporary_files", "winapp2_internet.internet_explorer_10_11") -verb runas -windowstyle minimized
popd
}
# Backup Favorites
if (test-path "$userprofile\Favorites") {
WK-write " Backing up Favorites" "$log"
pushd "$userprofile"
$sz_args = @(
"a",
"-t7z",
"-mx=1",
"$backup_path\IE Favorites.7z",
"Favorites")
start $sz -argumentlist $sz_args -wait -windowstyle minimized
popd
}
# Get IE settings
$reset_homepage = $true
$ie_settings = gp -path hkcu:"Software\Microsoft\Internet Explorer\Main"
# Reset IE settings (argmuentlist is case sensitive)
if (ask " Reset to default settings?" "$log") {
start -wait rundll32.exe -argumentlist 'inetcpl.cpl,ResetIEtoDefaults'
}
# Get homepage(s)
$_current_homepage = $ie_settings."Start Page"
if ($_current_homepage -ne $null -and $_current_homepage.Length -ne 0) {
$_secondary_homepages = @($ie_settings."Secondary Start Pages")
WK-write " Current homepage: $_current_homepage" "$log"
foreach ($p in $_secondary_homepages) {
if ($p -ne $null -and $p.Length -ne 0) {
WK-write " $p" "$log"
}
}
if ($_current_homepage -inotmatch '^https://www\.google\.com/$' -or $_secondary_homepages -imatch '^.+') {
$reset_homepage = (ask " Replace current homepage with google.com?" "$log")
}
}
# Set homepage(s)
WK-write " Setting homepage" "$log"
if ($reset_homepage) {
sp -path hkcu:"Software\Microsoft\Internet Explorer\Main" -name "Start Page" -Value "https://www.google.com/" 2>&1 | out-null
rp -path hkcu:"Software\Microsoft\Internet Explorer\Main" -name "Secondary Start Pages" 2>&1 | out-null
} else {
sp -path hkcu:"Software\Microsoft\Internet Explorer\Main" -name "Start Page" -Value $_current_homepage 2>&1 | out-null
new-itemproperty -path hkcu:"Software\Microsoft\Internet Explorer\Main" -name "Secondary Start Pages" -propertytype MultiString -value $_secondary_homepages 2>&1 | out-null
}
# Additional settings
if (test-path "$programfiles86\Internet Explorer\iexplore.exe") {
if (ask " Install Google search add-on?" "$log") {
start -wait "$programfiles86\Internet Explorer\iexplore.exe" -argumentlist "http://www.iegallery.com/en-us/Addons/Details/813"
}
#if (ask " Install AdBlock Plus?" "$log") {
# start -wait "$programfiles86\Internet Explorer\iexplore.exe" -argumentlist "https://adblockplus.org"
#}
} else {
WK-error " $programfiles86\Internet Explorer\iexplore.exe not found" "$log"
}
WK-write "" "$log"
## Mozilla Firefox ##
WK-write "==== Mozilla Firefox ====" "$log"
$ff_errors = 0
# Reduce size of AppData folder
if (test-path ".bin\Bleachbit") {
WK-write " Removing temporary files" "$log"
pushd ".bin\Bleachbit"
start -wait "bleachbit_console.exe" -argumentlist @("-c", "firefox.cache", "firefox.forms", "firefox.session_restore", "firefox.vacuum", "winapp2_mozilla.corrupt_sqlites") -verb runas -nonewwindow
popd
}
# Backup AppData
if (test-path "$ff_appdata") {
WK-write " Backing up AppData" "$log"
pushd "$ff_appdata"
$sz_args = @(
"a",
"-t7z",
"-mx=1",
"$backup_path\Firefox.7z",
"Profiles",
"profiles.ini")
start $sz -argumentlist $sz_args -wait -windowstyle minimized
popd
}
# Create default profile if necessary
if ($ff_profile_list.length -eq 0) {
if (ff-create-default-profile) {
# Update profile list to catch newly created profiles
sleep -s 1
$ff_profile_list = @(gci "$ff_appdata\Profiles" 2> $null | ?{ $_.PSIsContainer })
$ff_profile_list = $ff_profile_list -inotmatch '\.WKBak\d*$'
if ($ff_profile_list.length -eq 0) {
WK-warn " Failed to create default profile." "$log"
$ff_errors += 1
} else {
# Configure new profile
$ff_clean = $true
}
} else {
WK-error " Failed to create default profile." "$log"
$ff_errors += 1
}
} else {
WK-write " Profiles found: $($ff_profile_list.length)" "$log"
$ff_clean = (ask " Reset profile(s) to safe settings?" "$log")
}
# Reset profile(s) to safe defaults
if ($ff_clean -and $ff_errors -eq 0) {
pushd "$ff_appdata\Profiles"
foreach ($ff_profile in $ff_profile_list) {
WK-write " Resetting profile: $ff_profile" "$log"
# Backup old settings and only preserve essential settings
$ff_profile_bak = (gen-backup-name "$ff_profile")
mv "$ff_profile" "$ff_profile_bak"
md "$ff_profile" 2>&1 | out-null
# Add "search.json" to $robocopy_args preserve added search engines
$robocopy_args = @(
"/r:3",
"/w:1",
"$ff_profile_bak",
"$ff_profile",
"cookies.sqlite",
"formhistory.sqlite",
"key3.db",
"logins.json",
"persdict.dat",
"places.sqlite")
start "robocopy" -argumentlist $robocopy_args -wait -windowstyle minimized
# Add "searchplugins" below to preserve added search engines
foreach ($subdir in @("bookmarkbackups")) {
if (test-path "$ff_profile_bak\$subdir") {
md "$ff_profile\$subdir" 2>&1 | out-null
$robocopy_args = @(
"/e",
"/r:3",
"/w:1",
"$ff_profile_bak\$subdir",
"$ff_profile\$subdir")
start "robocopy" -argumentlist $robocopy_args -wait -windowstyle minimized
}
}
# Set homepage and search settings
WK-write " Setting homepage and default search" "$log"
out-file -encoding 'ascii' -filepath "$ff_profile\prefs.js" -inputobject 'user_pref("browser.search.geoSpecificDefaults", false);'
out-file -encoding 'ascii' -filepath "$ff_profile\prefs.js" -inputobject 'user_pref("browser.search.defaultenginename", "Google");' -append
out-file -encoding 'ascii' -filepath "$ff_profile\prefs.js" -inputobject 'user_pref("browser.search.defaultenginename.US", "Google");' -append
$homepage = "https://www.google.com/"
if (test-path "$ff_profile_bak\prefs.js") {
$_prefs = gc "$ff_profile_bak\prefs.js"
if ($_prefs -imatch '"browser.startup.homepage"') {
$_current_homepage = $_prefs -imatch '"browser.startup.homepage"'
$_current_homepage = $_current_homepage -ireplace 'user_pref\("browser.startup.homepage", "(.*)"\);', '$1'
$_header = " Current homepage:"
foreach ($url in @("$_current_homepage".split("|"))) {
WK-write "$_header $_current_homepage" "$log"
$_header = " "
}
if ($_current_homepage -inotmatch '^https://www\.google\.com/$') {
if (!(ask " Replace current homepage with google.com?" "$log")) {
$homepage = $_cur_home
}
}
}
}
$homepage = 'user_pref("browser.startup.homepage", "' + $homepage + '");'
out-file -encoding 'ascii' -filepath "$ff_profile\prefs.js" -inputobject $homepage -append
WK-write "" "$log"
}
popd
}
# Install uBlock Origin
if (test-path "$ff_exe") {
if ($ff_errors -eq 0 -and (ask " Install uBlock Origin?" "$log")) {
start -wait "$ff_exe" -argumentlist "https://addons.mozilla.org/en-us/firefox/addon/ublock-origin/"
}
} else {
WK-error " firefox.exe not found. Please verify installation." "$log"
}
WK-write "" "$log"
## Google Chrome ##
WK-write "==== Google Chrome ====" "$log"
$chrome_errors = 0
# Reduce size of AppData folder
if (test-path ".bin\Bleachbit") {
WK-write " Removing temporary files" "$log"
pushd ".bin\Bleachbit"
start -wait "bleachbit_console.exe" -argumentlist @("-c", "google_chrome.cache", "google_chrome.form_history", "google_chrome.search_engines", "google_chrome.session", "google_chrome.vacuum") -verb runas -nonewwindow
popd
}
# Backup AppData
if (test-path "$chrome_appdata") {
WK-write " Backing up AppData" "$log"
pushd "$chrome_appdata"
$sz_args = @(
"a",
"-t7z",
"-mx=1",
"$backup_path\Chrome.7z",
'"User Data"')
start $sz -argumentlist $sz_args -wait -windowstyle minimized
popd
}
# Check for profiles
if ($chrome_profile_list.length -gt 0) {
WK-write " Profiles found: $($chrome_profile_list.length)" "$log"
$chrome_clean = (ask " Reset profile(s) to safe settings?" "$log")
} else {
WK-warn " No profiles found" "$log"
}
# Reset profile(s) to safe defaults
if ($chrome_clean -and $chrome_errors -eq 0) {
pushd "$chrome_appdata\User Data"
foreach ($chrome_profile in $chrome_profile_list) {
WK-write " Cleaning profile: $chrome_profile" "$log"
$chrome_profile_bak = (gen-backup-name "$chrome_profile")
mv "$chrome_profile" "$chrome_profile_bak"
md "$chrome_profile" 2>&1 | out-null
$robocopy_args = @(
"/r:3",
"/w:1",
"$chrome_profile_bak",
"$chrome_profile",
"Bookmarks",
"Cookies",
"Favicons",
'"Google Profile*"',
"History",
'"Login Data"',
'"Top Sites"',
"TransportSecurity",
'"Visited Links"',
'"Web Data"')
start "robocopy" -argumentlist $robocopy_args -wait -windowstyle minimized
WK-write "" "$log"
}
popd
}
# Test for single-user installation
if (test-path "$chrome_appdata\Application\chrome.exe") {
if (test-path "$chrome_exe") {
WK-warn " Single-user and multi-user installations present. Please reinstall Chrome." "$log"
} else {
$chrome_exe = "$chrome_appdata\Application\chrome.exe"
}
}
if (test-path "$chrome_exe") {
# Set Chrome as default browser
start -wait "$chrome_exe" -argumentlist "--make-default-browser"
# Install uBlock Origin
if ($chrome_errors -eq 0 -and (ask " Install uBlock Origin?" "$log")) {
start -wait "$chrome_exe" -argumentlist "https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm?hl=en"
}
} else {
WK-error " chrome.exe not found. Please verify installation." "$log"
}
WK-write "" "$log"
## Done ##
popd
pause "Press Enter to exit..."
# Open log
$notepad2 = "$bin\Notepad2\Notepad2-Mod.exe"
if (test-path $notepad2) {
start "$notepad2" -argumentlist $log
} else {
start "notepad" -argumentlist $log
}