Add SessionData class to setup script

Added [SessionData] static class to setup script
Utilized SessionData for scoop refreshing, so it will happen once per login session
 - otherwise it's a nuisance.
This commit is contained in:
Gal Szkolnik 2021-06-29 09:43:27 -04:00
parent 90bd351641
commit 2d858a8a06
2 changed files with 72 additions and 4 deletions

View File

@ -34,12 +34,73 @@ Get-Command Reload-MyScripts -ErrorAction SilentlyContinue | ForEach-Object { .
#######################################################################
function ProfileCode_common {[CmdletBinding()]param()
function Get-PowerShellPath {[CmdletBinding()]param()
Get-Process -PID $PID | ForEach-Object { $_.Path, $_.Parent.Path } | Where-Object { $_ -match $('(powershell|pwsh)' + "`$") } | Select-Object -First 1
Get-Process -PID $PID | ForEach-Object { $_.Path, $_.Parent.Path } | Where-Object { $_ -match $('(?:powershell|pwsh)(?:.exe)?' + "`$") } | Select-Object -First 1
}
function ConvertTo-Base64 {
param([string]$String)
return [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($String))
param([string]$String,[switch]$AsPowershellCommandLine)
return "$(if($AsPowershellCommandLine){"$(Get-PowerShellPath) -EncodedCommand "} )$([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($String)))"
}
class SessionData {
static [hashtable]$Data = [ordered]@{}
static [string]$Path = $null
static [void] Init() {
if( -not [SessionData]::Data.SessionID ) {
[SessionData]::Data.SessionID =
Get-CimInstance -ClassName Win32_Service -Filter 'State="Running" AND DisplayName like "%[_]%"' -Property 'DisplayName','Name' |
Select-Object -ExpandProperty DisplayName |
ForEach-Object {
$_ -split '_' | Select-Object -Last 1
} |
Where-Object { $_ -notin ('agent') } |
Select-Object -First 1
[SessionData]::Data.CreateTimestamp = Get-Date
[SessionData]::Path = Join-Path $env:tmp "session.store.$([SessionData]::Data.SessionID).xmlcli"
}
}
static [int] GetSessionID() {
if( -not [SessionData]::Data.SessionID ) {
[SessionData]::Init()
[SessionData]::Save()
}
return [SessionData]::Data.SessionID
}
static [void] Save() {
if( -not [SessionData]::Path ) { [SessionData]::Init() }
[SessionData]::Data | Export-Clixml -Path $([SessionData]::Path)
}
static [void] Load() {
if( -not [SessionData]::Path ) { [SessionData]::Init() }
[SessionData]::Data = Import-Clixml -Path $([SessionData]::Path)
}
static [void] Set([string]$Name,[object]$Value) {
$null = [SessionData]::GetSessionID()
[SessionData]::Data[$Name] = $Value
[SessionData]::Save()
}
static [object] Get([string]$Name,[object]$DefaultValue=$null) {
[SessionData]::Load()
if( -not [SessionData]::Data.ContainsKey($Name) ) {
if( $DefaultValue ) {
[SessionData]::Set($Name,$DefaultValue)
} else {
return $DefaultValue
}
}
return [SessionData]::Data[$Name]
}
}
function Invoke-ExpressionEx {
@ -428,7 +489,7 @@ function _Init {
}
}
else {
$status = "Need $status"
#$status = $status -replace "Need ", '' -replace '^ ?','Need '
Write-Error "Cannot write into [$_]'$p'! Please re-run with sudo."
}
}

View File

@ -1,3 +1,10 @@
param([switch]$Force)
if( -not $global:ThisSeszion -or $Force ) {
$global:MyModulesWarning = @()
}
scoop autocomplete-on
if( Test-Path (Join-Path $env:SCOOP last-scoop-refresh.clixml) ) {