At work we had to change the homepage of all browsers for students for convenience reasons and so I wrote this VBScript, which is my first ever, so it could be made better maybe, but it works great – I seem to fall in love with it :D . I only wrote the script to change Firefox, Chrome and InternetExploder settings, because only those browsers are available on computers accessible to students.
The script was put in the Logon script of our Domain to run every time a user logs in.
Whole script can be downloaded from here and I’m posting the code for quick review :)
'=================================================
' Author: Kulverstukas
' Date: 2014.06.23
' Description:
' Changes the homepage for 3 most used browsers: Firefox, Chrome and IE.
' Could be added to a Logon script on a domain to do it every time user logs on.
'=================================================
Set wshShell = CreateObject("WScript.Shell")
Set objFS = CreateObject("Scripting.FileSystemObject")
'================= Firefox Block =================
Const ffFile = "\prefs.js"
Const ffTemp = "\prefs1.js"
Const homePageSubStr = """browser.startup.homepage"""
Const homePagePref = "user_pref(""browser.startup.homepage"", ""http://localhost/"");"
Const showHomepageSubStr = """browser.startup.page"""
Const showHomepagePref = "user_pref(""browser.startup.page"", 1);"
Const ffDir = "%appdata%\Mozilla\Firefox\Profiles"
foundHomePage = False
foundShowHomePage = False
If (objFS.FolderExists(wshShell.ExpandEnvironmentStrings(ffDir))) Then
Set profiles = objFS.GetFolder(wshShell.ExpandEnvironmentStrings(ffDir)).SubFolders
For Each profile in profiles ' change the homepage for every profile we can find
Set textStream = objFS.GetFile(profile & ffFile).OpenAsTextStream(1, -2)
Set objOutFile = objFS.CreateTextFile(profile & ffTemp, True)
Do Until textStream.AtEndOfStream
strLine = textStream.ReadLine
If (InStr(strLine, homePageSubStr) > 0) Then
objOutFile.WriteLine(homePagePref)
foundHomePage = True
Else If (InStr(strLine, showHomepageSubStr) > 0) Then
objOutFile.WriteLine(showHomepagePref)
foundShowHomePage = True
Else
objOutFile.WriteLine(strLine)
End If
End If
Loop
' append the configs if these lines were not found
If (foundHomePage = False) Then
objOutFile.WriteLine(homePagePref)
End If
If (foundShowHomePage = False) Then
objOutFile.WriteLine(showHomepagePref)
End If
objOutFile.Close()
textStream.Close()
objFS.DeleteFile(profile & ffFile)
objFS.MoveFile profile & ffTemp, profile & ffFile
Next
End If
'================= Chrome Block =================
Const chromeFile = "\Preferences"
Const chromeTempFile = "\Preferences1"
Const sessionRestoreSubStr = """restore_on_startup"""
Const sessionRestorePref = " ""restore_on_startup"": 4,"
Const sessionStartupUrlSubStr = """startup_urls"""
Const sessionStartupUrlPref = " ""startup_urls"": [ ""http://localhost/"" ],"
Const chromeDirVista = "%localappdata%\Google\Chrome\User Data\Default"
Const chromeDirXP = "%userprofile%\Local Settings\Application Data"
Const strComputer = "."
Const vistaVer = "6.1"
Const xpVer = "5.1"
' we must detect the system here so that we can know what path to take
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set oss = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
finalDir = ""
For Each os in oss ' could be better I suppose than this I guess :P
If (InStr(os.Version, xpVer) > 0) Then
finalDir = wshShell.ExpandEnvironmentStrings(chromeDirXP)
Else If (InStr(os.Version, vistaVer) > 0) Then
finalDir = wshShell.ExpandEnvironmentStrings(chromeDirVista)
End If
End If
Next
If (objFS.FileExists(finalDir & chromeFile)) Then
Set textStream = objFS.GetFile(finalDir & chromeFile).OpenAsTextStream(1, -2)
Set objOutFile = objFS.CreateTextFile(finalDir & chromeTempFile, True)
' business as usual...
Do Until textStream.AtEndOfStream
strLine = textStream.ReadLine
If (InStr(strLine, sessionRestoreSubStr) > 0) Then
objOutFile.WriteLine(sessionRestorePref)
Else If (InStr(strLine, sessionStartupUrlSubStr) > 0) Then
objOutFile.WriteLine(sessionStartupUrlPref)
Else
objOutFile.WriteLine(strLine)
End If
End If
Loop
objOutFile.Close()
textStream.Close()
objFS.DeleteFile(finalDir & chromeFile)
objFS.MoveFile finalDir & chromeTempFile, finalDir & chromeFile
End If
'================= Internet Explorer Block =======
wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", "http://localhost/", "REG_SZ"
wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\RunOnceHasShown", "1", "REG_DWORD"
wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\RunOnceComplete", "1", "REG_DWORD"