Jump to content

Map Network Drive With Password Prompt


Recommended Posts

Hello,

I feel as though this may be an easy question for you experts out there, but Ill ask anyway. I would like to utilize AutoIT for script that would prompt a user for their username and password. I know that this can done a variety of ways, but I am unsure what the easiest rout is. The computers that the script will be running from are on a WORKGROUP and not part of the domain that has network shares.

Ive been looking at this script that member Zedna wrote, but I tested it and it didn't seem like it was working from a computers outside the domain.

#AutoIt3Wrapper_icon=your_icon.ico
#AutoIt3Wrapper_Run_Obfuscator=y
#obfuscator_parameters=/striponly
#NoTrayIcon

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiStatusBar.au3>

Global $ext = StringRight(@ScriptName, 4) ; .exe or .au3
Global $ini = StringReplace(@ScriptName,$ext,'.ini')

Global $ini_device = IniRead($ini, "Options", "Device", "X:")
Global $ini_share = IniRead($ini, "Options", "Share", "\\filerserver\")
If StringRight($ini_share, 1) <> "\" Then $ini_share &= "\"

$Form1 = GUICreate("Connect To Your Drive", 265, 140)
$username_id = GUICtrlCreateInput("", 88, 16, 153, 21)
$password_id = GUICtrlCreateInput("", 87, 44, 153, 21, $ES_PASSWORD)
GUICtrlCreateLabel("&Username", 24, 16, 52, 17)
GUICtrlCreateLabel("&Password", 26, 46, 50, 17)
$connect = GUICtrlCreateButton("&Connect", 24, 80, 217, 33, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
_GUICtrlStatusBar_SetText($StatusBar1, $ini_device & " --> " & $ini_share)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $connect
            Connect()
    EndSwitch
WEnd

Func Connect()
    $username = GUICtrlRead($username_id)
    $password = GUICtrlRead($password_id)
    
    If $username = '' Or $password = '' Then
        MsgBox(16, 'Error', 'Empty username or password')
        Return
    EndIf
    
    If DriveMapGet($ini_device) <> '' Then ; very fast
        MsgBox(16, 'Error', 'The device is already assigned')
        Return
    EndIf
    
    GUISetCursor(15,1)
    DriveMapAdd($ini_device, $ini_share & $username, 0, $username, $password) ; slow
    If @error Then
        Switch @error
            Case 1 
                $err_message = 'Undefined / Other error. Windows API return code: ' & @extended
            Case 2 
                $err_message = 'Access to the remote share was denied'
            Case 3 
                $err_message = 'The device is already assigned'
            Case 4 
                $err_message = 'Invalid device name'
            Case 5 
                $err_message = 'Invalid remote share'
            Case 6 
                $err_message = 'Invalid password'
        EndSwitch
        GUISetCursor(2)
        MsgBox(16, 'Error', $err_message)
    Else ; everything OK
        Exit
    EndIf
EndFunc
Link to comment
Share on other sites

The most super simple way to get user input from a code standpoint is InputBox() but with the GUI creation tools out there you can make a GUI very easily as well and it could be a bit better presentation and give you more options/expandability. 

 

So I would start with basic first, get it working and then improve as needed/desired.

For mapping drives I still use CMD Net Use as DriveMapAdd kind was flaky for me.

;Just an example may not be runnable code
$sUser = InputBox("Input Your Username", "Input Your Username")
$sPass = InputBox("Input Your Password", "Input Your Password")

Run(@ComSpec & " /k Net Use H: \\MyDirectory\Share /Persistent:Yes /User:" & $sUser & " " & $sPass)

Depending on your environment if the connection username is the same as the current user name just use the macro @Username and don't ask the person to enter it.   You can add the @SW_HIDE and such to the cmd so that you do not see the cmd window open briefly.  Etc.

Link to comment
Share on other sites

Did you try to put the domain name before the username ?

DriveMapAdd($ini_device, $ini_share & $username, 0, "DomainName\" & $username, $password) ; replace DomainName by the good one


 

Link to comment
Share on other sites

error 2202 is ERROR_BAD_USERNAME : https://msdn.microsoft.com/en-us/library/windows/desktop/ms681386(v=vs.85).aspx

Can you try with the net use command in a command prompt ? Look at the syntax ViciousXUSMC used in #2, and test with or without the domain name before the username.

 

Link to comment
Share on other sites

The most super simple way to get user input from a code standpoint is InputBox() but with the GUI creation tools out there you can make a GUI very easily as well and it could be a bit better presentation and give you more options/expandability. 

 

So I would start with basic first, get it working and then improve as needed/desired.

For mapping drives I still use CMD Net Use as DriveMapAdd kind was flaky for me.

;Just an example may not be runnable code
$sUser = InputBox("Input Your Username", "Input Your Username")
$sPass = InputBox("Input Your Password", "Input Your Password")

Run(@ComSpec & " /k Net Use H: \\MyDirectory\Share /Persistent:Yes /User:" & $sUser & " " & $sPass)

Depending on your environment if the connection username is the same as the current user name just use the macro @Username and don't ask the person to enter it.   You can add the @SW_HIDE and such to the cmd so that you do not see the cmd window open briefly.  Etc.

So I actually got this to work. I no have two questions:

1. How do I suppress the command prompts from coming up?

2. How do I add a share with a space in it. like \\server\my share vs \\server\myshare do I need to use quotes?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...