Powershell 檢測USB存儲設備


1.查看存儲設備信息

1.1 Win32_LogicalDisk 獲取本機邏輯磁盤(計算機中顯示的盤符 C: D: E:等),包括硬盤,光驅,U盤等設備

    Get-WmiObject -Class Win32_LogicalDisk

運行結果

DeviceID --> 盤符
DriveType --> 設備類型
VolumeName --> 卷標
                DeviceID     : C:
                DriveType    : 3
                ProviderName :
                FreeSpace    : 25028714496
                Size         : 232373874688
                VolumeName   : OS

                DeviceID     : D:
                DriveType    : 3
                ProviderName :
                FreeSpace    : 8898105344
                Size         : 9751752704
                VolumeName   : 新加卷

                DeviceID     : F:
                DriveType    : 5
                ProviderName :
                FreeSpace    :
                Size         :
                VolumeName   :

使用 Select * 查看全部信息

eg.

$a=Get-WmiObject -Class Win32_LogicalDisk
$a[0] | Select *

1.2 Win32_DiskDrive 獲取本機物理磁盤信息

Get-WmiObject -Class Win32_DiskDrive

1.3 Win32_PnPEntity獲取本機USB設備信息

Get-WmiObject -Class Win32_PnPEntity | Where-Object { $_.DeviceID -like 'USBSTOR*' }

可以查看 Powershell 檢查設備管理器

Get-WmiObject -Class Win32_PnPEntity 可查本機所有設備

2. Compare-Object 比較差異

alias(別名): compare diff

$a=Compare-Object (1,2,3)  (1,2,4)
$a

InputObject SideIndicator
----------- -------------
          4 =>
          3 <=

3.監聽USB設備的插入和移除

# DriveType
# 2: USB Disk
# 3: HDD
# 5: ODD
 
$driverList=@{}  # HashTable  Key-Value
$driveType=@{2="USB_Disk";3="HDD";5="ODD"}   # HashTable  Key-Value
$ErrorActionPreference= 'silentlycontinue'   # 不顯示錯誤, global variable

Function Get_DiskChange(){
    $Device_Logical=Get-WmiObject -Class Win32_LogicalDisk
    $Device_Logical_num = $Device_Logical.Length
    $Device_Physical =  Get-WmiObject -Class Win32_DiskDrive 
    $Device_Physical_num = $Device_Physical.Length    
    while($Device_Logical_num -eq (Get-WmiObject -Class Win32_LogicalDisk).Length)
    {   
        # Start-Sleep -s 1  
        # Listen  Disk Change
    }
    $Now_Time=Get-Date -Format "yyyy/MM/dd HH:mm:ss"
    $Dev_Change=Get-WmiObject -Class Win32_LogicalDisk  # 獲取邏輯分區
    $com_Dev=Compare-Object $Device_Logical $Dev_Change  #比較兩次差異
    $Disk_Change=$com_Dev.InputObject
    $diskName=$Disk_Change.DeviceID[0]
    
    $DevPhy_Change = Get-WmiObject -Class Win32_DiskDrive  #獲取物理存儲設備
    $com_DevPhy=Compare-Object $Device_Physical $DevPhy_Change  #比較差異
    $DiskPhy_Change=$com_DevPhy.InputObject 
    $DiskPhy_Model=$DiskPhy_Change.Model  #eg. Model      : USB DISK 3.0 USB Device
    $par="\d\.\d"    # 正則匹配  數字.數字  eg 3.0
    
    if ($DiskPhy_Model -match $par) {
        $DiskPhy_Model=$matches[0]   # 匹配到就賦值給$DiskPhy_Model
    }
    
    $DiskPhy_Size=[int]($DiskPhy_Change.Size /1000/1000/1000)   # 轉化為GB
    $type_num=$Disk_Change.DriveType
    $type=$driveType[[int]$type_num]
  
    if($Device_Logical_num -le (Get-WmiObject -Class Win32_LogicalDisk).Length){
    
        return ("Add_Device",$diskName,$type,$DiskPhy_Model,$DiskPhy_Size,$Now_Time)
    }
    elseif($Device_Logical_num -gt (Get-WmiObject -Class Win32_LogicalDisk).Length){
    
        return ("Remove Device",$diskName,$type,$DiskPhy_Model,$DiskPhy_Size,$Now_Time)
    }
}


Function DetectUSB(){
    echo "**********************  Listening  **********************"
    echo " "
    $a=Get_DiskChange
    echo "USB Device Information"
    echo " "
    "   time:          "+$a[5]
    "   Option:        "+$a[0]
    "   Volume label:  "+$a[1]
    "   Drive Type:    "+$a[2]
    "   Model:         "+$a[3]
    "   Total Size:    "+$a[4]+"GB"
    echo " "
    #函數中的所有輸出 都在$a 中
    # for($i=0;$i -le $a.Length;$i++){
        # $i.ToString() +":" +$a[$i]
    # }


    if ($a[0] -eq "Add_Device"){ 
        # &執行命令
        & ($a[1].ToString()+":")  # 進入盤符
        $CheckDisk = chkdsk #執行CHKDSK
        # $CheckDisk 
        foreach ($i in $CheckDisk){
            if ($i -eq "Windows has scanned the file system and found no problems."){
                echo ""
                echo "CHKDSK pass"
                echo ""
                break
                } 
            }
    }   
}

while ($TRUE){
    # Powershell 中 布爾值 $TRUE $FALSE 和空值 $NULL
    DetectUSB
    # Start-Sleep -s 1  
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM