From 55f5acc0f2640ea228b6de7d3b87ffd80de89af0 Mon Sep 17 00:00:00 2001 From: lksz Date: Tue, 29 Jun 2021 10:35:25 -0400 Subject: [PATCH] =Modified SessionData to work on Linux --- Setup-ScriptEnv.ps1 | 48 +++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/Setup-ScriptEnv.ps1 b/Setup-ScriptEnv.ps1 index e3d3083..8562d67 100644 --- a/Setup-ScriptEnv.ps1 +++ b/Setup-ScriptEnv.ps1 @@ -42,28 +42,52 @@ param([string]$String,[switch]$AsPowershellCommandLine) return "$(if($AsPowershellCommandLine){"$(Get-PowerShellPath) -EncodedCommand "} )$([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($String)))" } +function Get-TempPath { + #New-TemporaryFile | ForEach-Object { Remove-Item $_; $_.DirectoryName } + [System.IO.Path]::GetTempPath() +} + 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 - + if( Get-Command -Name Get-CimInstance -ErrorAction SilentlyContinue ) { + [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 | + Out-String + } else { + $local:p = Get-Process -PID $global:PID + $local:t_p = $null + do { + $t_p = $null + if( $p.Parent.Id ) { + $t_p = Get-Process -Id $($p.Parent.Id) | Where-Object StartTime + } + if( $t_p ) { + $p = $t_p + } + } while ($t_p) + [SessionData]::Data.SessionID = "$( + $p.StartTime.ToString("yyyyMMddHHmmss") + )-$( + $p.SessionId + )" + } [SessionData]::Data.CreateTimestamp = Get-Date - - [SessionData]::Path = Join-Path $env:tmp "session.store.$([SessionData]::Data.SessionID).xmlcli" + + [SessionData]::Path = Join-Path $(Get-TempPath) "session.store.$([SessionData]::Data.SessionID).xmlcli" } } - static [int] GetSessionID() { + static [string] GetSessionID() { if( -not [SessionData]::Data.SessionID ) { [SessionData]::Init()