Author Topic: [VBScript] Change browser homepage programatically for Windows  (Read 4126 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
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 :)

Code: (vb) [Select]
'=================================================
'   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"

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: [VBScript] Change browser homepage programatically for Windows
« Reply #1 on: June 26, 2014, 03:26:50 pm »
Thats cool and all but why wouldnt you use group policy to do this?
Ahhh, nevermind ,that would only be for IE :)
« Last Edit: June 26, 2014, 03:27:29 pm by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline RedBullAddicted

  • Moderator
  • Sir
  • *
  • Posts: 519
  • Cookies: 189
    • View Profile
Re: [VBScript] Change browser homepage programatically for Windows
« Reply #2 on: June 26, 2014, 03:28:12 pm »
AFAIK you can only set IE settings via gpo.. correct me if I am wrong. Not a gpo expert.. lol :P
Deep into that darkness peering, long I stood there, wondering, fearing, doubting, dreaming dreams no mortal ever dared to dream before. - Edgar Allan Poe

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: [VBScript] Change browser homepage programatically for Windows
« Reply #3 on: June 26, 2014, 04:07:42 pm »
AFAIK you can only set IE settings via gpo.. correct me if I am wrong. Not a gpo expert.. lol :P
Edited me post :)
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Re: [VBScript] Change browser homepage programatically for Windows
« Reply #4 on: July 20, 2014, 12:56:39 am »
hey Kulver ....I heard you once say that vb.net was for noobs  :-\ ...but the script is kinda  8)

Offline Zesh

  • Royal Highness
  • ****
  • Posts: 699
  • Cookies: 42
    • View Profile
Re: [VBScript] Change browser homepage programatically for Windows
« Reply #5 on: July 20, 2014, 04:44:47 am »
hey Kulver ....I heard you once say that vb.net was for noobs  :-\ ...but the script is kinda  8)

This is VBScript not VB.NET, noob.

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Re: [VBScript] Change browser homepage programatically for Windows
« Reply #6 on: July 20, 2014, 12:50:42 pm »
This is VBScript not VB.NET, noob.
Damn...now that is embarrassing  :( .