Загрузка данных


$log = Join-Path $PSScriptRoot "usb-watch.log"

function Write-Log($msg) {
    $line = "{0} {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $msg
    Add-Content -Path $log -Value $line
    Write-Host $line
}

Write-Log "Watcher started"

Register-WmiEvent -Class Win32_DeviceChangeEvent -SourceIdentifier USBWatch | Out-Null

while ($true) {
    $evt = Wait-Event -SourceIdentifier USBWatch
    $null = Remove-Event -EventIdentifier $evt.EventIdentifier

    Write-Log "USB device change event"

    Start-Sleep -Seconds 2

    $devices = Get-PnpDevice | Where-Object {
        $_.FriendlyName -match 'Android|ADB|MTP|Unknown|USB' -or $_.InstanceId -match 'USB'
    }

    if ($devices) {
        Write-Log "Visible PnP devices:"
        $devices | Select-Object Status,Class,FriendlyName,InstanceId | ForEach-Object {
            Write-Log ("  {0} | {1} | {2} | {3}" -f $_.Status, $_.Class, $_.FriendlyName, $_.InstanceId)
        }
    } else {
        Write-Log "No new USB/PnP device visible"
    }

    $adb = & adb devices -l 2>$null
    if ($adb) {
        Write-Log "ADB output:"
        $adb | ForEach-Object { Write-Log ("  " + $_) }
    } else {
        Write-Log "ADB returned nothing"
    }
}