43 lines
1.4 KiB
PowerShell
43 lines
1.4 KiB
PowerShell
$ErrorActionPreference = "Stop"
|
|
|
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$pidFile = Join-Path $scriptDir "metrics-ble.pid"
|
|
$pushScript = Join-Path $scriptDir "push-metrics-ble.py"
|
|
|
|
function Get-MetricsProcesses {
|
|
Get-CimInstance Win32_Process -Filter "name='python.exe'" |
|
|
Where-Object { $_.CommandLine -and $_.CommandLine.Contains($pushScript) }
|
|
}
|
|
|
|
$processIds = @()
|
|
if (Test-Path $pidFile) {
|
|
$pidValue = Get-Content $pidFile -ErrorAction SilentlyContinue
|
|
if ($pidValue) {
|
|
$processIds += [int]$pidValue
|
|
}
|
|
}
|
|
|
|
$processIds += @(Get-MetricsProcesses | Select-Object -ExpandProperty ProcessId)
|
|
$processIds = @($processIds | Sort-Object -Unique)
|
|
|
|
if ($processIds.Count -eq 0) {
|
|
Write-Host "BLE metrics stream is not running."
|
|
Remove-Item -LiteralPath $pidFile -Force -ErrorAction SilentlyContinue
|
|
exit 0
|
|
}
|
|
|
|
$children = Get-CimInstance Win32_Process |
|
|
Where-Object { $processIds -contains $_.ParentProcessId } |
|
|
Select-Object -ExpandProperty ProcessId
|
|
|
|
foreach ($childId in @($children | Sort-Object -Unique)) {
|
|
Stop-Process -Id $childId -Force -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
foreach ($processId in $processIds) {
|
|
Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
|
|
Write-Host "BLE metrics stream stopped. PID: $processId"
|
|
}
|
|
|
|
Remove-Item -LiteralPath $pidFile -Force -ErrorAction SilentlyContinue
|