zoukankan      html  css  js  c++  java
  • Powershell 检测设备管理器

    Get-WmiObject -Class Win32_PNPEntity 获取设备信息

    下图为其中一个设备的详细信息

    Name存在且 PNPClass 为 $Null 的设备为其他设备(Other Devices)

    Status 为 "Error" 的设备为未正常工作设备(Yellow Bang)

    PNPClass 表示设备所属类

    通过比较数据的改变来检测设备的变化

    function CatchChange {
        Write-Host("Listening Device Change")
        $device=Get-WmiObject -Class Win32_PNPEntity
        $device_num=$device.Length
        while ($device_num -eq((Get-WmiObject -Class Win32_PNPEntity).Length)){
    
        }
        $device_Change=Get-WmiObject -Class Win32_PNPEntity
        $device_Compare=Compare-Object $device $device_Change
        Write-Host($device_Compare.InputObject.Name) 
    }
    

    检测设备管理器,监听设备变化

    # 检测Device Manager 
    # 0     Other Devices
    # 1     Error Devices (Yellow Bang)
    # 2     Camera Number
    
    function GetDevStatus {
        $Device_other = @()
        $Device_yellowbang = @()
        $CameraNum=0
        $All_List = Get-WmiObject -Class Win32_PNPEntity
        foreach ($i in $All_List) {
            if ($Null -ne $i.Name  -and $Null -eq $i.PNPClass) {
                $Device_other += ($i.Name)
            }
            if ($i.Status -eq "Error") {
                $Device_yellowbang += ($i.Name)
            }
            if ($i.PNPClass -eq "Camera"){
                $CameraNum+=1
            }
        }
        return $Device_other, $Device_yellowbang,$CameraNum
    }
    
    function CatchChange {
        Write-Host("************Listening Device Change************")
        $device=Get-WmiObject -Class Win32_PNPEntity
        $device_num=$device.Length
        while ($device_num -eq((Get-WmiObject -Class Win32_PNPEntity).Length)){
    
        }
        if ($device_num -gt((Get-WmiObject -Class Win32_PNPEntity).Length)){
            Write-Host("Remove Device")
        }
        else{
            Write-Host("Add Device")
        }
        $device_Change=Get-WmiObject -Class Win32_PNPEntity
        $device_Compare=Compare-Object $device $device_Change
        Write-Host($device_Compare.InputObject.Name) 
    }
    
    # CatchChange
    $a = GetDevStatus
     
    if ($Null -eq $a) {
        Write-Host("No Error")
    }
    else {
        Write-Host("************Other Devices************")
        $a[0]
        Write-Host("")
        Write-Host("************YellowBang Devices************")
        $a[1]
        Write-Host("")
        Write-Host("************Camera Num************")
        $a[2]
    }
    while ($True){
        CatchChange
    }
    
    
  • 相关阅读:
    如何成为伟大的程序员
    程序员如何增加收入
    一个阿里巴巴码农的六年回眸
    效仿盖茨:PPstream创始人的心路历程
    程序员的工作环境与效率
    软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比
    技术人员创业后就不再适合继续编码了
    互联网行业持续交付的经验
    11 款用于优化、分析源代码的Java工具
    常用 Java 静态代码分析工具的分析与比较
  • 原文地址:https://www.cnblogs.com/jyang/p/13039701.html
Copyright © 2011-2022 走看看