[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] 使用VBS禁用、启动USB存储设备

本帖最后由 yu2n 于 2014-9-2 11:53 编辑

翻出一个老脚本……

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

来顶贴学习。

TOP

学习了,谢谢,正好需要这个

TOP

谢谢分享,收藏学习!@

TOP

返回列表