Vbscript vb脚本修改USBSTOR密钥后USB_MASS_存储驱动程序崩溃

Vbscript vb脚本修改USBSTOR密钥后USB_MASS_存储驱动程序崩溃,vbscript,Vbscript,这个剧本是我自己写的。 基本上我想要的是脚本“解锁”前usb只为特定的设备。 这种情况下,我们有一个注册表项USBSTOR\Start设置为4(禁用)的工作站,因此如果没有IT部门的额外工作,前端usb不可用-这样我们可以控制谁可以访问usb 但是,员工必须使用相机拍摄特定需要的照片,并通过电子邮件客户端发送。因此,我们希望实现“锁定/解锁”阶段的自动化 插入感兴趣的设备时,usb处于解锁状态,设备处于usb状态时,usb处于“解锁”状态,设备拔出后,脚本将再次“锁定”usb 我已决定使用.vb

这个剧本是我自己写的。 基本上我想要的是脚本“解锁”前usb只为特定的设备。 这种情况下,我们有一个注册表项USBSTOR\Start设置为4(禁用)的工作站,因此如果没有IT部门的额外工作,前端usb不可用-这样我们可以控制谁可以访问usb

但是,员工必须使用相机拍摄特定需要的照片,并通过电子邮件客户端发送。因此,我们希望实现“锁定/解锁”阶段的自动化

插入感兴趣的设备时,usb处于解锁状态,设备处于usb状态时,usb处于“解锁”状态,设备拔出后,脚本将再次“锁定”usb

我已决定使用.vbs。脚本按照我的预期工作,但在“锁定”阶段后,USB_MASS_存储驱动程序崩溃。我必须卸载它并重新启动Windows,驱动程序才能重新加载并正常工作。在我多次运行脚本后,USBSTOR\Start中的注册表值不会影响USB,i、 即使有4个,usb也会解锁。如果我将值从4更改为3,则驱动程序会崩溃

我在寻求一些建议

以下是usbstor.vbs脚本的代码。我用了很多评论,其中一些解释了一件很明显的事情,但我决定这么做

' Script for access to Front Usb (a.k.a USB MASS STORAGE)
' The usb is locked by default(the value in Registry Key USBSTOR/Start is 4 - disable).It is enabled(the value in Registry Key USBSTOR/Start is 3 - enable) when the device of interest is put into front usb.
' The usb is in  "enable" state ,while the device is into it. After it is removed,the Registry Key USBSTOR/Start value is set to 4(disable).
' The device is recognized by hardware id ,which is known in advance by searching USBSTOR,when the device is inserted. This script is for pc,where what we want is access to front usb only for spcecific device(a camera in our case).
' For everything else the usb should be disabled.The script is loaded in RAM and if the while loop condition isn't change to false,we must kill the process within TaskManager 
' The CPU time is high > 98 while the script runs.I came to this solution for my problem,but any ideas for improvements or for different logic are highly welcomed.

Option Explicit On

Dim Shell,Start,Hwid,Enum_0,Enum_1,Count,Flag_0,Flag_1,Check_0,Check_1   'Dimension of varables we are going to use in the script.

Set Shell = CreateObject("WScript.Shell")   'Create an object to work with Windows Registry.

'Start =  Shell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start")  'Save the value of the registry key into Start variable

Hwid = "USB\Vid_0930&Pid_6545\001D92A866B8B8B1934703FE"  'hardawre id of device of interest.We get it from the registry and the script scan for this id.It is constant string

Count = 1  'Initialize the Count variable with value of 1.We use it as a condition in endless while() loop.It makes script run in real-time,so it can scan uninterupted for changes in the registry

QueryEnum0 ' The subroutines QueryEnum0 and QueryEnum1.The id is either in USBSTOR\Enum\0 or in USBSTOR\Enum\1 .That is for sure. 
QueryEnum1 ' Declaration before definition - not exactly explanation.

'The purpose of these two subroutines is: create an object everytime the sub is called ,thus read the value in Enum\0 or in Enum\1 constantly as "scanning"
'Probably not so elegant solution to somebody,but actually it works.

Sub QueryEnum0 ' Enter the sub

     Dim Flag_Enum_0,Shell ' Declare local variables.They will be created each time the sub is invoked.
     Set Shell = CreateObject("WScript.Shell")    'Create an object to work wirh registry any time the sub is called

     On Error Resume Next 'Error handling
        Flag_Enum_0 = Shell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\0")  'Try to read reg value into Flag_Enum_0. The purpose 

     On Error GoTo 0

     Flag_0 = Flag_Enum_0 'Assign the value to variable Flag_0,outside of sub.The memory for Flag_0 is set once and lasts while the script runs.
End Sub



' Same as QueryEnum0

Sub QueryEnum1

     Dim Flag_Enum_1,Shell
     Set Shell = CreateObject("WScript.Shell")

     On Error Resume Next
        Flag_Enum_1 = Shell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\1")
     On Error GoTo 0
     Flag_1 = Flag_Enum_1

End Sub


Do While Count = 1 'Real-time loop,the code within while is running while count is equal to 1. The script is loaded in memory constanlty.

  On Error Resume Next 

     Enum_0 = Shell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\0") ' Try to read hardware id if it is in Enum\0

  On Error GoTo 0 '


  On Error Resume Next 

     Enum_1 = Shell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\1") 'Try to read hardware id if it is in Enum\1

  On Error GoTo 0





  If StrComp(Hwid,Enum_0) <> 0 And StrComp(Hwid,Enum_1) <> 0  Then 'Check if both reg keys are empty

   MsgBox "There is no device in the front usb.Please put the device or see the connection"



  ElseIf StrComp(Hwid,Enum_0) = 0 Then 'If the hardware id is in Enum\0,thus assigned to Enum_0

      Shell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start",3 'Enable the usb by "unlock" it



        On Error Resume Next
          QueryEnum0 'Invoke sub QueryEnum0.If the id we looking for is in Enum\0,we know that it is assigned to Flag_0 also
          Check_0 = Flag_0 'Use another variable to copy value from Flag_0.                                                                                               
        On Error GoTo 0

         If StrComp(Hwid,Check_0) = 0 Then 'Compare the constant Hwid with the value in Check_0,test for id
            Msgbox "Check_0 still holds the hardware id" 'Some messages to inform us whats happening
         else    
            MsgBox "Check_0 does not contain the hardware id anymore"
            Shell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start",4 'Disable the front usb
            Count = 2 'End the while loop,count is 2,so the condition is false .The loop breaks.
         End If



  ElseIf StrComp(Hwid,Enum_1) = 0 Then 'If the hardware is in Enum\1....same as above mentioned

      Shell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start",3 



        On Error Resume Next

        QueryEnum1  
        Check_1 = Flag_1

        On Error GoTo 0

         If StrComp(Hwid,Check_1) = 0 Then
            MsgBox "Check_1 still holds the hardware id"
            MsgBox Check_1
         else

            MsgBox "Check_0 does not contain the hardware id anymore"
            Shell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start",4
            Count = 2

         End If


  End If

Loop   




' Useful information for me 

'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR -> value name -> Start ,value data = 3(enable) = 4(disable)

 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum -> value name -> 1 or 0 ,value data we look for is -> USB\Vid_04da&Pid_2372\5&2f621ee5&0&8
   ' USB\Vid_04da&Pid_2372\5&2f621ee5&0&8 - camera id in our case

   ' fantom value - USB\Vid_03f0&Pid_032a\000000000Q912WFBSI1c - name: 0 ,type: REG_SZ,in the key Enum.This is another hardware id,which is strange somehow,because I do not have any device
   ' inserted in my usb.However,I take this value into account,thus use both keys 0 and 1 within Enum to scan for the id I need.
用于访问前端Usb(又称Usb大容量存储)的脚本 '默认情况下,usb是锁定的(注册表项USBSTOR/Start中的值为4-禁用)。当感兴趣的设备放入前端usb时,usb是启用的(注册表项USBSTOR/Start中的值为3-启用)。 '设备进入usb时,usb处于“启用”状态。删除后,注册表项USBSTOR/Start值设置为4(禁用)。 '插入设备时,通过搜索USBSTOR预先知道的硬件id识别设备。这个脚本是为pc编写的,我们想要的是只为spcecific设备(在我们的例子中是一个摄像头)访问前端usb。 '对于其他所有内容,usb都应该被禁用。脚本加载到RAM中,如果while循环条件没有更改为false,我们必须在TaskManager中终止进程 当脚本运行时,CPU时间高达>98。我是为了解决我的问题而提出这个解决方案的,但是任何改进或不同逻辑的想法都是非常受欢迎的。 选项显式打开 Dim Shell、Start、Hwid、Enum_0、Enum_1、Count、Flag_0、Flag_1、Check_0、Check_1’我们将在脚本中使用的变量的维度。 设置Shell=CreateObject(“WScript.Shell”)'创建一个对象以使用Windows注册表。 'Start=Shell.regrad(“HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start”)'将注册表项的值保存到Start变量中 Hwid=“USB\Vid_0930&Pid_6545\001D92A866B8B1934703FE”感兴趣设备的硬件id。我们从注册表和脚本扫描中获取该id。它是常量字符串 Count=1'初始化值为1的Count变量。我们将其用作无穷while()循环中的条件。它使脚本实时运行,因此它可以扫描注册表中的未中断更改 QueryEnum0'子例程QueryEnum0和QueryEnum1。id在USBSTOR\Enum\0或USBSTOR\Enum\1中。这是肯定的。 定义前的QueryEnum1声明-不完全是解释。 '这两个子例程的目的是:每次调用子例程时都创建一个对象,从而将Enum\0或Enum\1中的值作为“扫描”不断读取' 对于某些人来说,这可能不是一个优雅的解决方案,但实际上它是有效的。 Sub QueryEnum0'输入Sub Dim Flag_Enum_0,Shell'声明局部变量。每次调用sub时都会创建这些变量。 Set Shell=CreateObject(“WScript.Shell”)'创建一个对象,以便在调用子对象时与注册表一起工作 错误恢复下一步“错误处理” Flag_Enum_0=Shell。重新读取(“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\0”)'尝试将注册表值读取到Flag_Enum_0中。目的 错误转到0 Flag_0=Flag_Enum_0'将值分配给变量Flag_0,位于sub之外。Flag_0的内存设置一次,并在脚本运行时保持不变。 端接头 '与QueryEnum0相同 子查询num1 Dim Flag_Enum_1,外壳 Set Shell=CreateObject(“WScript.Shell”) 出错时继续下一步 Flag_Enum_1=Shell.regrad(“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\1”) 错误转到0 Flag_1=Flag_Enum_1 端接头 Do While Count=1'实时循环,当Count等于1时,While内的代码正在运行。脚本将一直加载到内存中。 出错时继续下一步 Enum_0=Shell。重新读取(“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\0”)'如果硬件id在Enum\0中,请尝试读取它 错误转到0' 出错时继续下一步 Enum_1=Shell.Regrad(“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\1”)“如果硬件id在Enum\1中,请尝试读取它 错误转到0 如果StrComp(Hwid,Enum_0)0和StrComp(Hwid,Enum_1)0,则“检查两个注册表项是否为空” MsgBox“前usb中没有设备。请放入设备或查看连接” ElseIf StrComp(Hwid,Enum_0)=0,则“如果硬件id在Enum\0中,则分配给Enum_0 Shell.RegWrite“HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start”,3'通过“解锁”来启用usb 出错时继续下一步 QueryEnum0'调用子QueryEnum0。如果我们要查找的id在枚举\0中,我们知道它也被分配给标志\u 0 选中_0=Flag_0'使用另一个变量从Flag_0复制值。 错误转到0 如果StrComp(Hwid,Check_0)=0,则“将常数Hwid与Check_0,test for id中的值进行比较” Msgbox“Check_0仍保留硬件id”会显示一些消息,通知我们发生了什么 其他的