使用VBS禁用、启动USB存储设备
程序员文章站
2022-06-25 08:56:01
usb存储设备控制 by yu2n
xp系统测试通过,需要管理员权限。
在下次插拔设备时生效,无须重启。
'usb_stock_block.vbs...
usb存储设备控制 by yu2n
xp系统测试通过,需要管理员权限。
在下次插拔设备时生效,无须重启。
'usb_stock_block.vbs '=========================================================================================== checkos ' 检查操作系统版本 checkmestate ' 检查程序运行状态 main ' 执行主程序 '=========================================================================================== '主函数 sub main() dim wso, windir, enableusb set wso = createobject("wscript.shell") set objnetwork = createobject("wscript.network") strcomputer = objnetwork.computername if wso.popup(vbcrlf & "禁用 usb 存储设备,请按“确定”"& vbcrlf & _ vbcrlf & "启用 usb 存储设备,请按“取消” (6秒后自动取消)" _ , 6, "usb 存储设备控制 - 主菜单", 48+4096+1) = 1 then enableusb = 0 else enableusb = 1 end if if exist( "c:\windows\system32\cmd.exe" ) then windir = "windows" if exist( "c:\winnt\system32\cmd.exe" ) then windir = "winnt" if enableusb = 1 then wso.regwrite "hkey_local_machine\system\currentcontrolset\control\storagedevicepolicies\writeprotect","1","reg_dword" '禁止写入 wso.regwrite "hkey_local_machine\system\currentcontrolset\services\usbstor\start","3","reg_dword" '启用usbstor move "c:\" & windir & "\inf\usbstor.pnf_" , "c:\" & windir & "\inf\usbstor.pnf" move "c:\" & windir & "\inf\usbstor.inf_" , "c:\" & windir & "\inf\usbstor.inf" move "c:\" & windir & "\system32\drivers\usbstor.sys_" , "c:\" & windir & "\system32\drivers\usbstor.sys" if (not exist( "c:\" & windir & "\inf\usbstor.pnf_" )) and (regkeyread( "hkey_local_machine\system\currentcontrolset\services\usbstor\start" ) = 3 ) then wso.popup vbcrlf & "启用 usb 存储设备成功。 ", 5, "usb 存储设备控制 - 操作完成", 64+4096 else wso.popup vbcrlf & "启用 usb 存储设备失败。 ", 5, "usb 存储设备控制 - 操作完成", 16+4096 end if else wso.regwrite "hkey_local_machine\system\currentcontrolset\control\storagedevicepolicies\writeprotect","1","reg_dword" '禁止写入 wso.regwrite "hkey_local_machine\system\currentcontrolset\services\usbstor\start","4","reg_dword" '禁用用usbstor move "c:\" & windir & "\inf\usbstor.pnf" , "c:\" & windir & "\inf\usbstor.pnf_" move "c:\" & windir & "\inf\usbstor.inf" , "c:\" & windir & "\inf\usbstor.inf_" move "c:\" & windir & "\system32\drivers\usbstor.sys" , "c:\" & windir & "\system32\drivers\usbstor.sys_" if (not exist( "c:\" & windir & "\inf\usbstor.pnf" )) and (regkeyread( "hkey_local_machine\system\currentcontrolset\services\usbstor\start" ) = 4 ) then wso.popup vbcrlf & "禁用 usb 存储设备成功。 ", 5, "usb 存储设备控制 - 操作完成", 64+4096 else wso.popup vbcrlf & "禁用 usb 存储设备失败。 ", 5, "usb 存储设备控制 - 操作完成", 16+4096 end if end if set wso = nothing end sub '=========================================================================================== '小函数 function exist( strpath ) 'on error resume next set fso = createobject("scripting.filesystemobject") if ((fso.folderexists( strpath )) or (fso.fileexists( strpath ))) then exist = true else exist = false end if set fso = nothing end function sub move( strsource, strdestination ) on error resume next if exist( strsource ) then set fso = createobject("scripting.filesystemobject") if (fso.fileexists(strsource)) then fso.movefile strsource, strdestination if (fso.folderexists(strsource)) then fso.movefolder strsource, strdestination set fso = nothing else warninginfo "警告", "找不到 " & strsource & " 文件!", 2 end if if not exist( strdestination ) then warninginfo "警告", "移动失败,无法移动 " & vbcrlf & strsource & " 至" & vbcrlf & strdestination, 2 end sub function regkeyread( strkey ) set wso = createobject("wscript.shell") regkeyread = wso.regread( strkey ) 'strkey = "hkey_local_machine\software\microsoft\windows\currentversion\run\doctip" set wso = nothing end function '=========================================================================================== '是否重复运行 sub checkmestate() if isrun( wscript.scriptfullname ) then set wso = createobject("wscript.shell") if wso.popup("程序已运行,请不要重复运行本程序!" & vbcrlf & vbcrlf & _ "退出已运行程序,请按“确定”,否则请按“取消”。(3秒后自动取消)" _ , 3, "警告", 1) = 1 then killmeallrun end if set wso = nothing 'warninginfo "警告:", "程序已运行,请不要重复运行本程序!!", 1 wscript.quit end if end sub ' 检测是否重复运行 function isrun(apppath) isrun=false for each ps in getobject("winmgmts:\\.\root\cimv2:win32_process").instances_ 'if lcase(ps.name)="mshta.exe" then if lcase(ps.name)="wscript.exe" then if instr(lcase(ps.commandline),lcase(apppath)) then i=i+1 end if next if i>1 then isrun=true end if end function '终止自身 function killmeallrun() dim meallpid set pid = getobject("winmgmts:\\.").instancesof("win32_process") for each ps in pid 'if lcase(ps.name) = lcase("mshta.exe") then if lcase(ps.name)="wscript.exe" or lcase(ps.name)="cscript.exe"then if instr(lcase(ps.commandline),lcase(wscript.scriptfullname)) then meallpid = meallpid & "/pid " & ps.processid & " " end if next set wso = createobject("wscript.shell") wso.run "taskkill " & meallpid & " /f /t", 0, false set wso = nothing set pid = nothing end function '=========================================================================================== '检查操作系统版本 sub checkos() dim os_ver os_ver = getsystemversion if os_ver >= 60 or os_ver <= 50 then msgbox "不支持该操作系统! ", 48+4096, "警告" wscript.quit ' 退出程序 end if end sub '取得操作系统版本 function getsystemversion() dim os_obj, os_version, os_version_arr set os_obj = getobject("winmgmts:").instancesof("win32_operatingsystem") for each os_info in os_obj os_version = os_info.version if os_version <> "" then exit for next set os_obj = nothing os_version_arr = split( os_info.version, ".") getsystemversion = cint( os_version_arr( 0 ) & os_version_arr( 1 ) ) end function
推荐阅读
-
电脑如何屏蔽使用USB存储设备 怎样禁用USB存储工具
-
组策略禁用U盘、Win7组策略屏蔽USB存储设备 防止数据泄密的方法
-
电脑如何禁用U盘、屏蔽USB移动存储工具的使用保护电脑文件安全
-
如何禁用USB存储设备、禁用U盘而不影响USB鼠标键盘、网银U盾、加密狗的使用注意事项
-
组策略禁用USB存储设备和禁用U盘软件在禁用USB存储工具孰胜孰劣
-
用电脑U口禁用软件教你屏蔽USB端口、禁用USB存储设备的使用
-
禁止电脑使用U盘、限制电脑使用USB存储设备、控制USB端口使用的方法
-
如何只让Windows操作系统识别特定的U盘、只让使用某些U盘、只让使用指定的USB存储设备
-
教你通过USB控制软件来禁止U盘复制电脑文件、禁止移动存储设备的使用
-
使用VBS禁用、启动USB存储设备