Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #wew lad
- $setupapi = @"
- using System;
- using System.Diagnostics;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace Win32
- {
- public static class SetupApi
- {
- [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
- public static extern IntPtr SetupDiGetClassDevs(
- ref Guid ClassGuid,
- IntPtr Enumerator,
- IntPtr hwndParent,
- int Flags
- );
- [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
- public static extern IntPtr SetupDiGetClassDevs(
- IntPtr ClassGuid,
- string Enumerator,
- IntPtr hwndParent,
- int Flags
- );
- [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern bool SetupDiEnumDeviceInfo(
- IntPtr DeviceInfoSet,
- uint MemberIndex,
- ref SP_DEVINFO_DATA DeviceInfoData
- );
- [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern bool SetupDiCallClassInstaller(
- DiFunction installFunction,
- //ref SP_DEVINFO_DATA deviceInfoData,
- ref IntPtr DeviceInfoSet
- );
- [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern bool SetupDiSetClassInstallParams(
- IntPtr DeviceInfoSet,
- ref SP_DEVINFO_DATA DeviceInfoData,
- ref DeviceEnableParameters classInstallParams,
- int classInstallParamsSize
- );
- [DllImport("setupapi.dll", SetLastError = true)]
- public static extern bool SetupDiDestroyDeviceInfoList(
- IntPtr DeviceInfoSet
- );
- [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern bool SetupDiGetDeviceRegistryProperty(
- IntPtr deviceInfoSet,
- ref SP_DEVINFO_DATA deviceInfoData,
- uint property,
- out UInt32 propertyRegDataType,
- byte[] propertyBuffer,
- uint propertyBufferSize,
- out UInt32 requiredSize
- );
- [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
- public static extern bool SetupDiGetDeviceInstanceId(
- IntPtr DeviceInfoSet,
- ref SP_DEVINFO_DATA DeviceInfoData,
- StringBuilder DeviceInstanceId,
- int DeviceInstanceIdSize,
- out int RequiredSize
- );
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct SP_DEVINFO_DATA
- {
- public uint cbSize;
- public Guid classGuid;
- public uint devInst;
- public IntPtr reserved;
- }
- public enum DiFunction
- {
- SelectDevice = 1,
- InstallDevice = 2,
- AssignResources = 3,
- Properties = 4,
- Remove = 5,
- FirstTimeSetup = 6,
- FoundDevice = 7,
- SelectClassDrivers = 8,
- ValidateClassDrivers = 9,
- InstallClassDrivers = (int)0xa,
- CalcDiskSpace = (int)0xb,
- DestroyPrivateData = (int)0xc,
- ValidateDriver = (int)0xd,
- Detect = (int)0xf,
- InstallWizard = (int)0x10,
- DestroyWizardData = (int)0x11,
- PropertyChange = (int)0x12,
- EnableClass = (int)0x13,
- DetectVerify = (int)0x14,
- InstallDeviceFiles = (int)0x15,
- UnRemove = (int)0x16,
- SelectBestCompatDrv = (int)0x17,
- AllowInstall = (int)0x18,
- RegisterDevice = (int)0x19,
- NewDeviceWizardPreSelect = (int)0x1a,
- NewDeviceWizardSelect = (int)0x1b,
- NewDeviceWizardPreAnalyze = (int)0x1c,
- NewDeviceWizardPostAnalyze = (int)0x1d,
- NewDeviceWizardFinishInstall = (int)0x1e,
- Unused1 = (int)0x1f,
- InstallInterfaces = (int)0x20,
- DetectCancel = (int)0x21,
- RegisterCoInstallers = (int)0x22,
- AddPropertyPageAdvanced = (int)0x23,
- AddPropertyPageBasic = (int)0x24,
- Reserved1 = (int)0x25,
- Troubleshooter = (int)0x26,
- PowerMessageWake = (int)0x27,
- AddRemotePropertyPageAdvanced = (int)0x28,
- UpdateDriverUI = (int)0x29,
- Reserved2 = (int)0x30
- }
- public enum StateChangeAction
- {
- Enable = 1,
- Disable = 2,
- PropChange = 3,
- Start = 4,
- Stop = 5
- }
- [Flags()]
- public enum Scopes
- {
- Global = 1,
- ConfigSpecific = 2,
- ConfigGeneral = 4
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct DeviceEnableParameters
- {
- public int Size;
- public DiFunction DiFunction;
- public StateChangeAction StateChange;
- public Scopes Scope;
- public int HwProfile;
- }
- [Flags]
- public enum DiGetClassFlags : uint
- {
- DIGCF_DEFAULT = 0x00000001, // only valid with DIGCF_DEVICEINTERFACE
- DIGCF_PRESENT = 0x00000002,
- DIGCF_ALLCLASSES = 0x00000004,
- DIGCF_PROFILE = 0x00000008,
- DIGCF_DEVICEINTERFACE = 0x00000010,
- }
- public enum SetupDiGetDeviceRegistryPropertyEnum : uint
- {
- SPDRP_DEVICEDESC = 0x00000000, // DeviceDesc (R/W)
- SPDRP_HARDWAREID = 0x00000001, // HardwareID (R/W)
- SPDRP_COMPATIBLEIDS = 0x00000002, // CompatibleIDs (R/W)
- SPDRP_UNUSED0 = 0x00000003, // unused
- SPDRP_SERVICE = 0x00000004, // Service (R/W)
- SPDRP_UNUSED1 = 0x00000005, // unused
- SPDRP_UNUSED2 = 0x00000006, // unused
- SPDRP_CLASS = 0x00000007, // Class (R--tied to ClassGUID)
- SPDRP_CLASSGUID = 0x00000008, // ClassGUID (R/W)
- SPDRP_DRIVER = 0x00000009, // Driver (R/W)
- SPDRP_CONFIGFLAGS = 0x0000000A, // ConfigFlags (R/W)
- SPDRP_MFG = 0x0000000B, // Mfg (R/W)
- SPDRP_FRIENDLYNAME = 0x0000000C, // FriendlyName (R/W)
- SPDRP_LOCATION_INFORMATION = 0x0000000D, // LocationInformation (R/W)
- SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E, // PhysicalDeviceObjectName (R)
- SPDRP_CAPABILITIES = 0x0000000F, // Capabilities (R)
- SPDRP_UI_NUMBER = 0x00000010, // UiNumber (R)
- SPDRP_UPPERFILTERS = 0x00000011, // UpperFilters (R/W)
- SPDRP_LOWERFILTERS = 0x00000012, // LowerFilters (R/W)
- SPDRP_BUSTYPEGUID = 0x00000013, // BusTypeGUID (R)
- SPDRP_LEGACYBUSTYPE = 0x00000014, // LegacyBusType (R)
- SPDRP_BUSNUMBER = 0x00000015, // BusNumber (R)
- SPDRP_ENUMERATOR_NAME = 0x00000016, // Enumerator Name (R)
- SPDRP_SECURITY = 0x00000017, // Security (R/W, binary form)
- SPDRP_SECURITY_SDS = 0x00000018, // Security (W, SDS form)
- SPDRP_DEVTYPE = 0x00000019, // Device Type (R/W)
- SPDRP_EXCLUSIVE = 0x0000001A, // Device is exclusive-access (R/W)
- SPDRP_CHARACTERISTICS = 0x0000001B, // Device Characteristics (R/W)
- SPDRP_ADDRESS = 0x0000001C, // Device Address (R)
- SPDRP_UI_NUMBER_DESC_FORMAT = 0X0000001D, // UiNumberDescFormat (R/W)
- SPDRP_DEVICE_POWER_DATA = 0x0000001E, // Device Power Data (R)
- SPDRP_REMOVAL_POLICY = 0x0000001F, // Removal Policy (R)
- SPDRP_REMOVAL_POLICY_HW_DEFAULT = 0x00000020, // Hardware Removal Policy (R)
- SPDRP_REMOVAL_POLICY_OVERRIDE = 0x00000021, // Removal Policy Override (RW)
- SPDRP_INSTALL_STATE = 0x00000022, // Device Install State (R)
- SPDRP_LOCATION_PATHS = 0x00000023, // Device Location Paths (R)
- SPDRP_BASE_CONTAINERID = 0x00000024 // Base ContainerID (R)
- }
- }
- "@
- Add-Type -TypeDefinition $setupapi
- Function Set-DeviceEnableState {
- Param(
- [GUID]$ClassGUID,
- [string]$InstanceID,
- [Boolean]$Enabled
- )
- $array = @()
- $devs = [Win32.SetupApi]::SetupDiGetClassDevs([ref]$ClassGUID, [IntPtr]::Zero, [IntPtr]::Zero, [Win32.DiGetClassFlags]::DIGCF_ALLCLASSES)
- $devInfo = new-object Win32.SP_DEVINFO_DATA
- $devInfo.cbSize = [System.Runtime.InteropServices.Marshal]::SizeOf($devInfo)
- $devCount = 0
- $installParams = New-Object -Typename Win32.DeviceEnableParameters
- $installParams.Size = 8
- $installParams.DiFunction = [Win32.DiFunction]::PropertyChange
- $installParams.Scope = [Win32.Scopes]::Global
- if ($Enabled -eq $True) {
- $installParams.StateChange = [Win32.StateChangeAction]::Enable
- }
- else {
- $installParams.StateChange = [Win32.StateChangeAction]::Disable
- }
- $installParamSize = [System.Runtime.InteropServices.Marshal]::SizeOf($installParams)
- $installParamDiFunction = [Win32.DiFunction]::PropertyChange
- #Enumerate Devices in class
- while([Win32.SetupApi]::SetupDiEnumDeviceInfo($devs, $devCount, [ref]$devInfo)){
- #Read device instance ID.
- $InstIDStringBuilder = New-Object -TypeName "System.Text.StringBuilder"
- $InstIDStringBuilder.Capacity = 5000
- $reqSize = 0
- $Result = [Win32.SetupAPI]::SetupDiGetDeviceInstanceId($devs, [ref]$devInfo, $InstIDStringBuilder, $InstIDStringBuilder.Capacity, [ref]$reqSize);
- if (($Result -eq $False) -and ($reqSize -gt 0)) {
- $sb.Capacity -eq $reqSize
- $Result = [Win32.SetupAPI]::SetupDiGetDeviceInstanceId($devs, [ref]$devInfo, $InstIDStringBuilder, $InstIDStringBuilder.Capacity, [ref]$reqSize);
- }
- $InstIDString = $InstIDStringBuilder.ToString()
- if ($InstIDString -eq $InstanceID) {
- [Win32.SetupApi]::SetupDiSetClassInstallParams($devs, [ref]$devInfo, [ref]$installParams, $installParamSize);
- #This line works, returns True.
- [Win32.SetupApi]::SetupDiCallClassInstaller($installParamDiFunction, [ref]$devs);
- #This line works, returns False (ie: failed).
- #GetLastWin23Error() returns 203.
- }
- $devcount += 1
- }
- }
- 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