首页 > 编程 > VBScript > 正文

使用VBS禁用、启动USB存储设备

2020-06-26 18:40:24
字体:
来源:转载
供稿:网友

这篇文章主要介绍了使用VBS禁用、启动USB存储设备,需要的朋友可以参考下

USB存储设备控制 By Yu2n

XP系统测试通过,需要管理员权限。

在下次插拔设备时生效,无须重启。

  1. 'USB_Stock_Block.vbs 
  2. '=========================================================================================== 
  3. CheckOS ' 检查操作系统版本 
  4. CheckMeState ' 检查程序运行状态 
  5. main ' 执行主程序 
  6.  
  7. '=========================================================================================== 
  8. '主函数 
  9. Sub main() 
  10. Dim wso, windir, EnableUSB 
  11. Set wso = CreateObject("WScript.Shell"
  12. Set objNetwork = CreateObject("wscript.network"
  13. strComputer = objNetwork.ComputerName 
  14.  
  15. If wso.Popup(VbCrLf & "禁用 USB 存储设备,请按“确定”"& VbCrLf & _ 
  16. VbCrLf & "启用 USB 存储设备,请按“取消” (6秒后自动取消)" _ 
  17. , 6, "USB 存储设备控制 - 主菜单", 48+4096+1) = 1 Then 
  18. EnableUSB = 0 
  19. Else 
  20. EnableUSB = 1 
  21. End If 
  22.  
  23. If Exist( "C:/windows/system32/cmd.exe" ) Then windir = "windows" 
  24. If Exist( "C:/winnt/system32/cmd.exe" ) Then windir = "winnt" 
  25.  
  26. If EnableUSB = 1 Then 
  27. wso.RegWrite "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/StorageDevicePolicies/WriteProtect","1","REG_DWORD" '禁止写入 
  28. wso.RegWrite "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/USBSTOR/Start","3","REG_DWORD" '启用USBStor 
  29.  
  30. Move "C:/" & windir & "/inf/usbstor.pnf_" , "C:/" & windir & "/inf/usbstor.pnf" 
  31. Move "C:/" & windir & "/inf/usbstor.inf_" , "C:/" & windir & "/inf/usbstor.inf" 
  32. Move "C:/" & windir & "/system32/drivers/usbstor.sys_" , "C:/" & windir & "/system32/drivers/usbstor.sys" 
  33.  
  34. If (Not Exist( "C:/" & windir & "/inf/usbstor.pnf_" )) And (regKeyRead( "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/USBSTOR/Start" ) = 3 ) Then 
  35. wso.Popup VbCrLf & "启用 USB 存储设备成功。    ", 5, "USB 存储设备控制 - 操作完成", 64+4096 
  36. Else 
  37. wso.Popup VbCrLf & "启用 USB 存储设备失败。    ", 5, "USB 存储设备控制 - 操作完成", 16+4096 
  38. End If 
  39. Else 
  40. wso.RegWrite "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/StorageDevicePolicies/WriteProtect","1","REG_DWORD" '禁止写入 
  41. wso.RegWrite "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/USBSTOR/Start","4","REG_DWORD" '禁用用USBStor 
  42.  
  43. Move "C:/" & windir & "/inf/usbstor.pnf" , "C:/" & windir & "/inf/usbstor.pnf_" 
  44. Move "C:/" & windir & "/inf/usbstor.inf" , "C:/" & windir & "/inf/usbstor.inf_" 
  45. Move "C:/" & windir & "/system32/drivers/usbstor.sys" , "C:/" & windir & "/system32/drivers/usbstor.sys_" 
  46.  
  47. If (Not Exist( "C:/" & windir & "/inf/usbstor.pnf" )) And (regKeyRead( "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/USBSTOR/Start" ) = 4 ) Then 
  48. wso.Popup VbCrLf & "禁用 USB 存储设备成功。    ", 5, "USB 存储设备控制 - 操作完成", 64+4096 
  49. Else 
  50. wso.Popup VbCrLf & "禁用 USB 存储设备失败。    ", 5, "USB 存储设备控制 - 操作完成", 16+4096 
  51. End If 
  52. End if 
  53.  
  54. Set wso = Nothing 
  55. End Sub 
  56.  
  57. '=========================================================================================== 
  58. '小函数 
  59. Function Exist( strPath ) 
  60. 'On Error Resume Next 
  61. Set fso = CreateObject("Scripting.FileSystemObject"
  62. If ((fso.FolderExists( strPath )) Or (fso.FileExists( strPath ))) then 
  63. Exist = True 
  64. Else 
  65. Exist = False 
  66. End if 
  67. Set fso = Nothing 
  68. End Function 
  69. Sub Move( strSource, strDestination ) 
  70. On Error Resume Next 
  71. If Exist( strSource ) Then 
  72. Set fso = CreateObject("Scripting.FileSystemObject"
  73. If (fso.FileExists(strSource)) Then fso.MoveFile strSource, strDestination 
  74. If (fso.FolderExists(strSource)) Then fso.MoveFolder strSource, strDestination 
  75. Set fso = Nothing 
  76. Else 
  77. WarningInfo "警告""找不到 " & strSource & " 文件!", 2 
  78. End If 
  79. If Not Exist( strDestination ) Then WarningInfo "警告""移动失败,无法移动 " & VbCrLf & strSource & " 至" & VbCrLf & strDestination, 2 
  80. End Sub 
  81. Function regKeyRead( strKey ) 
  82. Set wso = CreateObject("WScript.Shell"
  83. regKeyRead = wso.RegRead( strKey ) 'strKey = "HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run/DocTip" 
  84. Set wso = Nothing 
  85. End Function 
  86.  
  87. '=========================================================================================== 
  88. '是否重复运行 
  89. Sub CheckMeState() 
  90. If IsRun( WScript.ScriptFullName ) Then 
  91. Set wso = CreateObject("WScript.Shell"
  92. If wso.Popup("程序已运行,请不要重复运行本程序!" & VbCrLf & VbCrLf & _ 
  93. "退出已运行程序,请按“确定”,否则请按“取消”。(3秒后自动取消)" _ 
  94. , 3, "警告", 1) = 1 Then 
  95. KillMeAllRun 
  96. End If 
  97. Set wso = Nothing 
  98. 'WarningInfo "警告:", "程序已运行,请不要重复运行本程序!!", 1 
  99. WScript.Quit 
  100. End If 
  101. End Sub 
  102. ' 检测是否重复运行 
  103. Function IsRun(appPath) 
  104. IsRun=False 
  105. For Each ps in GetObject("winmgmts://./root/cimv2:win32_process").instances_ 
  106. 'IF Lcase(ps.name)="mshta.exe" Then 
  107. IF Lcase(ps.name)="wscript.exe" Then 
  108. IF instr(Lcase(ps.CommandLine),Lcase(appPath)) Then i=i+1 
  109. End IF 
  110. next 
  111. if i>1 then 
  112. IsRun=True 
  113. end if 
  114. End Function 
  115. '终止自身 
  116. Function KillMeAllRun() 
  117. Dim MeAllPid 
  118. Set pid = Getobject("winmgmts://.").InstancesOf("Win32_Process"
  119. For Each ps In pid 
  120. 'if LCase(ps.name) = LCase("mshta.exe") then 
  121. IF Lcase(ps.name)="wscript.exe" Or Lcase(ps.name)="cscript.exe"Then 
  122. IF instr(Lcase(ps.CommandLine),Lcase(WScript.ScriptFullName)) Then MeAllPid = MeAllPid & "/PID " & ps.ProcessID & " " 
  123. end if 
  124. next 
  125. Set wso = CreateObject("WScript.Shell"
  126. wso.Run "TASKKILL " & MeAllPid & " /F /T", 0, False 
  127. Set wso = Nothing 
  128. Set pid = Nothing 
  129. End Function 
  130.  
  131. '=========================================================================================== 
  132. '检查操作系统版本 
  133. Sub CheckOS() 
  134. Dim os_ver 
  135. os_ver = GetSystemVersion 
  136. If os_ver >= 60 Or os_ver <= 50 Then 
  137. Msgbox "不支持该操作系统!    ", 48+4096, "警告" 
  138. WScript.Quit ' 退出程序 
  139. End If 
  140. End Sub 
  141. '取得操作系统版本 
  142. Function GetSystemVersion() 
  143. Dim os_obj, os_version, os_version_arr 
  144. Set os_obj = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem"
  145. For Each os_info In os_obj 
  146. os_version = os_info.Version 
  147. If os_version <> "" Then Exit For 
  148. Next 
  149. Set os_obj = Nothing 
  150. os_version_arr = Split( os_info.Version, "."
  151. GetSystemVersion = Cint( os_version_arr( 0 ) & os_version_arr( 1 ) ) 
  152. End Function 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表