Skip to content

Old Init Script

Atiq Rahman edited this page Nov 4, 2021 · 3 revisions

Examples from Old Init Script

This is how we used to launch process/app,

# Support for IsRegAppPath = False, not required anymore
function Start-Process-Single([string] $ProcessRunCommand, [string] $ProcessName, [string] $ProcessPath, [bool] $IsRegAppPath = $true, [string[]] $pArgs) {
  if (Get-Process $ProcessRunCommand -ErrorAction SilentlyContinue) {
    Write-Host "$ProcessName is already running"
  }
  elseif ($IsRegAppPath) {
    Write-Host "Starting $ProcessName"
    # this for fb dev-vm
    if ($ProcessRunCommand -eq 'chrome') {
      # Start-Process $ProcessRunCommand --ignore-certificate-errors
      Start-Process $ProcessRunCommand
    }
    else { Start-Process $ProcessRunCommand }
    # some time for monster chrome to consume resources
    if ($ProcessRunCommand -eq 'chrome') { Start-Sleep 2 }
  }
  elseif ((Test-Path "$ProcessPath")) {
    Write-Host "Starting $ProcessName"
	if ($pArgs) { Start-Process -FilePath $ProcessPath -ArgumentList $pArgs }
	else {Start-Process $ProcessPath}
  }
  else {
    Write-Host "$ProcessName is not installed."
  }
}

SingleInstanceRunning is used to,

  • avoid strings are not appended multiple times
  • only run gadgets for first instance and git update

Implementation

[bool] $isSinglePS=$false
function SingleInstanceRunning([string] $processName)
{
  $isSinglePS = $Global:isSinglePS
  if ($isSinglePS) {
    return $isSinglePS
  }
  if (GetProcessInstanceNumber $processName -lt 2) {
    $isSinglePS = $true
  }
  $Global:isSinglePS = $isSinglePS
  return $isSinglePS
}

Main Method used to call it like this,

if (SingleInstanceRunning) {
  # Update Repository
  UpdateCoreRepo
  # Start other most frequently opened processes
  StartCustomPrcoesses
}
else {
  Write-Host  "`t`t`[Ready`]"
}

This helped preventing later instances of pwsh not to relaunch the same applications again. It also did not need to pull the repository changes again.

Finally, we had a long method named CustomizeConsoleUI to handle Window Size, Color and Title of the powersehll terminal,

<#
.SYNOPSIS
  Settings for Home or Work or any other place to personalize the console UI
.DESCRIPTION
  Minimal init for Console UI
.PARAMETER IsWorkPlace
  Need to add support more workstations if required
.EXAMPLE
  CustomizeConsoleUI
  CustomizeConsoleUI $true
#>
function CustomizeConsoleUI([bool] $IsWorkPlace = $false) {
  # As we are getting "Unable to modify shortcut error", doing this from here; run only in office, I can't change the console properties in Win8 there
  # if ($Env:SESSIONNAME -And $Env:SESSIONNAME.StartsWith('RDP')) { }
  # if ($PSVersionTable.PSEdition -eq 'Core') { }
  $screen_width = 3840
  $screen_height = 2160

  # Get display proportionat size: CurrentHorizontalResolution, CurrentVerticalResolution
  # Since NVidia CUDA installation and driver updates we now have more than one video controller
	# Handle cases that some systems only have one Video Display
  # Only Powershell (Windows Desktop) has GWMI
  if ($PSVersionTable.PSEdition -eq 'Desktop') {
	  $screen_obj = (GWMI win32_videocontroller)[0]
	  if (! $screen_obj) { $screen_obj = (GWMI win32_videocontroller) }
    $screen_width = [convert]::ToInt32([string] ($screen_obj.CurrentHorizontalResolution))
    $screen_height = [convert]::ToInt32([string] ($screen_obj.CurrentVerticalResolution))
  }

  # 10 24 is okay for aspect ratio 16:9
  # 9 22 for 1366x768

  # Resolution: 1920x1080, default aspect ratio, approx result: 198, 33.75
  # This resolution should not be default anymore
  if ($screen_height -eq 1080 -And $screen_width -eq 1920) {
    $console_width = $screen_width/8.27586206896552
    $console_height = $screen_height/32
  }
  # Aspect Ratio: 5:4 - HSL library monitor, approx result 155, 40
  elseif (($screen_height*5) -eq ($screen_width*4)) {
    $console_width = [int] ($screen_width/10)
    $console_height = [int] ($screen_height/21)
  }
  # Current notebook: 3840x2160, aspect ratio (16:9), expected result approx, 294 37
  else {
    $console_width = $screen_width/13
    $console_height = $screen_height/59
  }

  # Write-Host "debug width $screen_width height $screen_height"
  # Write-Host "debug width $console_width height $console_height"
  $WSTitle = $(if ($psConsoleType -eq 'ML' ) { "Machine Learning Workstation" } else { $(if ($IsWorkPlace) { "FB Workstation" } else { "Matrix Workstation" }) })
  (Get-Host).UI.RawUI.WindowTitle = $WSTitle

  # Sometimes, Window Size and buffer size conflict because window size cannot be bigger than buffer size, swapping the statements help
  # Seems like it also requires fixing the console.lnk shortcut in the system
  (Get-Host).UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size -Property @{Width=$width; Height=$history_size}
  (Get-Host).UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size -Property @{Width=$width; Height=$height}

  Write-Host -NoNewline "Applying settings on "
  Write-Host -NoNewline $WSTitle -foregroundcolor Blue
  # ref https://stackoverflow.com/q/2085744
  Write-Host " for" $Env:UserName "`r`n"
}
Clone this wiki locally