Files
pikasTech-unidesk/scripts/windows/personal-wechat/status.ps1
T

55 lines
1.7 KiB
PowerShell

$ErrorActionPreference = "Continue"
$Root = "C:\UniDesk\personal-wechat"
$StateRoot = Join-Path $Root "wcf-state"
$PidFile = Join-Path $StateRoot "wcf-host.pid"
$StatusFile = Join-Path $StateRoot "status.json"
$PrepareResultFile = Join-Path $StateRoot "prepare-result.json"
$Stdout = Join-Path $StateRoot "wcf-host.stdout.log"
$Stderr = Join-Path $StateRoot "wcf-host.stderr.log"
function Read-JsonFile {
param([string]$Path)
if (!(Test-Path $Path)) { return $null }
try { return Get-Content $Path -Raw | ConvertFrom-Json } catch { return @{ parseError = $_.Exception.Message } }
}
function Tail-Text {
param([string]$Path)
if (!(Test-Path $Path)) { return "" }
try {
return (Get-Content $Path -Tail 80 -ErrorAction SilentlyContinue) -join "`n"
} catch {
return $_.Exception.Message
}
}
$pidValue = $null
$running = $false
if (Test-Path $PidFile) {
$pidValue = Get-Content $PidFile -ErrorAction SilentlyContinue
if ($pidValue) {
$running = [bool](Get-Process -Id ([int]$pidValue) -ErrorAction SilentlyContinue)
}
}
$status = Read-JsonFile $StatusFile
$prepare = Read-JsonFile $PrepareResultFile
[pscustomobject]@{
ok = $true
pid = $pidValue
running = $running
prepared = if ($prepare) { [bool]$prepare.ok } else { $false }
prepare = $prepare
status = $status
ports = Get-NetTCPConnection -LocalPort 10086,10087 -ErrorAction SilentlyContinue | Select-Object LocalAddress,LocalPort,State,OwningProcess
logs = [ordered]@{
stdout = $Stdout
stderr = $Stderr
stdoutTail = Tail-Text $Stdout
stderrTail = Tail-Text $Stderr
}
paths = [ordered]@{
root = $Root
stateRoot = $StateRoot
statusFile = $StatusFile
prepareResultFile = $PrepareResultFile
}
} | ConvertTo-Json -Depth 8