找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 27661|回复: 3

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

[复制链接]
发表于 2014-9-2 11:51:33 | 显示全部楼层 |阅读模式
本帖最后由 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.                
  14.         If wso.Popup(VbCrLf & "禁用 USB 存储设备,请按“确定”"& VbCrLf &  _
  15.                                 VbCrLf & "启用 USB 存储设备,请按“取消”     (6秒后自动取消)" _
  16.                                 , 6, "USB 存储设备控制 - 主菜单", 48+4096+1) = 1 Then
  17.                 EnableUSB = 0
  18.         Else
  19.                 EnableUSB = 1
  20.         End If
  21.        
  22.         If Exist( "C:\windows\system32\cmd.exe" ) Then windir = "windows"
  23.         If Exist( "C:\winnt\system32\cmd.exe" ) Then windir = "winnt"

  24.         If EnableUSB = 1 Then
  25.                 wso.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies\WriteProtect","1","REG_DWORD" '禁止写入
  26.                 wso.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start","3","REG_DWORD"                     '启用USBStor
  27.                
  28.                 Move "C:" & windir & "\inf\usbstor.pnf_" , "C:" & windir & "\inf\usbstor.pnf"
  29.                 Move "C:" & windir & "\inf\usbstor.inf_" , "C:" & windir & "\inf\usbstor.inf"
  30.                 Move "C:" & windir & "\system32\drivers\usbstor.sys_" , "C:" & windir & "\system32\drivers\usbstor.sys"
  31.                
  32.                 If (Not Exist( "C:" & windir & "\inf\usbstor.pnf_" )) And (regKeyRead( "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start" ) = 3 ) Then
  33.                         wso.Popup VbCrLf & "启用 USB 存储设备成功。    ", 5, "USB 存储设备控制 - 操作完成", 64+4096
  34.                 Else
  35.                         wso.Popup VbCrLf & "启用 USB 存储设备失败。    ", 5, "USB 存储设备控制 - 操作完成", 16+4096
  36.                 End If
  37.         Else
  38.                 wso.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies\WriteProtect","1","REG_DWORD" '禁止写入
  39.                 wso.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start","4","REG_DWORD"                     '禁用用USBStor
  40.                
  41.                 Move "C:" & windir & "\inf\usbstor.pnf" , "C:" & windir & "\inf\usbstor.pnf_"
  42.                 Move "C:" & windir & "\inf\usbstor.inf" , "C:" & windir & "\inf\usbstor.inf_"
  43.                 Move "C:" & windir & "\system32\drivers\usbstor.sys" , "C:" & windir & "\system32\drivers\usbstor.sys_"
  44.                
  45.                 If (Not Exist( "C:" & windir & "\inf\usbstor.pnf" )) And (regKeyRead( "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start" ) = 4 ) Then
  46.                         wso.Popup VbCrLf & "禁用 USB 存储设备成功。    ", 5, "USB 存储设备控制 - 操作完成", 64+4096
  47.                 Else
  48.                         wso.Popup VbCrLf & "禁用 USB 存储设备失败。    ", 5, "USB 存储设备控制 - 操作完成", 16+4096
  49.                 End If
  50.         End if
  51.                
  52.         Set wso = Nothing
  53. End Sub

  54. '===========================================================================================
  55. '小函数
  56. Function Exist( strPath )
  57.         'On Error Resume Next
  58.         Set fso = CreateObject("Scripting.FileSystemObject")
  59.         If ((fso.FolderExists( strPath )) Or (fso.FileExists( strPath ))) then
  60.                 Exist = True
  61.         Else
  62.                 Exist = False
  63.         End if
  64.         Set fso = Nothing
  65. End Function
  66. Sub Move( strSource, strDestination )
  67.         On Error Resume Next
  68.         If Exist( strSource ) Then
  69.                 Set fso = CreateObject("Scripting.FileSystemObject")
  70.                 If (fso.FileExists(strSource)) Then fso.MoveFile strSource, strDestination
  71.                 If (fso.FolderExists(strSource)) Then fso.MoveFolder strSource, strDestination
  72.                 Set fso = Nothing
  73.         Else
  74.                 WarningInfo "警告", "找不到 " & strSource & " 文件!", 2
  75.         End If
  76.         If Not Exist( strDestination ) Then WarningInfo "警告", "移动失败,无法移动 " & VbCrLf & strSource & " 至" & VbCrLf & strDestination, 2
  77. End Sub
  78. Function regKeyRead( strKey )
  79.         Set wso = CreateObject("WScript.Shell")
  80.         regKeyRead = wso.RegRead( strKey )    'strKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\DocTip"
  81.         Set wso = Nothing
  82. End Function

  83. '===========================================================================================
  84. '是否重复运行
  85. Sub CheckMeState()
  86.         If IsRun( WScript.ScriptFullName ) Then
  87.                 Set wso = CreateObject("WScript.Shell")
  88.                 If wso.Popup("程序已运行,请不要重复运行本程序!" & VbCrLf & VbCrLf & _
  89.                                                         "退出已运行程序,请按“确定”,否则请按“取消”。(3秒后自动取消)" _
  90.                                                         , 3, "警告", 1) = 1 Then
  91.                         KillMeAllRun
  92.                 End If
  93.                 Set wso = Nothing
  94.                 'WarningInfo "警告:", "程序已运行,请不要重复运行本程序!!", 1
  95.                 WScript.Quit
  96.         End If
  97. End Sub
  98. ' 检测是否重复运行
  99. Function IsRun(appPath)
  100.         IsRun=False
  101.         For Each ps in GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
  102.                 'IF Lcase(ps.name)="mshta.exe" Then
  103.                 IF Lcase(ps.name)="wscript.exe" Then
  104.                         IF instr(Lcase(ps.CommandLine),Lcase(appPath)) Then i=i+1
  105.                 End IF
  106.         next
  107.         if i>1 then
  108.                 IsRun=True
  109.         end if
  110. End Function
  111. '终止自身
  112. Function KillMeAllRun()
  113.         Dim MeAllPid
  114.         Set pid = Getobject("winmgmts:\\.").InstancesOf("Win32_Process")
  115.         For Each ps In pid
  116.                 'if LCase(ps.name) = LCase("mshta.exe") then
  117.                 IF Lcase(ps.name)="wscript.exe" Or Lcase(ps.name)="cscript.exe"Then
  118.                         IF instr(Lcase(ps.CommandLine),Lcase(WScript.ScriptFullName)) Then MeAllPid = MeAllPid & "/PID " & ps.ProcessID & " "
  119.                 end if
  120.         next
  121.         Set wso = CreateObject("WScript.Shell")
  122.         wso.Run "TASKKILL " & MeAllPid & " /F /T", 0, False
  123.         Set wso = Nothing
  124.         Set pid = Nothing
  125. End Function

  126. '===========================================================================================
  127. '检查操作系统版本
  128. Sub CheckOS()
  129.         Dim os_ver
  130.         os_ver = GetSystemVersion
  131.         If os_ver >= 60 Or os_ver <= 50 Then
  132.                 Msgbox "不支持该操作系统!    ", 48+4096, "警告"
  133.                 WScript.Quit    ' 退出程序
  134.         End If
  135. End Sub
  136. '取得操作系统版本
  137. Function GetSystemVersion()
  138.         Dim os_obj, os_version, os_version_arr
  139.         Set os_obj = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  140.         For Each os_info In os_obj
  141.                 os_version = os_info.Version
  142.                 If os_version <> "" Then Exit For
  143.         Next
  144.         Set os_obj = Nothing
  145.         os_version_arr = Split( os_info.Version, ".")
  146.         GetSystemVersion = Cint( os_version_arr( 0 ) & os_version_arr( 1 ) )
  147. End Function
复制代码
发表于 2014-9-2 14:52:37 | 显示全部楼层
来顶贴学习。
发表于 2015-2-13 16:15:32 | 显示全部楼层
学习了,谢谢,正好需要这个
发表于 2019-8-29 15:25:25 | 显示全部楼层
谢谢分享,收藏学习!@
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 04:50 , Processed in 0.018330 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表