vbscript-mini-logo

Change browser homepage programatically for Windows

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 :)

  1. '=================================================
  2. '   Author: Kulverstukas
  3. '   Date: 2014.06.23
  4. '   Description:
  5. '       Changes the homepage for 3 most used browsers: Firefox, Chrome and IE.
  6. '       Could be added to a Logon script on a domain to do it every time user logs on.
  7. '=================================================
  8.  
  9. Set wshShell = CreateObject("WScript.Shell")
  10. Set objFS = CreateObject("Scripting.FileSystemObject")
  11.  
  12. '================= Firefox Block =================
  13. Const ffFile = "\prefs.js"
  14. Const ffTemp = "\prefs1.js"
  15. Const homePageSubStr = """browser.startup.homepage"""
  16. Const homePagePref = "user_pref(""browser.startup.homepage"", ""http://localhost/"");"
  17. Const showHomepageSubStr = """browser.startup.page"""
  18. Const showHomepagePref = "user_pref(""browser.startup.page"", 1);"
  19. Const ffDir = "%appdata%\Mozilla\Firefox\Profiles"
  20. foundHomePage = False
  21. foundShowHomePage = False
  22.  
  23. If (objFS.FolderExists(wshShell.ExpandEnvironmentStrings(ffDir))) Then
  24.     Set profiles = objFS.GetFolder(wshShell.ExpandEnvironmentStrings(ffDir)).SubFolders
  25.     For Each profile in profiles  ' change the homepage for every profile we can find
  26.         Set textStream = objFS.GetFile(profile & ffFile).OpenAsTextStream(1, -2)
  27.         Set objOutFile = objFS.CreateTextFile(profile & ffTemp, True)
  28.         Do Until textStream.AtEndOfStream
  29.             strLine = textStream.ReadLine
  30.             If (InStr(strLine, homePageSubStr) > 0) Then
  31.                 objOutFile.WriteLine(homePagePref)
  32.                 foundHomePage = True
  33.             Else If (InStr(strLine, showHomepageSubStr) > 0) Then
  34.                 objOutFile.WriteLine(showHomepagePref)
  35.                 foundShowHomePage = True
  36.             Else
  37.                 objOutFile.WriteLine(strLine)
  38.             End If
  39.             End If
  40.         Loop
  41.         ' append the configs if these lines were not found
  42.         If (foundHomePage = False) Then
  43.             objOutFile.WriteLine(homePagePref)
  44.         End If
  45.         If (foundShowHomePage = False) Then
  46.             objOutFile.WriteLine(showHomepagePref)
  47.         End If
  48.  
  49.         objOutFile.Close()
  50.         textStream.Close()
  51.         objFS.DeleteFile(profile & ffFile)
  52.         objFS.MoveFile profile & ffTemp, profile & ffFile
  53.     Next
  54. End If
  55.  
  56. '================= Chrome  Block =================
  57. Const chromeFile = "\Preferences"
  58. Const chromeTempFile = "\Preferences1"
  59. Const sessionRestoreSubStr = """restore_on_startup"""
  60. Const sessionRestorePref = "      ""restore_on_startup"": 4,"
  61. Const sessionStartupUrlSubStr = """startup_urls"""
  62. Const sessionStartupUrlPref = "      ""startup_urls"": [ ""http://localhost/"" ],"
  63. Const chromeDirVista = "%localappdata%\Google\Chrome\User Data\Default"
  64. Const chromeDirXP = "%userprofile%\Local Settings\Application Data"
  65. Const strComputer = "."
  66. Const vistaVer = "6.1"
  67. Const xpVer = "5.1"
  68.  
  69. ' we must detect the system here so that we can know what path to take
  70. Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
  71. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  72. Set oss = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
  73. finalDir = ""
  74. For Each os in oss ' could be better I suppose than this I guess :P
  75.     If (InStr(os.Version, xpVer) > 0) Then
  76.         finalDir = wshShell.ExpandEnvironmentStrings(chromeDirXP)
  77.     Else If (InStr(os.Version, vistaVer) > 0) Then
  78.         finalDir = wshShell.ExpandEnvironmentStrings(chromeDirVista)
  79.     End If
  80.     End If
  81. Next
  82. If (objFS.FileExists(finalDir & chromeFile)) Then
  83.     Set textStream = objFS.GetFile(finalDir & chromeFile).OpenAsTextStream(1, -2)
  84.     Set objOutFile = objFS.CreateTextFile(finalDir & chromeTempFile, True)
  85.     ' business as usual...
  86.     Do Until textStream.AtEndOfStream
  87.         strLine = textStream.ReadLine
  88.         If (InStr(strLine, sessionRestoreSubStr) > 0) Then
  89.             objOutFile.WriteLine(sessionRestorePref)
  90.         Else If (InStr(strLine, sessionStartupUrlSubStr) > 0) Then
  91.             objOutFile.WriteLine(sessionStartupUrlPref)
  92.         Else
  93.             objOutFile.WriteLine(strLine)
  94.         End If
  95.         End If
  96.     Loop
  97.     objOutFile.Close()
  98.     textStream.Close()
  99.     objFS.DeleteFile(finalDir & chromeFile)
  100.     objFS.MoveFile finalDir & chromeTempFile, finalDir & chromeFile
  101. End If
  102.  
  103. '================= Internet Explorer Block =======
  104. wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", "http://localhost/", "REG_SZ"
  105. wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\RunOnceHasShown", "1", "REG_DWORD"
  106. wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\RunOnceComplete", "1", "REG_DWORD"
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments