Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #wew lad
  2.  
  3. $setupapi = @"
  4. using System;
  5. using System.Diagnostics;
  6. using System.Text;
  7. using System.Runtime.InteropServices;
  8. namespace Win32
  9. {
  10.    public static class SetupApi
  11.    {
  12.        [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
  13.        public static extern IntPtr SetupDiGetClassDevs(
  14.           ref Guid ClassGuid,
  15.           IntPtr Enumerator,
  16.           IntPtr hwndParent,
  17.           int Flags
  18.        );
  19.    
  20.        [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
  21.        public static extern IntPtr SetupDiGetClassDevs(
  22.           IntPtr ClassGuid,
  23.           string Enumerator,
  24.           IntPtr hwndParent,
  25.           int Flags
  26.        );
  27.  
  28.        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  29.        public static extern bool SetupDiEnumDeviceInfo(
  30.            IntPtr DeviceInfoSet,
  31.            uint MemberIndex,
  32.            ref SP_DEVINFO_DATA DeviceInfoData
  33.        );
  34.  
  35.        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  36.        public static extern bool SetupDiCallClassInstaller(
  37.            DiFunction installFunction,
  38.            //ref SP_DEVINFO_DATA deviceInfoData,
  39.            ref IntPtr DeviceInfoSet
  40.        );
  41.  
  42.        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  43.        public static extern bool SetupDiSetClassInstallParams(
  44.            IntPtr DeviceInfoSet,
  45.            ref SP_DEVINFO_DATA DeviceInfoData,
  46.            ref DeviceEnableParameters classInstallParams,
  47.            int classInstallParamsSize
  48.        );
  49.  
  50.        [DllImport("setupapi.dll", SetLastError = true)]
  51.        public static extern bool SetupDiDestroyDeviceInfoList(
  52.            IntPtr DeviceInfoSet
  53.        );
  54.  
  55.        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  56.        public static extern bool SetupDiGetDeviceRegistryProperty(
  57.            IntPtr deviceInfoSet,
  58.            ref SP_DEVINFO_DATA deviceInfoData,
  59.            uint property,
  60.            out UInt32 propertyRegDataType,
  61.            byte[] propertyBuffer,
  62.            uint propertyBufferSize,
  63.            out UInt32 requiredSize
  64.        );
  65.        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
  66.        public static extern bool SetupDiGetDeviceInstanceId(
  67.            IntPtr DeviceInfoSet,
  68.            ref SP_DEVINFO_DATA DeviceInfoData,
  69.            StringBuilder DeviceInstanceId,
  70.            int DeviceInstanceIdSize,
  71.            out int RequiredSize
  72.        );
  73.    }
  74.  
  75.    [StructLayout(LayoutKind.Sequential)]
  76.    public struct SP_DEVINFO_DATA
  77.    {
  78.       public uint cbSize;
  79.       public Guid classGuid;
  80.       public uint devInst;
  81.       public IntPtr reserved;
  82.    }
  83.  
  84.    public enum DiFunction
  85.    {
  86.        SelectDevice                  = 1,
  87.        InstallDevice                 = 2,
  88.        AssignResources               = 3,
  89.        Properties                    = 4,
  90.        Remove                        = 5,
  91.        FirstTimeSetup                = 6,
  92.        FoundDevice                   = 7,
  93.        SelectClassDrivers            = 8,
  94.        ValidateClassDrivers          = 9,
  95.        InstallClassDrivers           = (int)0xa,
  96.        CalcDiskSpace                 = (int)0xb,
  97.        DestroyPrivateData            = (int)0xc,
  98.        ValidateDriver                = (int)0xd,
  99.        Detect                        = (int)0xf,
  100.        InstallWizard                 = (int)0x10,
  101.        DestroyWizardData             = (int)0x11,
  102.        PropertyChange                = (int)0x12,
  103.        EnableClass                   = (int)0x13,
  104.        DetectVerify                  = (int)0x14,
  105.        InstallDeviceFiles            = (int)0x15,
  106.        UnRemove                      = (int)0x16,
  107.        SelectBestCompatDrv           = (int)0x17,
  108.        AllowInstall                  = (int)0x18,
  109.        RegisterDevice                = (int)0x19,
  110.        NewDeviceWizardPreSelect      = (int)0x1a,
  111.        NewDeviceWizardSelect         = (int)0x1b,
  112.        NewDeviceWizardPreAnalyze     = (int)0x1c,
  113.        NewDeviceWizardPostAnalyze    = (int)0x1d,
  114.        NewDeviceWizardFinishInstall  = (int)0x1e,
  115.        Unused1                       = (int)0x1f,
  116.        InstallInterfaces             = (int)0x20,
  117.        DetectCancel                  = (int)0x21,
  118.        RegisterCoInstallers          = (int)0x22,
  119.        AddPropertyPageAdvanced       = (int)0x23,
  120.        AddPropertyPageBasic          = (int)0x24,
  121.        Reserved1                     = (int)0x25,
  122.        Troubleshooter                = (int)0x26,
  123.        PowerMessageWake              = (int)0x27,
  124.        AddRemotePropertyPageAdvanced = (int)0x28,
  125.        UpdateDriverUI                = (int)0x29,
  126.        Reserved2                     = (int)0x30
  127.    }
  128.  
  129.    public enum StateChangeAction
  130.    {
  131.        Enable     = 1,
  132.        Disable    = 2,
  133.        PropChange = 3,
  134.        Start      = 4,
  135.        Stop       = 5
  136.    }
  137.  
  138.    [Flags()]
  139.    public enum Scopes
  140.    {
  141.        Global         = 1,
  142.        ConfigSpecific = 2,
  143.        ConfigGeneral  = 4
  144.    }
  145.  
  146.    [StructLayout(LayoutKind.Sequential)]
  147.    public struct DeviceEnableParameters
  148.    {
  149.        public int Size;
  150.        public DiFunction DiFunction;
  151.        public StateChangeAction StateChange;
  152.        public Scopes Scope;
  153.        public int HwProfile;
  154.    }
  155.  
  156.    [Flags]
  157.    public enum DiGetClassFlags : uint
  158.    {
  159.        DIGCF_DEFAULT           = 0x00000001,  // only valid with DIGCF_DEVICEINTERFACE
  160.        DIGCF_PRESENT           = 0x00000002,
  161.        DIGCF_ALLCLASSES        = 0x00000004,
  162.        DIGCF_PROFILE           = 0x00000008,
  163.        DIGCF_DEVICEINTERFACE   = 0x00000010,
  164.    }
  165.  
  166.    public enum SetupDiGetDeviceRegistryPropertyEnum : uint
  167.    {
  168.         SPDRP_DEVICEDESC                  = 0x00000000, // DeviceDesc (R/W)
  169.         SPDRP_HARDWAREID                  = 0x00000001, // HardwareID (R/W)
  170.         SPDRP_COMPATIBLEIDS               = 0x00000002, // CompatibleIDs (R/W)
  171.         SPDRP_UNUSED0                     = 0x00000003, // unused
  172.         SPDRP_SERVICE                     = 0x00000004, // Service (R/W)
  173.         SPDRP_UNUSED1                     = 0x00000005, // unused
  174.         SPDRP_UNUSED2                     = 0x00000006, // unused
  175.         SPDRP_CLASS                       = 0x00000007, // Class (R--tied to ClassGUID)
  176.         SPDRP_CLASSGUID                   = 0x00000008, // ClassGUID (R/W)
  177.         SPDRP_DRIVER                      = 0x00000009, // Driver (R/W)
  178.         SPDRP_CONFIGFLAGS                 = 0x0000000A, // ConfigFlags (R/W)
  179.         SPDRP_MFG                         = 0x0000000B, // Mfg (R/W)
  180.         SPDRP_FRIENDLYNAME                = 0x0000000C, // FriendlyName (R/W)
  181.         SPDRP_LOCATION_INFORMATION        = 0x0000000D, // LocationInformation (R/W)
  182.         SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E, // PhysicalDeviceObjectName (R)
  183.         SPDRP_CAPABILITIES                = 0x0000000F, // Capabilities (R)
  184.         SPDRP_UI_NUMBER                   = 0x00000010, // UiNumber (R)
  185.         SPDRP_UPPERFILTERS                = 0x00000011, // UpperFilters (R/W)
  186.         SPDRP_LOWERFILTERS                = 0x00000012, // LowerFilters (R/W)
  187.         SPDRP_BUSTYPEGUID                 = 0x00000013, // BusTypeGUID (R)
  188.         SPDRP_LEGACYBUSTYPE               = 0x00000014, // LegacyBusType (R)
  189.         SPDRP_BUSNUMBER                   = 0x00000015, // BusNumber (R)
  190.         SPDRP_ENUMERATOR_NAME             = 0x00000016, // Enumerator Name (R)
  191.         SPDRP_SECURITY                    = 0x00000017, // Security (R/W, binary form)
  192.         SPDRP_SECURITY_SDS                = 0x00000018, // Security (W, SDS form)
  193.         SPDRP_DEVTYPE                     = 0x00000019, // Device Type (R/W)
  194.         SPDRP_EXCLUSIVE                   = 0x0000001A, // Device is exclusive-access (R/W)
  195.         SPDRP_CHARACTERISTICS             = 0x0000001B, // Device Characteristics (R/W)
  196.         SPDRP_ADDRESS                     = 0x0000001C, // Device Address (R)
  197.         SPDRP_UI_NUMBER_DESC_FORMAT       = 0X0000001D, // UiNumberDescFormat (R/W)
  198.         SPDRP_DEVICE_POWER_DATA           = 0x0000001E, // Device Power Data (R)
  199.         SPDRP_REMOVAL_POLICY              = 0x0000001F, // Removal Policy (R)
  200.         SPDRP_REMOVAL_POLICY_HW_DEFAULT   = 0x00000020, // Hardware Removal Policy (R)
  201.         SPDRP_REMOVAL_POLICY_OVERRIDE     = 0x00000021, // Removal Policy Override (RW)
  202.         SPDRP_INSTALL_STATE               = 0x00000022, // Device Install State (R)
  203.         SPDRP_LOCATION_PATHS              = 0x00000023, // Device Location Paths (R)
  204.         SPDRP_BASE_CONTAINERID            = 0x00000024  // Base ContainerID (R)
  205.    }
  206. }
  207. "@
  208. Add-Type -TypeDefinition $setupapi
  209.  
  210. Function Set-DeviceEnableState {
  211.     Param(
  212.         [GUID]$ClassGUID,
  213.         [string]$InstanceID,
  214.         [Boolean]$Enabled
  215.     )
  216.     $array                     = @()
  217.     $devs                      = [Win32.SetupApi]::SetupDiGetClassDevs([ref]$ClassGUID, [IntPtr]::Zero, [IntPtr]::Zero, [Win32.DiGetClassFlags]::DIGCF_ALLCLASSES)
  218.     $devInfo                   = new-object Win32.SP_DEVINFO_DATA
  219.     $devInfo.cbSize            = [System.Runtime.InteropServices.Marshal]::SizeOf($devInfo)
  220.     $devCount                  = 0
  221.     $installParams             = New-Object -Typename Win32.DeviceEnableParameters
  222.     $installParams.Size        = 8
  223.     $installParams.DiFunction  = [Win32.DiFunction]::PropertyChange
  224.     $installParams.Scope       = [Win32.Scopes]::Global
  225.     if ($Enabled -eq $True) {
  226.         $installParams.StateChange = [Win32.StateChangeAction]::Enable
  227.     }
  228.     else {
  229.         $installParams.StateChange = [Win32.StateChangeAction]::Disable
  230.     }
  231.     $installParamSize       = [System.Runtime.InteropServices.Marshal]::SizeOf($installParams)
  232.     $installParamDiFunction = [Win32.DiFunction]::PropertyChange
  233.  
  234.     #Enumerate Devices in class
  235.     while([Win32.SetupApi]::SetupDiEnumDeviceInfo($devs, $devCount, [ref]$devInfo)){
  236.  
  237.         #Read device instance ID.
  238.         $InstIDStringBuilder          = New-Object -TypeName "System.Text.StringBuilder"
  239.         $InstIDStringBuilder.Capacity = 5000
  240.         $reqSize                      = 0
  241.         $Result = [Win32.SetupAPI]::SetupDiGetDeviceInstanceId($devs, [ref]$devInfo, $InstIDStringBuilder, $InstIDStringBuilder.Capacity, [ref]$reqSize);
  242.         if (($Result -eq $False) -and ($reqSize -gt 0)) {
  243.             $sb.Capacity -eq $reqSize
  244.             $Result = [Win32.SetupAPI]::SetupDiGetDeviceInstanceId($devs, [ref]$devInfo, $InstIDStringBuilder, $InstIDStringBuilder.Capacity, [ref]$reqSize);
  245.         }
  246.  
  247.         $InstIDString = $InstIDStringBuilder.ToString()
  248.         if ($InstIDString -eq $InstanceID) {
  249.             [Win32.SetupApi]::SetupDiSetClassInstallParams($devs, [ref]$devInfo, [ref]$installParams, $installParamSize);
  250.             #This line works, returns True.
  251.             [Win32.SetupApi]::SetupDiCallClassInstaller($installParamDiFunction, [ref]$devs);
  252.             #This line works, returns False (ie: failed).
  253.             #GetLastWin23Error() returns 203.
  254.         }
  255.  
  256.         $devcount += 1
  257.     }
  258. }
  259.  
  260. Set-DeviceEnableState -ClassGUID "4d36e96f-e325-11ce-bfc1-08002be10318" -InstanceID "HID\VID_046D&PID_C52B&MI_01&COL01\8&53437E0&0&0000" -Enabled $False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement