欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用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