=Modified SessionData to work on Linux

This commit is contained in:
lksz 2021-06-29 10:35:25 -04:00
parent 2d858a8a06
commit 55f5acc0f2
1 changed files with 36 additions and 12 deletions

View File

@ -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()