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:
parent
90bd351641
commit
2d858a8a06
|
@ -34,12 +34,73 @@ Get-Command Reload-MyScripts -ErrorAction SilentlyContinue | ForEach-Object { .
|
||||||
#######################################################################
|
#######################################################################
|
||||||
function ProfileCode_common {[CmdletBinding()]param()
|
function ProfileCode_common {[CmdletBinding()]param()
|
||||||
function Get-PowerShellPath {[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 {
|
function ConvertTo-Base64 {
|
||||||
param([string]$String)
|
param([string]$String,[switch]$AsPowershellCommandLine)
|
||||||
return [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($String))
|
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 {
|
function Invoke-ExpressionEx {
|
||||||
|
@ -428,7 +489,7 @@ function _Init {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$status = "Need $status"
|
#$status = $status -replace "Need ", '' -replace '^ ?','Need '
|
||||||
Write-Error "Cannot write into [$_]'$p'! Please re-run with sudo."
|
Write-Error "Cannot write into [$_]'$p'! Please re-run with sudo."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
|
param([switch]$Force)
|
||||||
|
|
||||||
|
if( -not $global:ThisSeszion -or $Force ) {
|
||||||
|
$global:MyModulesWarning = @()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
scoop autocomplete-on
|
scoop autocomplete-on
|
||||||
|
|
||||||
if( Test-Path (Join-Path $env:SCOOP last-scoop-refresh.clixml) ) {
|
if( Test-Path (Join-Path $env:SCOOP last-scoop-refresh.clixml) ) {
|
||||||
|
|
Loading…
Reference in New Issue