Sign in to follow this  
Followers 0
Ferman

Mapping Network Drive With a prompt

16 posts in this topic

Hey everyone I am currently trying to make an autoit script that asks for a username and password before it maps the drive..

$user = InputBox("I did this.", "Please enter in your username.")
$password = InputBox("And this.", "Please enter in your password.")
DriveMapAdd("Z:", "\\FILESERVER\$user", 0, "$user", "$password")

It looks like it would work but for some reason it isnt. Anyone have an idea why?

Share this post


Link to post
Share on other sites



Variables should not be enclosed in double quotes.

Jos

Alright I just took the quotes out off of $user and $password.... but should i take the quotes off of "\\FILESERVER\$user"?

Share this post


Link to post
Share on other sites

Just concatenate literal string "\\FILESERVER\" with variable $user by an &:

"\\FILESERVER\" & $user

SWEET!!! Thanks!

My cousin just showed me autoit and its amazing so far! And it looks like the community is very helpful :x.

Thanks

Share this post


Link to post
Share on other sites

Alright so i got everything working but id like to make it a bit prettier.. I used Koda GUI designer and it looks good but I am unsure what func to use when the connect button is pressed... I was either thinking "MouseClick" or "GUICtrlSetOnEvent"...

Here is what I was thinking but im sure its wrong. Because ive looked at some source of some scripts people made and it doesnt look like mine on the SetOnEvent function...

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\Users\Justin\Desktop\AutoIt GUI\Forms\NetowrkDriveConnect.kxf
$Form1 = GUICreate("Connect To Your Drive", 265, 135, 633, 213)
$username = GUICtrlCreateInput("", 88, 16, 153, 21)
$password = GUICtrlCreateInput("", 87, 44, 153, 21)
$user = GUICtrlCreateLabel("Username", 24, 16, 52, 17)
$pass = GUICtrlCreateLabel("Password", 26, 46, 50, 17)
$connect = GUICtrlCreateButton("Connect", 24, 80, 217, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlSetOnEvent ( Connect, "DriveMapAdd("X:", "\\filerserver\" & $username, 0, $username, $password)" )


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

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

$Form1 = GUICreate("Connect To Your Drive", 265, 135, 633, 213)
$username_id = GUICtrlCreateInput("", 88, 16, 153, 21)
$password_id = GUICtrlCreateInput("", 87, 44, 153, 21)
GUICtrlCreateLabel("Username", 24, 16, 52, 17)
GUICtrlCreateLabel("Password", 26, 46, 50, 17)
$connect = GUICtrlCreateButton("Connect", 24, 80, 217, 33)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $connect
            $username = GUICtrlRead($username_id)
            $password = GUICtrlRead($password_id)
            DriveMapAdd("X:", "\\filerserver\" & $username, 0, $username, $password)
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

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

$Form1 = GUICreate("Connect To Your Drive", 265, 135, 633, 213)
$username_id = GUICtrlCreateInput("", 88, 16, 153, 21)
$password_id = GUICtrlCreateInput("", 87, 44, 153, 21)
GUICtrlCreateLabel("Username", 24, 16, 52, 17)
GUICtrlCreateLabel("Password", 26, 46, 50, 17)
$connect = GUICtrlCreateButton("Connect", 24, 80, 217, 33)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $connect
            $username = GUICtrlRead($username_id)
            $password = GUICtrlRead($password_id)
            DriveMapAdd("X:", "\\filerserver\" & $username, 0, $username, $password)
    EndSwitch
WEnd

Thank you very much!! I am thinking about adding a success window so when connect is pressed you get a windows saying it is connected or it didnt work.. :x

Share this post


Link to post
Share on other sites

Thank you very much!! I am thinking about adding a success window so when connect is pressed you get a windows saying it is connected or it didnt work.. :x

I think it will be better to just immediatelly close this window if everything is OK (to not bother users)

and show error message if something is wrong only.

You should add test for result or @error from DriveMapAdd for this ...

Share this post


Link to post
Share on other sites

I think it will be better to just immediatelly close this window if everything is OK (to not bother users)

and show error message if something is wrong only.

You should add test for result or @error from DriveMapAdd for this ...

Thats a better idea. Thanks Ill work on adding this. :x

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Here is improved version.

changelog:

- custom application's icon

- smaller EXE file (Obfuscator /striponly)

- hide tray icon

- window centered on desktop

- added $ES_PASSWORD style to hide password chars behind asterisk

- added keyboard shortcuts to labels/button (by &)

- added $BS_DEFPUSHBUTTON style to Connect button

- added check for valid (not empty) username/password

- test if device is already assigned by DriveMapGet (fast)

- added hourglass cursor during execution of DriveMapAdd

- added Error messagebox with apropriate error message

- if everything is OK then immediatelly close window

#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>

$Form1 = GUICreate("Connect To Your Drive", 265, 135)
$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))
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $connect
            $username = GUICtrlRead($username_id)
            $password = GUICtrlRead($password_id)
            
            If $username = '' Or $password = '' Then
                MsgBox(16, 'Error', 'Empty username or password')
                ContinueLoop
            EndIf
            
            If DriveMapGet("X:") <> '' Then ; very fast
                MsgBox(16, 'Error', 'The device is already assigned')
                ContinueLoop
            EndIf
            
            GUISetCursor(15,1)
            DriveMapAdd("X:", "\\filerserver\" & $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
            
    EndSwitch
WEnd
Edited by Zedna

Share this post


Link to post
Share on other sites

Here is new version with added INI configuration

so you can easily customize Device letter or server Share just by editing of INI file without recompiling EXE.

There is default value inside EXE so INI is not required.

INI file have the same name as script with INI extension

[Options]

Device=X:

Share=\\filerserver\

#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>

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, 135)
$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))
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $connect
            $username = GUICtrlRead($username_id)
            $password = GUICtrlRead($password_id)
            
            If $username = '' Or $password = '' Then
                MsgBox(16, 'Error', 'Empty username or password')
                ContinueLoop
            EndIf
            
            If DriveMapGet($ini_device) <> '' Then ; very fast
                MsgBox(16, 'Error', 'The device is already assigned')
                ContinueLoop
            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
            
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Another improved version:

Code for button Connect moved from main GUI loop into function Connect()

#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>

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, 135)
$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))
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
Edited by Zedna

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Added statusbar showing mapping information:

(slightly bigger window height)

#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
Edited by Zedna

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

You sir are a boss!

Edited by Ferman

Share this post


Link to post
Share on other sites

Hello,
first of all, thank you very much for that script, it is nealy that what I need.
As an absolute beginner I was able to follow most of the functions.

I tried to make some company specific changes, but some parts are not working...

Description of what I did and need:

  • I build a form with Koda and added some radio buttons for our company locations.
  • Users have to activate their Home location.
  • Based on the selction the script should do the following:
    • map global shares (works)
    • map location specific shares (works)
    • map user homedrive (location specific, line 70, doesn't work)

What I need:

  • Error message that no location was selected, then go back to start.
  • a status message which drive mappings were made (nice to have)

a shot description how to get our company logos into the compiled .exe (I think this should be done with filecopy)

Thanks in advance

 Matthias
 

MapDrives.au3

Share this post


Link to post
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
Sign in to follow this  
Followers 0