Jump to content

Mapping Network Drive With a prompt


 Share

Recommended Posts

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?

Link to comment
Share on other sites

  • Developers

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

"\\FILESERVER\" & $user

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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
Link to comment
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 ...

Link to comment
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

Link to comment
Share on other sites

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
Link to comment
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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 5 years later...

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

Link to comment
Share on other sites

  • 2 years later...
  • 3 years later...

I know that it's too late :-)

but here is general improvement based on question in previous post and some later PM: 

in case of undefined/other error now it will show not only error code but also exact (system) error text obtained by GetLastError + FormatMessage API calls in new function _GetLastErrorMessage()

Note: Code is in syntax for old AutoIt's version (#Autoit3Wrapper) but runs also on latest Autoit's version only with warning

#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
                $err_message &= @CRLF & _GetLastErrorMessage()
            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

;===============================================
;    _GetLastErrorMessage($DisplayMsgBox="")
;    Format the last windows error as a string and return it
;    if $DisplayMsgBox <> "" Then it will display a message box w/ the error
;    Return        Window's error as a string
;===============================================
Func _GetLastErrorMessage ($DisplayMsgBox = "")
    Local Const $FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
    Local $ret, $s
    Local $p = DllStructCreate("char[4096]")
    If @error Then Return ""

    $ret = DllCall("Kernel32.dll", "int", "GetLastError")

    $ret = DllCall("kernel32.dll", "int", "FormatMessage", _
            "int", $FORMAT_MESSAGE_FROM_SYSTEM, _
            "ptr", 0, _
            "int", $ret[0], _
            "int", 0, _
            "ptr", DllStructGetPtr($p), _
            "int", 4096, _
            "ptr", 0)
    $s = DllStructGetData($p, 1)
    $p = 0
    If $DisplayMsgBox <> "" Then MsgBox(0, "_GetLastErrorMessage", $DisplayMsgBox & @CRLF & $s)
    Return $s
EndFunc   ;==>_GetLastErrorMessage

 

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