Disconnect Mapped drive in HTA

Home Forums Scripting General Scripting Disconnect Mapped drive in HTA

This topic contains 4 replies, has 3 voices, and was last updated by  wozzell 9 years, 9 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts

  • jeffjenkins1
    Member
    #136024

    Hello,

    I have a questions about disconnecting mapped drives in an HTA. I got this hta and it works beautifully when modded for my environment thanks Rems! http://forums.petri.com/showpost.php?p=68043&postcount=3.

    The only problem is, I need to check if a drive is mapped and if it is, I would like to disconnect it. Can’t seem to get it to work for me. Here is the modded post (the red text is the part I addded that does not work ) :roll: Thanks, Jeff:

    Edit by Mod: added -tags [/I]’round the code[/COLOR]
    [CODE]

    Map Drive N for Tech Support


    Your Username

      

    @mycompany.net Password

      



       



    [/CODE][code]-tags ’round the code


    Map Drive N for Tech Support


    Your Username

      

    @mycompany.net Password

      



       



    [/CODE][CODE]

    Map Drive N for Tech Support

    ApplicationName=”Map N.HTA”
    SingleInstance=”Yes”
    WindowsState=”Normal”
    Scroll=”No”
    Navigable=”Yes”
    MaximizeButton=”No”
    SysMenu=”Yes”
    Caption=”Yes”
    >


    Your Username

      

    @mycompany.net Password

      


       



    [/CODE]


    Rems
    Moderator
    #227117

    Re: Disconnect Mapped drive in HTA

    The line: objNetwork.RemoveNetworkDrive “F:”, True, True -> should be after the line where the object objNetwork is being defined and created.

    Code:
    Sub RunScript
    on Error Resume Next

    ‘ *** variables
    mDrive = “N:”
    strRemoteShare = “\nas1support”
    strDriveAlias = “Tech-Support”
    strUPNsuffix = “@mycompany.net”
    minUSRnamelength = 2
    minPASSwrdlength = 3

    ‘ *** Map drive using the entered credentials
    strUsr = UsrnameArea.Value
    strPas = PasswordArea.Value

    Set objNetwork = CreateObject(“WScript.Network”)
    Set oShell = CreateObject(“Shell.Application”)
    Err.Clear : objNetwork.RemoveNetworkDrive “F:”, True, True
    If Err.Number = 0 Then Msgbox “Station F: succesfully disconnected”
    If Len(strUsr) >= minUSRnamelength then
    strUsr = Ucase(strUsr) & strUPNsuffix ‘= minPASSwrdlength then
    Err.Clear
    objNetwork.MapNetworkDrive mDrive, strRemoteShare, False, strUsr, strPas
    If Err.Number = 0 Then
    oShell.NameSpace(mDrive).Self.Name = strDriveAlias
    End If
    ELSE
    Msgbox chr(34) & strPas & “”” is an incorrect password !”
    Exit Sub
    End If
    ELSE
    Msgbox chr(34) & strUsr & “”” is an incorrect Username !”
    Exit Sub
    End If
    Set oShell = Nothing
    Set objNetwork = Nothing
    Self.Close()
    End Sub

    note,
    This subroutine adds the @UPNsuffix to the entered username. But,, the AD Accountname and the NT Account names does not have to be the same!!! So Instead of the “@UPNsuffix” (user’s PrincipalName) you might want to use the “nbdomain” prefix (user’s sAMAccountName).

    Rems


    jeffjenkins1
    Member
    #346642

    Re: Disconnect Mapped drive in HTA

    This forum rocks, thanks Mod Rems!! :bowdown::bowdown:
    I tested it and it works perfectly!

    Is there anyway to remove the connection to the server no matter what drive letter?

    Just trying to think ahead, in the future we may use multiple connections to this server. If not I can just add them one by one.

    Thanks again!

    Jeff


    Rems
    Moderator
    #227125

    Re: Disconnect Mapped drive in HTA

    jeffjenkins1;127229 wrote:
    Is there anyway to remove the connection to the server no matter what drive letter?

    Yes,, just have to enumerate the mapped drives to retrieve the drive letter and unc path. Then compare each path with the \servershare

    Replace

    Code:
    Err.Clear : objNetwork.RemoveNetworkDrive “F:”, True, True
    If Err.Number = 0 Then Msgbox “Station F: succesfully disconnected”[/CODE]
    With
    [code]
    Set colDrives = objNetwork.EnumNetworkDrives
    For i = 0 to colDrives.Count-1 Step 2
    appDrive = colDrives.Item(i)
    appUNC = colDrives.Item(i+1)
    If UCase(appUNC) = UCase(strRemoteShare) Then
    Err.Clear
    objNetwork.RemoveNetworkDrive appDrive, True, True
    If Err.Number = 0 Then
    deleted = deleted & ” ” & appDrive
    Else
    alert(“Unable to disconnect ” & appDrive & ” from ” & appUNC)
    End If
    End If
    Next
    If Instr(deleted, “:”) > 0 Then alert(“Disconnected drives:” & deleted)

    I noticed in the original HTA code that I added quotes around a boolean value
    objNetwork.MapNetworkDrive mDrive, strRemoteShare, “FALSE”, strUsr, strPas
    Remove the quote signs that are around False.

    Also in the “Sub Window_Onload” routine there are some errors (like quotes around numbers). Here is an improved Window_Onload subroutine.

    Code:
    Sub Window_Onload
    ‘# Size Window
    sHorizontal = 440
    sVertical = 175
    Window.resizeTo sHorizontal, sVertical
    ‘# Get Monitor Details
    Set objWMIService = GetObject _
    (“winmgmts:rootcimv2”)
    intHorizontal = sHorizontal *2
    intVertical = sVertical *2
    Set colItems = objWMIService.ExecQuery( _
    “Select ScreenWidth, ScreenHeight from” _
    & ” Win32_DesktopMonitor”, , 48)
    For Each objItem In colItems
    sWidth= objItem.ScreenWidth
    sHeight = objItem.ScreenHeight
    If sWidth > sHorizontal _
    then intHorizontal = sWidth
    If sHeight > sVertical _
    then intVertical = sHeight
    Next
    Set objWMIService = Nothing
    ‘# Center window on the screen
    intLeft = (intHorizontal – sHorizontal) /2
    intTop = (intVertical – sVertical) /2
    Window.moveTo intLeft, intTop
    ‘# default window content
    window.location.href=”#Top”
    End Sub

    Rems


    wozzell
    Member
    #350996

    Re: Disconnect Mapped drive in HTA

    Sorry for posting on an old thread, I’m a complete newbie with scripts.

    How could you add multiple network drives to the script?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.