Announcement

Collapse
No announcement yet.

VBS + Hiding drives in Windows clients

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBS + Hiding drives in Windows clients

    I was scanning the "net" to find a way to hide drives on a windows workstation.
    Everybody is looking for this solution, but nobody had really posted anything working.
    I decided to create my fist VB script.... YES!!!
    Here is a script to create another script that you can push through a login script on a server or install in the startup group on the workstation.
    Run it from the command prompt (<script name.vbs> <drive> <drive>)
    The only drive that you cannot hide is the C: . I gave it a code of 0 when it should be 4.

    The content of the generated file is:
    Code:
    dim objshell
    PATH="HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\nodrives"
    Set objShell = CreateObject("WScript.Shell")
    objShell.RegWrite PATH,"0","REG_DWORD"
    The value 0 (zero) after "path," will not hide anything. This exemple will restore the visibility of all drives.

    I tried it locally on a win XP and Vista workstation and it works. tomorrow I will try it as a user's login script on one of my 2003 servers.

    Enjoy,

    Bert (first posting)


    Code:
    '======================================================================================================
    '=                    Creates a .VBS script to hide drives in Windows XP,2000,Vista                   =
    '=       Some software require to have the directory mapped and users having full rights to it.       =
    '=                     This is often the case if the software uses a database                         =
    '= When the drive is hidden, users won't see it in Windows Explorer, but it is still visible from DOS =
    '=  Users keep their full rights to the drive but they cannot browse it to ad, delete or modify files =
    '=              usage: from command prompt: hidedrive.vbs <drive letter> <drive letter>.....          =
    '=                   (command: Hidedrive.vbs x y z) will create a file hide-x-z-y.vbs                 =
    '=                    This file can be loaded in the startup folder of the workstation                =  
    '=                       or in the user's loging script on the Windows 2003 server.                   =
    '=                                                                                                    = 
    '=                                          author:Bertrand Vogel                                     =
    '======================================================================================================
    
    dim x, bit,selected,selection
    Dim objFileSystem, objOutputFile
    Dim strOutputFile
    
    set args = WScript.Arguments
    num = args.Count
    
    
    if num = 0 then
       WScript.Echo "No command line parameters !"
       WScript.Quit 1
    end if
    
    bit=00
    selected=""
    
    for k = 0 to num - 1
    
    selected = selected & args.item(k) & "-"
    Select Case args.Item(k)
    
    Case "A","a"
    bit=bit+1
    
    Case "B","b"
    bit=bit+2
    
    Case "C","c"
    bit=bit
    
    Case "D","d"
    bit=bit+8
    
    Case "E","e"
    bit=bit+16
    
    Case "F","f"
    bit=bit+32
    
    Case "G","g"
    bit=bit+64
    
    Case "H","h"
    bit=bit+128
    
    Case "I","i"
    bit=bit+256
    
    Case "J","j"
    bit=bit+512
    
    Case "K","k"
    bit=bit+1024
    
    Case "L", "l"
    bit=bit+2048
    
    Case "M","m"
    bit=bit+4096
    
    Case "N", "n"
    bit=bit+8192
    
    Case "O","o"
    bit=bit+16384
    
    Case "P", "p"
    bit=bit+32768
    
    Case "Q", "q"
    bit=bit+65536
    
    Case "R", "r"
    bit=bit+131072
    
    Case "S", "s"
    bit=bit+262144
    
    Case "T", "t"
    bit=bit+524288
    
    Case "U", "u"
    bit=bit+1048576
    
    Case "V", "v"
    bit=bit+2097152
    
    Case "W", "w"
    bit=bit+4194304
    
    Case "X", "x"
    bit=bit+8388608
    
    Case "Y", "y"
    bit=bit+16777216
    
    Case "Z", "z"
    bit=bit+33554432
    
    
    End Select
    next
    
    x=len(cstr(selected))
    selection = left(cstr(selected),x -1)
    
    ' Create the file in the format of hide-<drive>-<drive>-<drive>.vbs
    strOutputFile = "hide-" & selection & ".vbs"
    
    Set objFileSystem = CreateObject("Scripting.fileSystemObject")
    Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)
    
    objOutputFile.WriteLine("dim objshell")
    objOutputFile.WriteLine("PATH=" & chr(34) & "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\nodrives" & chr(34))
    objOutputFile.WriteLine("Set objShell = CreateObject(" & chr(34) & "WScript.Shell" & chr(34) &")")
    objOutputFile.WriteLine("objShell.RegWrite PATH" & chr(44) & chr(34) & bit & chr(34) & chr(44) & chr(34) & "REG_DWORD" & chr(34))
    
    objOutputFile.Close
    
    Set objFileSystem = Nothing
    
    msgbox "Created " & stroutputfile & (Chr(13)& Chr(10)) & "for the purpose of hiding drive(s) " & selection
    WScript.Quit(0)
    Last edited by Rems; 30th July 2008, 00:53. Reason: added [code] tags around the script

  • #2
    Re: VBS + Hiding drives in Windows clients

    Originally posted by bertv View Post
    Everybody is looking for this solution, but nobody had really posted anything working.
    I decided to create my fist VB script.... YES!!!

    <...>
    I tried it locally on a win XP and Vista workstation and it works. tomorrow I will try it as a user's login script on one of my 2003 servers.
    nice script Bert, specially since it was your first.
    Thanks for sharing.
    Did you already tried it as a startup / user logon script for the domain?

    I was wandering if you had a particular reason to hide-drives in a two-way-step (using two scripts)?

    btw. you can hide C: too the same way as for the other drive letters.
    Possibly that you open explorer with c:? and that would make C: visible again. Because in exporer users still can make the drives temporarly visible one by one via the addressbar.

    And that is actualy what makes 'NoDrives' not a save way to hide data!! A better option would be using the ntfs permissions to unselect the listing of subfolders.

    -
    The problem with running the script as a logon script is that, however users have permissions to add, change and delete most entries in HKCU, they only have read permissions in the policy subkeys if the user is not a member of the localgroup Administrators (It is not recommended to change the default permissions on these keys).

    What you can try is to change the script into a computer startup script. Then make the NoDrives configuration global by writing the policy in the HKLM subkey instead of HKCU. IF a 'NoDrive' value exists in the hklm subkey it will take precedence over the configuration in hkcu !!!

    Code:
    Option Explicit
    Dim args, num
    Dim objShell, PATH, bits
    Dim exitcode
    Dim k, x, i
    
    Set args = WScript.Arguments
    num = args.Count
    
    Set objShell = CreateObject("WScript.Shell")
    PATH = "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\nodrives"
    bits = 0
    
    If num = 0 then
       exitcode = 1
    Else
       exitcode = 0 
    End If
    
    For k = 0 to num - 1
       x = 0
       For i = Asc("A") To Asc("Z")
          If Chr(i) = UCase(Left(args.item(k), 1)) then
             bits = bits + 2^x 
          End If
          x = x + 1
       Next
    Next
    
    
    On error resume next
    
    Dim sNoDrives
    sNoDrives = objShell.RegRead(PATH)
    If (err.number =  0 and sNoDrives <> bits) OR _
       (err.number <> 0 and bits <> 0) then
    
      ' Write registry
      '-------------------------------------------------------------
      err.clear
      If bits <> 0 Then
        objShell.RegWrite PATH, bits, "REG_DWORD"
      Else
        objShell.RegDelete PATH
        objShell.popup "Script was executed without script arguments" & vbNewLine _
          & "global drive visability is set to system default" ,3
      End If
    
      If err <> 0 then
        objShell.popup "This script works only if the current_user is also a sysadmin", 3
        exitcode = 55
        err.clear
      Else
        ' Refresh explorer
        '-------------------------------------------------------------
        dim strComputer, objWMIService, colProcess, objProcess 
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" _ 
          & strComputer & "\root\cimv2") 
    
        Set colProcess = objWMIService.ExecQuery _
          ("Select * from Win32_Process Where Name = 'explorer.exe'")
        For Each objProcess in colProcess
           objProcess.Terminate()
        Next 
      End If
    End If
    
    Wscript.quit(exitcode)
    \Rems

    EDIT
    When you like to test the script as a computer startup script, you can comment out the two popup lines and comment out the part that is refressing explorer.exe. (these lines are colored Gray)
    Last edited by Rems; 30th July 2008, 01:27.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: VBS + Hiding drives in Windows clients

      The reason to hide drives using 2 scripts is simple.

      I am working for a school board and administer many servers (Novell and Microsoft) in different sites (Each High school and middle shool has 1 or 2 servers).
      Depending of the applications that each site runs, the drive mapping may be different.
      My solution permits to creates customized vbs from one main script.


      I did test it and it works very well. I just insert the created script into the login of the group policy.

      Bert

      Comment

      Working...
      X