LoginVSI – Browser_Start_Random Script

LoginVSI – Browser_Start_Random Script

Background

Browser_Start_Random is a PowerShell script developed to mirror the built-in web and video content randomisation functionality in LoginVSI for Internet Explorer, Google Chrome, Firefox, or Microsoft Edge.

The script was written to achieve this functionality as a natural follow on from writing the Chrome_Start_Random script previously and negates using the built-in App_Start function for Chrome, Firefox, and Edge.

Script Features

The Browser_Start_Random script mirrors the functionality of the LoginVSI built-in Web_Start_Random function by providing the following:

  • Accepts the built-in LoginVSI Variable %VSI_WebLocation%.
  • Provides the capability to open either random web content or video content from the downloaded VSI Share web sites.
  •  Allows the type of video player to be selected from Auto, HTML5, or Flash.
  • Allows the quality of the video to be selected from 240p, 360p, or 480p.
  • Opens a new Internet Explorer, Google Chrome, or Mozilla Firefox window as default depending if they are selected as the browser each time the script is called.
  • Defaults to launching Internet Explorer is the browser method is not used.

The Browser_Start_Random script also provides the following additional functionality:

  • Allows the choice between launching a new window or a new tab in Google Chrome, Internet Explorer, or Mozilla Firefox when the script is called.
    • Note. Microsoft Edge only opens a new window for the first instance opened.  Subsequent executions of the script will open web content in a new tab of the Edge instance.
  • Defaults to launching as a new window if the open method is not supplied to mirror the built-in Web_Start_Random function.
  • Allows the usage of a CSV file holding web sites to provide randomisation of non LoginVSI downloaded web sites.

Script Limitations

The Browser_Start_Random script has the following limitations at this time:

  • When using EDGE as the browser method the script can only launch a new instance of Edge for the first time.  Subsequent executions of the script using Edge as the browser method will open new tabs in the existing .

Script Usage

parameter Mandatory Details
 -WebLocation Yes

The WebLocation parameter is used to provide the script either the location of the VSI downloaded web sites or the name of the csv file to be used.If using the LoginVSI downloaded web sites from the VSI Share then this parameter should use the predefined LoginVSI variable %WebLocation%

If using a CSV file to randomly choose from Interent and / or internal web sites the this parameter should state the csv file name.  The csv file must only contain web site addresses with no header and should be saved in the {VSI ServerName}\{VSI Share}\_VSI_Binaries\Target folder.

 -SiteName No The SiteName parameter is used to choose the LoginVSI downloaded web content so that a random page is selected from a specific site or video quality.  Possible values for the SiteName parameter are:

  • 240P
  • 360P
  • 480P
  • BBC
  • ENGADGET
  • PINTEREST
  • THEVERGE
 -VideoType No The VideoType parameter allows the selection between Auto, HTML5, or Flash when the SiteName parameter is set to 240P, 360P, or 480P
 -WindowMethod No The WindowMethod parameter is used to select whether a new window should be opened for Google Chrome or a new tab.  Possible values for the WindowMethod parameter are:

  • NEW
  • TAB

If the WindowMethod parameter is omitted the the script automatically launches a new window to mirror the functionality of the LoginVSI Web_Start and Web_Start_Random functions.

 -Brower No The Browser parameter is used to select Which browser should be launched.  Possible values for the Browser parameter are:

  • IE
  • EDGE
  • CHROME
  • FIREFOX

If the Broswer parameter is omitted the the script automatically launches Internet Explorer to mirror the functionality of the LoginVSI Web_Start and Web_Start_Random functions.

Script Contents

# Script      - Browser_Start_Random
# Description - Script to launch random web content from Login VSI Share using Google Chrome, Internet Explorer, Microsoft Edge, or Mozilla Firefox.
# Version     - 1.0
# Written By  - Nick Page
# Date        - 11-10-2019 
#
# PowerShell script written for LoginVSI to provide random web and video content using a predefined browser.
#
########################################################################

########################################################################
#
# Configure initial environment

param([Parameter(Mandatory=$true)][string]$WebLocation = "", [string]$Browser="DEFAULT", [string]$SiteName = "", [string]$VideoType = "", [string]$WindowMethod="NEW")

$ContentLocation = ""
$ContentType = "HTML"
$URL = ""
$VideoPlayer = ""
$executable = ""

#########################################################
#
#  Functions
#
#########################################################

#########################################################
#
#  SetContent Function

Function DetermineBrowser
    {
     switch ($browser)
        {
         "CHROME"{$script:executable = "chrome.exe"; break}
         "IE"{$script:executable = "iexplore.exe"; break}
         "EDGE"{$script:executable = "shell:AppsFolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge";break}
         "FIREFOX"{$script:executable = "firefox.exe";break}
         "DEFAULT"{$script:executable = "iexplore.exe"; break}
        }
    }

