Announcement

Collapse
No announcement yet.

Disconnect Mapped drive in HTA

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

  • Disconnect Mapped drive in HTA

    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...43&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 ) Thanks, Jeff:

    Edit by Mod: added [code]-tags 'round the code
    Code:
    <HTML>
    <HEAD>
    <TITLE>Map Drive N for Tech Support</title>
    <HTA:APPLICATION
    ApplicationName="Map N.HTA"
    SingleInstance="Yes"
    WindowsState="Normal"
    Scroll="No"
    Navigable="Yes"
    MaximizeButton="No"
    SysMenu="Yes"
    Caption="Yes"
    ></HEAD>
    <SCRIPT LANGUAGE="VBScript">
    Sub Window_Onload
    'Size Window
    sHorizontal = "440"
    sVertical = "175"
    Set objWMIService = GetObject _
    ("winmgmts:\\" & "." & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_DesktopMonitor")
    For Each objItem in colItems
    intHorizontal = objItem.ScreenWidth
    intVertical = objItem.ScreenHeight
    next
    Window.resizeTo sHorizontal,sVertical
    'Center window
    intLeft = (intHorizontal - sHorizontal) /2
    intTop = (intVertical - sVertical) /2
    Window.moveTo intLeft, intTop
    Set colItems = Nothing
    Set objWMIService = Nothing
    'default window content
    window.location.href="#Top"
    End Sub
     
    Sub RunScript
    on Error Resume Next
     
    ' *** variables
     
    objNetwork.RemoveNetworkDrive "F:", True, True
    
    mDrive = "N:"
    strRemoteShare = "\\nas1\support"
    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")
    If Len(strUsr) >= minUSRnamelength then
    strUsr = Ucase(strUsr) & strUPNsuffix '<--- adds the UPNsuffix to the account
    if Len(strPas) >= 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
     
    Sub CancelScript
    Set oShell = Nothing
    Set objNetwork = Nothing
    Self.Close()
    End Sub
    </SCRIPT>
     
    <BODY STYLE="font:14 pt arial; color:white; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#000000', EndColorStr='#0000FF')">
    <a name="Top"></a><CENTER>
    <table border="0" cellpadding="0" cellspacing="0"><font size="2" color="black" face="Arial">
    <tr>
    <td height="30">
    <p align="right">Your Username</p>
    </td>
    <td height="30">&nbsp;&nbsp; <input type="text" name="UsrnameArea" size="30"></td></tr>
    <tr>
    <td height="30">
    <p align="right">@mycompany.net Password</p>
    </td>
    <td height="30">&nbsp;&nbsp; <input type="password" name="PasswordArea" size="30"></td></tr>
    </table><BR>
    <HR color="#0000FF">
    <Input id=runbutton class="button" type="button" value=" Create Drive " name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp; 
    <Input id=runbutton class="button" type="button" value="Cancel" name="cancel_button" onClick="CancelScript">
    </CENTER>
    </BODY>
    </HTML>
    Last edited by Rems; 14th October 2008, 19:41.

  • #2
    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 = "\\nas1\support"
     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 '<--- adds the UPNsuffix to the account ! see note!!!!!!
       If Len(strPas) >= 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
    Last edited by Rems; 20th October 2008, 20:52.

    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: Disconnect Mapped drive in HTA

      This forum rocks, thanks Mod Rems!!
      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

      Comment


      • #4
        Re: Disconnect Mapped drive in HTA

        Originally posted by jeffjenkins1 View Post
        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 \\server\share

        Replace
        Code:
        Err.Clear : objNetwork.RemoveNetworkDrive "F:", True, True
        If Err.Number = 0 Then Msgbox "Station F: succesfully disconnected"
        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:root\cimv2")
          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
        Last edited by Rems; 19th October 2008, 12:45.

        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


        • #5
          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?

          Comment

          Working...
          X