71 lines
2.4 KiB
PowerShell
71 lines
2.4 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"
|
|
$PrepareStdout = Join-Path $StateRoot "prepare.stdout.log"
|
|
$PrepareStderr = Join-Path $StateRoot "prepare.stderr.log"
|
|
$PrepareProgress = Join-Path $StateRoot "prepare-progress.log"
|
|
$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
|
|
$prepareProc = Get-Process -Name powershell -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.Path -like "*powershell*" -or $_.ProcessName -like "*powershell*" } |
|
|
Sort-Object StartTime -Descending |
|
|
Select-Object -First 5 Id,ProcessName,StartTime
|
|
[pscustomobject]@{
|
|
ok = $true
|
|
pid = $pidValue
|
|
running = $running
|
|
prepared = if ($prepare) { [bool]$prepare.ok } else { $false }
|
|
prepare = $prepare
|
|
prepareRuntime = [ordered]@{
|
|
candidateProcesses = $prepareProc
|
|
stdout = $PrepareStdout
|
|
stderr = $PrepareStderr
|
|
progress = $PrepareProgress
|
|
stdoutTail = Tail-Text $PrepareStdout
|
|
stderrTail = Tail-Text $PrepareStderr
|
|
progressTail = Tail-Text $PrepareProgress
|
|
}
|
|
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
|