import subprocess

def run_powershell_command(cmd):
    try:
        completed = subprocess.run(
            ["powershell", "-Command", cmd],
            capture_output=True, text=True, check=True
        )
        return completed.stdout.strip()
    except subprocess.CalledProcessError as e:
        return f"Ошибка выполнения PowerShell команды: {e}"

def check_devices():
    device_queries = {
        "Процессоры": "Get-CimInstance Win32_Processor | Format-List",
        "Жесткие диски": "Get-CimInstance Win32_DiskDrive | Format-List",
        "Сетевые адаптеры": "Get-CimInstance Win32_NetworkAdapter | Where-Object {$_.NetEnabled -eq $true} | Format-List",
        "Видеоадаптеры": "Get-CimInstance Win32_VideoController | Format-List",
    }

    for device_name, ps_cmd in device_queries.items():
        print(f"--- {device_name} ---")
        output = run_powershell_command(ps_cmd)
        print(output)
        print("\n")

    # Проверка устройств с ошибками драйверов
    print("--- Устройства с ошибками драйверов ---")
    error_check_cmd = (
        "Get-CimInstance Win32_PnPEntity | "
        "Where-Object { $_.ConfigManagerErrorCode -ne 0 } | "
        "Select-Object Name, ConfigManagerErrorCode, Status | Format-List"
    )
    error_output = run_powershell_command(error_check_cmd)
    if error_output:
        print(error_output)
    else:
        print("Ошибок драйверов не обнаружено.")
    print("\n")

if __name__ == "__main__":
    check_devices()
