Jump to content

Troubleshooting First Script


Recommended Posts

Hi,

I've just quickly put together a basic script to map two drives based on a username and password as my first exercise with AutoIt.

When executed it throws up an error saying "Subscript used on a non-accessible variable", I'm sure this will be me misunderstanding something basic but if I could be pointed in the right direction it would be appreciated.

;Requesting User input to create username and password variables

$username = InputBox("Enter your username", "Enter your username")
$password = InputBox("Enter your password", "Enter your password", "", "*")

;Creating yearlevel variable

$firsttwochar = StringMid ( $username, 1 [, count = 2] )
$yearlevel = "20" & $firsttwochar

;Mounting drives

$HDrivePath = "\\file-r2.giad.local\users\" & $yearlevel & "\" & $username

DriveMapAdd ( "H:", $HDrivePath, [, flags = 0 [, $username [, $password]]] )
DriveMapAdd ( "S:", "\\file-r2.giad.local\student\", [, flags = 0 [, $username [, $password]]] )

Cheers

Link to comment
Share on other sites

  • Moderators

Hi, Sparlou, welcome to the forum. You don't need the brackets; try something like this:

;Requesting User input to create username and password variables

$username = InputBox("Enter your username", "Enter your username")
$password = InputBox("Enter your password", "Enter your password", "", "*")

;Creating yearlevel variable

$firsttwochar = StringMid ( $username, 1, -2)
$yearlevel = "20" & $firsttwochar

;Mounting drives

$HDrivePath = "\\file-r2.giad.local\users\" & $yearlevel & "\" & $username

DriveMapAdd ( "H:", $HDrivePath, 0, $username, $password)
DriveMapAdd ( "S:", "\\file-r2.giad.local\student\", 0, $username, $password)

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

It just shows you the optional parameters you can use: The device in the first parameter and the remote share in the second are mandatory, the flags, username and password parameters are optional.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Hi. 

Sometimes.

Parameters are separated by commas. All parameters in [] can be omitted because they are optional. Will you set the second optional you have to set the first optional too. The first is then best set with the default which is written after the =.

There can be a lot defaults: 0, -1, True, False, "" and much more. Is nothing written as default after a = take "" as default.

Regards, Conrad

Edited by Simpel
Spelling
SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

To clarify, there are two different kinds of optional parameter, declared and undeclared. Here's your example from the help file:

DriveMapAdd ( "device", "remote share" [, flags = 0 [, "user" [, "password"]]] )

This function always expects the first two parameters, as they are not enclosed in square brackets. So you have to provide these for the function to work. Then comes the first opening square bracket, so everything that follows is optional.

The first optional parameter ($flags) has a declared default value of zero ("$flags = 0"). If you choose not to specify this value explicitly, the function will use that setting (It's a predefined fall-back, so the function does need this parameter to work, but if you don't tell it what to use, it'll use that predefined zero). Thus this call works:

DriveMapAdd ( "mydevicename", "my remote share name" )

because the function definition itself provides the missing, but required $flags parameter value. (NB even though the function works, the call may still fail if that drive you're mapping requires user credentials; in that case the function will return with a non-zero @error value, for example 2 = Access to the remote share was denied, see the function definition in the Help file).

Contrastingly, the last two optional parameters (user and password, both strings (within double quotation marks, each inside its own double brackets) are not predeclared (they are not followed by ' = "<some string>" ' in the function definition), which means, that IF you need to parse those, you need to provide their content yourself.

Finally, as stated earlier, optional parameters are nested, meaning each successive one you choose to include automatically entails having to specify all previous ones as well. So if you're going to parse the username string, you have to specify a value for $flags preceding it (because that's the order the function expects to receive the necessary information to do its job). You could do that like this (assuming the drive to map requires user name, but no password):

DriveMapAdd ( "mydevicename", "my remote share name" , 0 , "myusername" )
Edited by RTFC
clarification
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...