Files
AndroidDeviceStreaming/deploy/windows/migrate-managed.ps1
T

73 lines
4.4 KiB
PowerShell

param(
[string]$Directory = (Join-Path $env:LOCALAPPDATA 'AndroidDeviceStreaming/agent'),
[string]$Id = 'home-pc',
[string]$Name = '家里电脑',
[string]$LegacyTask = 'ADS-Agent'
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$root = [IO.Path]::GetFullPath($Directory).TrimEnd('\')
$configPath = Join-Path $root 'agent.json'
$exe = Join-Path $root 'ads-agent.exe'
$ctl = Join-Path $root 'adsctl.exe'
$old = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json
if ($old.devices -or $old.id) { throw 'Already managed; use adsctl config commands instead' }
if (-not $old.device -or -not $old.target) { throw 'Expected legacy TCP phone configuration' }
$task = Get-ScheduledTask -TaskName $LegacyTask -ErrorAction Stop
if (-not @($task.Actions | Where-Object { $_.Arguments -and $_.Arguments.Contains($root) }).Count) { throw 'Legacy task does not belong to the requested Agent directory' }
if (Get-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue) { throw 'Managed task already exists' }
foreach ($file in @('ads-agent.exe.new', 'adsctl.exe.new')) {
if (-not (Test-Path -LiteralPath (Join-Path $root $file))) { throw "Missing staged $file" }
}
$backup = Join-Path $root ('backup-management-' + (Get-Date -Format yyyyMMdd-HHmmss))
New-Item -ItemType Directory -Path $backup | Out-Null
foreach ($file in @('agent.json','ads-agent.exe','adsctl.exe')) {
$path = Join-Path $root $file
if (Test-Path -LiteralPath $path) { Copy-Item -LiteralPath $path -Destination $backup }
}
Export-ScheduledTask -TaskName $LegacyTask | Set-Content -LiteralPath (Join-Path $backup 'legacy-task.xml') -Encoding utf8
$next = [ordered]@{version=1;id=$Id;name=$Name;server=$old.server;connect_address=$old.connect_address;adb_server='127.0.0.1:5037';devices=@(@{id=$old.device;target=$old.target;enabled=$true})}
try {
Disable-ScheduledTask -TaskName $LegacyTask | Out-Null
Stop-ScheduledTask -TaskName $LegacyTask
Get-CimInstance Win32_Process -Filter "Name='ads-agent.exe'" | Where-Object { $_.ExecutablePath -eq $exe } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force }
Start-Sleep -Seconds 1
Move-Item -LiteralPath (Join-Path $root 'ads-agent.exe.new') -Destination $exe -Force
Move-Item -LiteralPath (Join-Path $root 'adsctl.exe.new') -Destination $ctl -Force
$next | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $configPath -Encoding utf8NoBOM
& $ctl --config $configPath config validate --file $configPath
if ($LASTEXITCODE) { throw 'Managed config validation failed' }
# Elevated directory creation may set Administrators as owner. The pipe ACL
# follows this owner, so use the actual installing account rather than its group.
$directoryAcl = Get-Acl -LiteralPath $root
$directoryAcl.SetOwner([System.Security.Principal.WindowsIdentity]::GetCurrent().User)
Set-Acl -LiteralPath $root -AclObject $directoryAcl
& $ctl --config $configPath service install
if ($LASTEXITCODE) { throw 'Managed service installation failed' }
$ready=$false
for ($i=0; $i -lt 10; $i++) {
Start-Sleep -Seconds 1
$raw = & $ctl --json --config $configPath status 2>$null
if ($LASTEXITCODE -eq 0 -and ($raw | ConvertFrom-Json).id -eq $Id) { $ready=$true;break }
}
if (-not $ready) { throw 'Managed Agent did not become ready' }
Write-Output "Migration succeeded; backup: $backup"
& $ctl --json --config $configPath status
} catch {
$failure=$_
$managed=Get-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue
if ($managed -and @($managed.Actions | Where-Object { $_.Execute -eq $exe -and $_.Arguments.Contains($configPath) }).Count) {
Stop-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName "ADS-Agent-$Id" -Confirm:$false
}
Get-CimInstance Win32_Process -Filter "Name='ads-agent.exe'" | Where-Object { $_.ExecutablePath -eq $exe } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
Start-Sleep -Seconds 1
foreach ($file in @('agent.json','ads-agent.exe','adsctl.exe')) {
$saved=Join-Path $backup $file
if (Test-Path -LiteralPath $saved) { Copy-Item -LiteralPath $saved -Destination (Join-Path $root $file) -Force }
}
Enable-ScheduledTask -TaskName $LegacyTask | Out-Null
Start-ScheduledTask -TaskName $LegacyTask
throw $failure
}