Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Script to download the secure web browser for NBME, and reduce as much as possible
- the chance of the secure exam being interrupted by external variables. This is to
- get the NBME exam to load and the tests to be ready with as little input from the
- operator as possible
- Written for UNM SOM UME ETS
- Version 2.1.11032015
- #>
- #Check to make sure we're connected to a netowrk. If not, see if HSC_Guest is available,
- #and then try and auto-connect to that.
- function Clean-Quit { #leave the script gracefully
- try
- {
- Write-Host "Press Y to Shutdown, or Press Any Other Key to Exit..." -BackgroundColor Green -ForegroundColor White
- $WaitKeypress = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | select character -expand character
- if ( $WaitKeypress -eq 'y' ) { Stop-Computer }
- }
- catch
- {
- Write-Host "Actually, never mind. I lied. You need to close this window to exit." -BackgroundColor Blue -ForegroundColor red
- }
- exit
- }
- function Dirty-Quit { #something bad went down
- param([string]$Reason)
- Write-Host $Reason -BackgroundColor Red -ForegroundColor White
- try
- {
- $WaitKeypress = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
- }
- catch {}
- exit
- }
- function Get-Networks { #return a string with the network SSID or LAN
- $SSID = netsh wlan show interfaces | findstr /r "^....SSID"
- $netInfo = Get-WmiObject win32_networkadapter -filter "netconnectionstatus = 2" | select netconnectionid -expand netconnectionid
- if ($netInfo.count -eq 0) { return $false }
- elseif ($netInfo -contains "Local Area Connection") { return "Local Area Connection" }
- else { return [string]$SSID.SubString(29) }
- }
- function Query-NTP { #Get the internet time from time.nist.gov. Probably won't work with anywhere else.
- $TcpClient = New-Object System.Net.Sockets.TcpClient
- [byte[]]$buffer = ,0 * 64
- try
- {
- $TcpClient.Connect('time.nist.gov',13)
- }
- catch
- {
- return $false
- }
- $TcpStream = $TcpClient.GetStream()
- $length = $TcpStream.read($buffer, 0, $buffer.length)
- [void]$TcpClient.Close()
- if ($length -gt 0) {
- $raw = [Text.Encoding]::ASCII.GetString($buffer)
- return [DateTime]::ParseExact($raw.SubString(7,17),'yy-MM-dd HH:mm:ss', $null).toLocalTime()
- } else {
- return $false
- }
- }
- Function Check-Time { #Check that our computer time matches official time (NBME's browser is date-based, I don't care about am/pm)
- Write-Host "Attempting to check for correct system Date/Time"
- $ntpTime = Query-NTP | Out-String
- $LocalTime = get-date | Out-String
- if ($ntpTime.Length -ge 24) { $ntpTime = $ntpTime.SubString(1,24) }
- else { return "Unable to successfully query NTP. Disregarding." }
- $LocalTime = $LocalTime.SubString(1,24)
- if ($LocalTime -eq $ntpTime) {
- Write-Host "System Date/Time is correct." -ForegroundColor Green
- } else {
- Write-Host "Incorrect system date. Download will most likely fail." -BackgroundColor Yellow -ForegroundColor White #we don't quit, because it's probs nothing
- }
- }
- function Check-Activation { #check windows activation state. Will eventually automatically activate windows. Not really necessary for us anymore, but left it in for funsies.
- $licenseStatus=@{0="Unlicensed"; 1="Licensed"; 2="OOBGrace"; 3="OOTGrace"; 4="NonGenuineGrace"; 5="Notification"; 6="ExtendedGrace"}
- $ActStatus = Get-CimInstance -Class SoftwareLicensingProduct | Where {$_.ApplicationID -eq "55c92734-d682-4d71-983e-d6ec3f16059f" -AND $_.PartialProductKey -ne $null}
- return $licenseStatus[[int]$ActStatus.LicenseStatus]
- }
- function Check-Secure { #see if secure is available. If it is, check that creds are cached. If not, user has to join.
- $secAvail = netsh wlan show networks | findstr /r "HSC_Secure"
- if ( ! $secAvail ) {
- Dirty-Quit("HSC_Secure is unavailabe. No other connection found. Connection via ethernet required")
- }
- else {
- Write-Host "Attempting to connect to HSC_Secure..."
- netsh wlan connect ssid=HSC_Secure name=HSC_Secure interface="Wireless Network Connection" | Out-Null
- if (! $?) { Dirty-Quit("Connection to HSC_Secure failed due to incorrect or missing credentials. Please connect manually.") }
- else {
- write-host "Pausing five seconds to allow connection to complete...."
- Start-Sleep -s 5
- return
- }
- }
- }
- function Remove-Guest {
- if ( Get-Networks -eq "HSC_Guest" ) { netsh wlan disconnect interface="Wireless Network Connection" | Out-Null }
- $WLAN_Profiles = netsh wlan show profiles | findstr /r "HSC_Guest"
- if ($WLAN_Profiles) { netsh wlan delete profile HSC_Guest | Out-Null }
- }
- #Checks to make sure PS is >=3 and PSWindowsUpdate is in the Modules
- function checkVer {
- if ( ! (Test-Path C:\windows\System32\WindowsPowershell\v1.0\Modules\PSWindowsUpdate) -or ($PSVersionTable.PSVersion | select -expand Major) -lt 3 ) {
- return $false
- } else {
- return $true
- }
- }
- #We need to set these variables before establishing the function, otherwise PS gets all pee-pee hearted
- #Get Scheduled Tasks
- $TaskSchedule = New-Object -com("Schedule.Service")
- $TaskSchedule.connect("localhost")
- try
- {
- $EnabledTasks = $TaskSchedule.getfolder("\apple").gettasks(0) | Format-Table name
- }
- catch {}
- $EnabledTasks += $TaskSchedule.getfolder("\").gettasks(0) | Format-Table name
- $EnabledTasks = $EnabledTasks | Format-Table Name
- #Get Running Process
- $RunningProcesses = Get-Process | Format-Table name
- #Kills processes that are running
- function Kill-Process {
- param($ProcessName)
- $CheckProcess = $RunningProcesses | findstr /r "$ProcessName"
- if ($CheckProcess) { Stop-Process -processname "$ProcessName" -Force }
- else { Write-Host "Process $ProcessName is not running" -ForegroundColor DarkYellow }
- }
- function Disable-SchedTask {
- param(
- [string]$RootFolder,
- [string]$TaskName
- )
- $TaskEnabled = $EnabledTasks | findstr /r $TaskName
- if ($TaskEnabled) {
- $WorkTask = $TaskSchedule.GetFolder("$RootFolder").GetTask("$TaskName")
- $WorkTask.Enabled = $False }
- else { Write-Host "Task $TaskName is not a scheduled task" -ForegroundColor DarkYellow }
- }
- #Check for PS version and PSWindowsUpdate before continuing to load those
- $hasVer = checkVer
- #
- #
- # End of function declarations. Runtime code starts
- # here.
- #
- #
- #
- #Set our variables
- $userPath=$env:TEMP
- $DateStr = Get-Date -format "MMM-dd-yyyy"
- #Find our connection
- $connStatus = Get-Networks
- if (! $connStatus) { Write-Host "No current connections found" }
- else { Write-Host "Connected via $connStatus" }
- switch ($connStatus) {
- $false {
- Check-Secure
- Check-Time
- }
- "Local Area Connection" {
- Check-Time
- }
- "HSC_Secure" {
- Check-Time
- }
- "HSC_Guest" {
- Write-Host "HSC_Guest cannot be used for testing purposes."
- Remove-Guest
- Check-Secure
- Check-Time
- }
- "Unknown Network" {
- Write-Host "We're connected to an unknown network, so we're just going to give a shot. No promises." -BackgroundColor Yellow -ForegroundColor White
- }
- }
- #All of this functionality relies on an updated version of PS (Management Framework 4)
- #And a particular library that needs to be installed.
- if ($hasVer)
- {
- Import-Module PSWindowsUpdate
- write-host "Checking reboot status..."
- Get-WURebootStatus #This function asks the user if they want to restart, so we don't have to.
- Write-Host "Checking that Windows is activated..."
- $isActivated = Check-Activation
- if ( $isActivated -eq "Licensed" ) { Write-Host "Windows is activated." -ForegroundColor Green }
- else {
- Dirty-Quit("Windows in an unactivated state will interrupt the testing ")
- }
- }
- else
- {
- Write-Host "INFO: Module PSWindowsUpdate is missing, or the PowerShell version installed does not support it." -ForegroundColor Black -BackgroundColor Yellow
- Write-Host "INFO: Some (non-essential) functionality has been disabled." -ForegroundColor White -BackgroundColor Yellow
- }
- #Stop the windows update service to prevent it from interrupting our test
- Write-Host "Stopping Windows Update services..."
- Stop-Service wuauserv -force
- Stop-Service bits -force
- Stop-Service appidsvc -force
- Stop-Service cryptsvc -force
- Write-Host "Done." -ForegroundColor Green
- #Stop any update processes that may be running
- Write-Host "Killing any software process that may interrupt our user's session"
- Kill-Process("SoftwareUpdate")
- Kill-Process("AdobeARM")
- Kill-Process("FlashPlayerUpdateService")
- Kill-Process("jusched")
- Write-Host "Done." -ForegroundColor Green
- #Stop any Scheduled Services that may interrupt our session
- Write-Host "Disabling all scheduled tasks that may interrupt our user's session"
- Disable-SchedTask "\" "Adobe Flash Player Updater"
- Disable-SchedTask "\Apple" "AppleSoftwareUpdate"
- Write-Host "Done." -ForegroundColor Green
- #Create download object, and download
- Write-Host "Grabbing NBME Secure Browser ($DateStr). This may take a while..."
- $Client = New-Object System.Net.WebClient
- $GetFile = "https://wbt.nbme.org/wbtexam/download/today/PCSecureBrowser-$(Get-Date -format "MMM-dd-yyyy").exe"
- $ExecFile = "$userPath\PCSecureBrowser-$DateStr.exe"
- try
- {
- $Client.DownloadFile($GetFile,$ExecFile)
- }
- catch
- {
- $count = 0
- do {
- $count++
- Write-Host "Download command failed to complete! Retrying $count of 3" -BackgroundColor Red -ForegroundColor White
- Start-Sleep -s 5
- try
- {
- $Client.DownloadFile($GetFile,$ExecFile)
- }
- catch {}
- } while ( (! $?) -and ($count -lt 3))
- if ( $count -ge 3 ) {
- Dirty-Quit("The download failed to complete. Check internet connectivity.")
- }
- }
- Write-Host "Download command completed successfully." -ForegroundColor green
- Write-Host "Verifying File..."
- if (Test-Path $ExecFile)
- {
- Write-Host "Success!" -ForegroundColor Green
- }
- else
- {
- Dirty-Quit("There was something wrong with the download. Try again, or download manually.")
- }
- #Start secure exam, and wait for it to finish before continuing.
- Write-Host "Starting Secure Exam..."
- & $ExecFile | Out-Null
- Write-Host "Secure Exam Finished."
- #Restart Windows Update services
- Write-Host "Restarting Windows Update services..."
- Start-Service wuauserv
- Start-Service bits
- Start-Service appidsvc
- Start-Service cryptsvc
- Write-Host "Done." -ForegroundColor Green
- #Clean up
- Write-Host "Cleaning up..."
- Remove-Item $ExecFile
- Write-Host "All tasks completed! We're done here." -ForegroundColor Green
- Clean-Quit
Add Comment
Please, Sign In to add comment