Login Script to handle Persistent Drive Mapping

Discussion in 'Scripting' started by Mike Bailey, Sep 10, 2008.

  1. Mike Bailey

    Mike Bailey Guest

    I've been trying to put together a login script that will account for XP
    persistent drive mapping. I've foudn that with normal vbs scripting to
    just remove and then remap a drive letter, that it may still appear to
    be mapped incorrectly in My Computer.

    All of the script that I have has come from other people.

    Here is what I hve right now, and it is producing an error: Line 19
    Char: 5 Error: Variable is undefined: 'HKCU'

    ----Start My Scrip-----------------------------------------------

    Option Explicit
    Dim objFSO, objNetwork, objReg

    '*****************
    'MAP Q: TO sever
    '*****************
    '
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")
    Set objReg =
    GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

    'Q: Drive
    '*********
    If (objFSO.DriveExists("Q:" = True)) Then
    objNetwork.RemoveNetworkDrive "Q:", True, True
    End If

    If objFSO.DriveExists("Q:") Then
    objReg.DeleteKey HKCU, "Network\" & Left("Q:", 1)
    End If

    objNetwork.MapNetworkDrive "Q:", "\\server\share"

    ---End My Script-----------------------------------------------


    At one time, I had received this email/post with the script suggestions
    below from a couple of people tryign to help. I'm not sure if all of
    this is needed, or just part of it.

    =======================================================================
    To remove any persistent mappings, more may be necessary.

    If (objFSO.DriveExists("j:" = True) Then
    objNetwork.RemoveNetworkDrive "j:",True,True
    End If

    If objFSO.DriveExists("j:") Then
    Set objReg =
    GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    objReg.DeleteKey HKCU, "Network\" & Left("j:", 1)
    Set objReg = Nothing
    End If

    --------------------------------------------------
    Dim WshNet

    Set WshNet = WScript.CreateObject("WScript.Network")
    WshNet.RemoveNetworkDrive "J:", True, True
    Wscript.Sleep(1000)
    WshNet.MapNetworkDrive "J:", "\\myserver\users\shared"
    Set WSHNet = Nothing

    I put a 1 second delay in to allow the network to "settle" before the
    reconnect. Might help??

    ---------------------------------------------------------

    ==========================================================================

    Thanks for any help,
    Mike
     
    Mike Bailey, Sep 10, 2008
    #1
    1. Advertisements

  2. Mike Bailey

    Mike Bailey Guest

    By the way, if I take the code out causing the error, I then get this
    error which reflects the persistent dive maping issue:

    Error: THe local device name has a remembered connection to antoher
    network resource.

    Thanks,
    Mike
     
    Mike Bailey, Sep 10, 2008
    #2
    1. Advertisements

  3. Mike Bailey

    Al Dunbar Guest

    This statement:
    calls the DeleteKey method, and passes two parameters. The second parameter
    is the string "Network\Q", while the first parameter is being interpreted
    according to vbscript syntax rules as a reference to a non-existent variable
    (or constant) called HKCU.

    The first parameter to this method needs to be one of a series of special
    numeric values. To delete a key in the Current User hive with the above
    code, you'd be best off defining HKCU as a constant:

    const HKCU = &H80000001


    /Al
     
    Al Dunbar, Sep 11, 2008
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.