#########################################################
#
#  SetContent Function

Function SetContent
    {
     switch ($SiteName)
        {
         "240P"{$script:ContentLocation = $WebLocation+"\player\"+$SiteName+"\HTML"; break}
         "360P" {$script:ContentLocation = $WebLocation+"\player\"+$SiteName+"\HTML"; break}
         "480P" {$script:ContentLocation = $WebLocation+"\player\"+$SiteName+"\HTML"; break}
         "BBC" {$script:ContentLocation = $WebLocation+"\"+$SiteName; break}
         "THEVERGE" {$script:ContentLocation = $WebLocation+"\"+$SiteName; break}
         "ENGADGET" {$script:ContentLocation = $WebLocation+"\"+$SiteName; break}
         "PINTEREST" {$script:ContentLocation = $WebLocation+"\"+$SiteName; break}
        }

    switch ($VideoType)
        {
         "AUTO" {$script:VideoPlayer = "AUTO"; $script:ContentType = "Video"; break}
         "HTML5" {$script:VideoPlayer = "HTML5"; $script:ContentType = "Video"; break}
         "FLASH" {$script:VideoPlayer = "FLASH"; $script:ContentType = "Video"; break}
        }
    }

#########################################################
#
# SetWindowMethod Function

Function SetWindowMethod
    {
     switch ($WindowMethod)
         {
          "NEW" {$script:WindowType = "/New-Window"; break}
          "TAB" {$script:WindowType = ""; break}
         }

    If ($Browser -eq "IE")
        {
         $script:WindowType = ""
        }
    Else
        {
         If($Browser -eq "EDGE" -and $WindowMethod -eq "TAB")
            {
             $script:WindowType = "-newtab"
            }
        Else
            {
             $script:WindowType = ""
            }   
        }
    }

#########################################################
#
#  GetRandomPage Function

Function GetRandomPage
    {
     switch ($ContentType)
        {
         "VIDEO"{$arr = Get-ChildItem -Path $script:ContentLocation\* -Include $script:VideoPlayer*.html | Select-Object Fullname; $arr = $arr | Sort-Object {Get-Random}; $Script:URL = $arr[0] -replace ".*=" -replace ".$"; break}
         "HTML"{$arr = Get-ChildItem $script:ContentLocation -Filter "index.html" -Recurse | Select-Object Fullname; $arr = $arr | Sort-Object {Get-Random}; $Script:URL = $arr[0] -replace ".*=" -replace ".$"; break}         
        }
    }

#########################################################
#
#  Main Script
#
#########################################################

DetermineBrowser

If ($WebLocation -like "*.csv")
    {
     $csvFile = $ENV:Temp + "\VSI\RunTime\" + $WebLocation
     $urllist = import-csv $csvFile -Header 'URL'
     $urllist = $urllist  | Sort-Object {Get-Random}
     $Script:URL = $urllist.URL[0]
     SetWindowMethod
    }
Else
    {
     SetContent
     GetRandomPage
     SetWindowMethod
    }

If ($Browser -ne "IE")
    {     
     Start-Process -FilePath $script:executable -ArgumentList "$WindowType $URL"
    }
Else
    {
     If ($WindowMethod -eq "TAB")
        {     
         If(Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""})
            {
             $IE = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
             $IE.visible = $true; 
             $IE.Navigate2($URL, 0x800)
            }
        }
    Else
        {    
         $IE = New-Object -com internetexplorer.application; 
         $IE.visible = $true; 
         $IE.Navigate($URL)
        }
    }

Script Examples

Opening a random BBC web site in Google Chrome

The following example will open a random page from the LoginVSI downloaded BBC web site content in a new Google Chrome window.

  Browser_Start_Random.ps1 –WebLocation %VSI_WebLocation% -SiteName BBC -Browser CHROME

Opening a random video site in Mozilla Firefox

The following example will open a random HTML5 video from the LoginVSI 240p quality web site content in a new Mozilla Firefox window.

  Browser_Start_Random.ps1 –WebLocation %VSI_WebLocation% -SiteName 240P -VideoType HTML5 -Browser FIREFOX

Opening a random BBC web site in a new tab of Internet Explorer

The following example will open a random page from the LoginVSI downloaded BBC web site content in a new Internet Explorer tab if there is already an instance of Internet Explorer running on the target machine.

  Browser_Start_Random.ps1 –WebLocation %VSI_WebLocation% -SiteName BBC -WindowMethod TAB -Browser IE

Opening a random web site from a CSV file

The following example will open a random web site address from a CSV file named chrome_links.csv which has been saved in the {VSI ServerName}\{VSI Share}\_VSI_Binaries\Target folder.

  Browser_Start_Random.ps1 –WebLocation chrome_links.csv -Browser CHROME

 

Leave a Reply

Your email address will not be published. Required fields are marked *