Advertisement
Guest User

Untitled

a guest
Aug 11th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $removeDevices = $true
  2. $setupapi = @"
  3. using System;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using System.Runtime.InteropServices;
  7. namespace Win32
  8. {
  9.     public static class SetupApi
  10.     {
  11.         [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
  12.         public static extern IntPtr SetupDiGetClassDevs(
  13.         ref Guid ClassGuid,
  14.         IntPtr Enumerator,
  15.         IntPtr hwndParent,
  16.         int Flags
  17.         );
  18.  
  19.         [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
  20.         public static extern IntPtr SetupDiGetClassDevs(
  21.         IntPtr ClassGuid,
  22.         string Enumerator,
  23.         IntPtr hwndParent,
  24.         int Flags
  25.         );
  26.  
  27.         [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  28.         public static extern bool SetupDiEnumDeviceInfo(
  29.         IntPtr DeviceInfoSet,
  30.         uint MemberIndex,
  31.         ref SP_DEVINFO_DATA DeviceInfoData
  32.         );
  33.  
  34.         [DllImport("setupapi.dll", SetLastError = true)]
  35.         public static extern bool SetupDiDestroyDeviceInfoList(
  36.         IntPtr DeviceInfoSet
  37.         );
  38.         [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  39.         public static extern bool SetupDiGetDeviceRegistryProperty(
  40.         IntPtr deviceInfoSet,
  41.         ref SP_DEVINFO_DATA deviceInfoData,
  42.         uint property,
  43.         out UInt32 propertyRegDataType,
  44.         byte[] propertyBuffer,
  45.         uint propertyBufferSize,
  46.         out UInt32 requiredSize
  47.         );
  48.         [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
  49.         public static extern bool SetupDiGetDeviceInstanceId(
  50.         IntPtr DeviceInfoSet,
  51.         ref SP_DEVINFO_DATA DeviceInfoData,
  52.         StringBuilder DeviceInstanceId,
  53.         int DeviceInstanceIdSize,
  54.         out int RequiredSize
  55.         );
  56.  
  57.         [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  58.         public static extern bool SetupDiRemoveDevice(IntPtr DeviceInfoSet,ref SP_DEVINFO_DATA DeviceInfoData);
  59.     }
  60.     [StructLayout(LayoutKind.Sequential)]
  61.     public struct SP_DEVINFO_DATA
  62.     {
  63.         public uint cbSize;
  64.         public Guid classGuid;
  65.         public uint devInst;
  66.         public IntPtr reserved;
  67.    }
  68.     [Flags]
  69.     public enum DiGetClassFlags : uint
  70.     {
  71.         DIGCF_DEFAULT       = 0x00000001,  // only valid with DIGCF_DEVICEINTERFACE
  72.         DIGCF_PRESENT       = 0x00000002,
  73.         DIGCF_ALLCLASSES    = 0x00000004,
  74.         DIGCF_PROFILE       = 0x00000008,
  75.         DIGCF_DEVICEINTERFACE   = 0x00000010,
  76.    }
  77.     public enum SetupDiGetDeviceRegistryPropertyEnum : uint
  78.     {
  79.         SPDRP_DEVICEDESC         = 0x00000000, // DeviceDesc (R/W)
  80.         SPDRP_HARDWAREID         = 0x00000001, // HardwareID (R/W)
  81.         SPDRP_COMPATIBLEIDS       = 0x00000002, // CompatibleIDs (R/W)
  82.         SPDRP_UNUSED0           = 0x00000003, // unused
  83.         SPDRP_SERVICE           = 0x00000004, // Service (R/W)
  84.         SPDRP_UNUSED1           = 0x00000005, // unused
  85.         SPDRP_UNUSED2           = 0x00000006, // unused
  86.         SPDRP_CLASS           = 0x00000007, // Class (R--tied to ClassGUID)
  87.         SPDRP_CLASSGUID       = 0x00000008, // ClassGUID (R/W)
  88.         SPDRP_DRIVER             = 0x00000009, // Driver (R/W)
  89.         SPDRP_CONFIGFLAGS       = 0x0000000A, // ConfigFlags (R/W)
  90.         SPDRP_MFG           = 0x0000000B, // Mfg (R/W)
  91.         SPDRP_FRIENDLYNAME  = 0x0000000C, // FriendlyName (R/W)
  92.         SPDRP_LOCATION_INFORMATION    = 0x0000000D, // LocationInformation (R/W)
  93.         SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E, // PhysicalDeviceObjectName (R)
  94.         SPDRP_CAPABILITIES  = 0x0000000F, // Capabilities (R)
  95.         SPDRP_UI_NUMBER       = 0x00000010, // UiNumber (R)
  96.         SPDRP_UPPERFILTERS  = 0x00000011, // UpperFilters (R/W)
  97.         SPDRP_LOWERFILTERS  = 0x00000012, // LowerFilters (R/W)
  98.         SPDRP_BUSTYPEGUID       = 0x00000013, // BusTypeGUID (R)
  99.         SPDRP_LEGACYBUSTYPE       = 0x00000014, // LegacyBusType (R)
  100.         SPDRP_BUSNUMBER       = 0x00000015, // BusNumber (R)
  101.         SPDRP_ENUMERATOR_NAME       = 0x00000016, // Enumerator Name (R)
  102.         SPDRP_SECURITY      = 0x00000017, // Security (R/W, binary form)
  103.         SPDRP_SECURITY_SDS  = 0x00000018, // Security (W, SDS form)
  104.         SPDRP_DEVTYPE           = 0x00000019, // Device Type (R/W)
  105.         SPDRP_EXCLUSIVE       = 0x0000001A, // Device is exclusive-access (R/W)
  106.         SPDRP_CHARACTERISTICS       = 0x0000001B, // Device Characteristics (R/W)
  107.         SPDRP_ADDRESS           = 0x0000001C, // Device Address (R)
  108.         SPDRP_UI_NUMBER_DESC_FORMAT       = 0X0000001D, // UiNumberDescFormat (R/W)
  109.         SPDRP_DEVICE_POWER_DATA       = 0x0000001E, // Device Power Data (R)
  110.         SPDRP_REMOVAL_POLICY         = 0x0000001F, // Removal Policy (R)
  111.         SPDRP_REMOVAL_POLICY_HW_DEFAULT   = 0x00000020, // Hardware Removal Policy (R)
  112.         SPDRP_REMOVAL_POLICY_OVERRIDE     = 0x00000021, // Removal Policy Override (RW)
  113.         SPDRP_INSTALL_STATE       = 0x00000022, // Device Install State (R)
  114.         SPDRP_LOCATION_PATHS         = 0x00000023, // Device Location Paths (R)
  115.         SPDRP_BASE_CONTAINERID  = 0x00000024  // Base ContainerID (R)
  116.     }
  117. }
  118. "@
  119. Add-Type -TypeDefinition $setupapi
  120.  
  121. $removeArray = @()
  122. $array = @()
  123. $setupClass = [Guid]::Empty
  124. $devs = [Win32.SetupApi]::SetupDiGetClassDevs([ref]$setupClass, [IntPtr]::Zero, [IntPtr]::Zero, [Win32.DiGetClassFlags]::DIGCF_ALLCLASSES)
  125. $devInfo = new-object Win32.SP_DEVINFO_DATA
  126. $devInfo.cbSize = [System.Runtime.InteropServices.Marshal]::SizeOf($devInfo)
  127. $devCount = 0
  128. While ([Win32.SetupApi]::SetupDiEnumDeviceInfo($devs, $devCount, [ref]$devInfo))
  129. {
  130.     $propType = 0
  131.     [byte[]]$propBuffer = $null
  132.     $propBufferSize = 0
  133.     [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_FRIENDLYNAME, [ref]$propType, $propBuffer, 0, [ref]$propBufferSize) | Out-Null
  134.     [byte[]]$propBuffer = New-Object byte[] $propBufferSize
  135.  
  136.     $propTypeHWID = 0
  137.     [byte[]]$propBufferHWID = $null
  138.     $propBufferSizeHWID = 0
  139.     [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_HARDWAREID, [ref]$propTypeHWID, $propBufferHWID, 0, [ref]$propBufferSizeHWID) | Out-Null
  140.     [byte[]]$propBufferHWID = New-Object byte[] $propBufferSizeHWID
  141.  
  142.     $propTypeDD = 0
  143.     [byte[]]$propBufferDD = $null
  144.     $propBufferSizeDD = 0
  145.     [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_DEVICEDESC, [ref]$propTypeDD, $propBufferDD, 0, [ref]$propBufferSizeDD) | Out-Null
  146.     [byte[]]$propBufferDD = New-Object byte[] $propBufferSizeDD
  147.  
  148.     $propTypeIS = 0
  149.     [byte[]]$propBufferIS = $null
  150.     $propBufferSizeIS = 0
  151.     [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_INSTALL_STATE, [ref]$propTypeIS, $propBufferIS, 0, [ref]$propBufferSizeIS) | Out-Null
  152.     [byte[]]$propBufferIS = New-Object byte[] $propBufferSizeIS
  153.  
  154.     $propTypeCLSS = 0
  155.     [byte[]]$propBufferCLSS = $null
  156.     $propBufferSizeCLSS = 0
  157.     [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo, [Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_CLASS, [ref]$propTypeCLSS, $propBufferCLSS, 0, [ref]$propBufferSizeCLSS) | Out-Null
  158.     [byte[]]$propBufferCLSS = New-Object byte[] $propBufferSizeCLSS
  159.     [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_CLASS, [ref]$propTypeCLSS, $propBufferCLSS, $propBufferSizeCLSS, [ref]$propBufferSizeCLSS) | Out-Null
  160.     $Class = [System.Text.Encoding]::Unicode.GetString($propBufferCLSS)
  161.  
  162.     IF(![Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_FRIENDLYNAME, [ref]$propType, $propBuffer, $propBufferSize, [ref]$propBufferSize))
  163.     {
  164.         [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_DEVICEDESC, [ref]$propTypeDD, $propBufferDD, $propBufferSizeDD, [ref]$propBufferSizeDD) | Out-Null
  165.         $FriendlyName = [System.Text.Encoding]::Unicode.GetString($propBufferDD)
  166.         IF ($FriendlyName.Length -ge 1)
  167.         {
  168.             $FriendlyName = $FriendlyName.Substring(0,$FriendlyName.Length-1)
  169.         }
  170.     }
  171.     Else
  172.     {
  173.         $FriendlyName = [System.Text.Encoding]::Unicode.GetString($propBuffer)
  174.         IF ($FriendlyName.Length -ge 1)
  175.         {
  176.             $FriendlyName = $FriendlyName.Substring(0,$FriendlyName.Length-1)
  177.         }
  178.     }
  179.  
  180.     $InstallState = [Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_INSTALL_STATE, [ref]$propTypeIS, $propBufferIS, $propBufferSizeIS, [ref]$propBufferSizeIS)
  181.  
  182.     IF(![Win32.SetupApi]::SetupDiGetDeviceRegistryProperty($devs, [ref]$devInfo,[Win32.SetupDiGetDeviceRegistryPropertyEnum]::SPDRP_HARDWAREID, [ref]$propTypeHWID, $propBufferHWID, $propBufferSizeHWID, [ref]$propBufferSizeHWID))
  183.     {
  184.         $HWID = ""
  185.     }
  186.     Else
  187.     {
  188.         $HWID = [System.Text.Encoding]::Unicode.GetString($propBufferHWID)
  189.         $HWID = $HWID.split([char]0x0000)[0].ToUpper()
  190.     }
  191.  
  192.     $obj = New-Object System.Object
  193.     $obj | Add-Member -type NoteProperty -name FriendlyName -value $FriendlyName
  194.     $obj | Add-Member -type NoteProperty -name HWID -value $HWID
  195.     $obj | Add-Member -type NoteProperty -name InstallState -value $InstallState
  196.     $obj | Add-Member -type NoteProperty -name Class -value $Class
  197.     IF ($array.count -le 0)
  198.     {
  199.         sleep 1
  200.     }
  201.     $array += @($obj)
  202.    
  203.     $matchFilter = $false
  204.     IF ($removeDevices -eq $true)
  205.     {
  206.         IF ($InstallState -eq $False)
  207.         {
  208.             IF ($matchFilter -eq $false)
  209.             {
  210.                 Write-Host "Attempting to removing device $FriendlyName" -ForegroundColor Yellow
  211.                 $removeObj = New-Object System.Object
  212.                 $removeObj | Add-Member -type NoteProperty -name FriendlyName -value $FriendlyName
  213.                 $removeObj | Add-Member -type NoteProperty -name HWID -value $HWID
  214.                 $removeObj | Add-Member -type NoteProperty -name InstallState -value $InstallState
  215.                 $removeObj | Add-Member -type NoteProperty -name Class -value $Class
  216.                 $removeArray += @($removeObj)
  217.                 IF([Win32.SetupApi]::SetupDiRemoveDevice($devs, [ref]$devInfo))
  218.                 {
  219.                     Write-Host "Removed device $FriendlyName"  -ForegroundColor Green
  220.                 }
  221.                 Else
  222.                 {
  223.                     Write-Host "Failed to remove device $FriendlyName" -ForegroundColor Red
  224.                 }
  225.             }
  226.             Else
  227.             {
  228.                 Write-Host "Filter matched. Skipping $FriendlyName" -ForegroundColor Yellow
  229.             }
  230.         }
  231.     }
  232.     $devcount++
  233. }
  234.  
  235. IF ($removeDevices -eq $true)
  236. {
  237.     Write-Host "Removed devices:"
  238.     $removeArray  | sort -Property FriendlyName | ft
  239.     Write-Host "Total removed devices: $($removeArray.count)"
  240.     return $removeArray | Out-Null
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement