Jump to content

Error in If...Then syntax


Recommended Posts

I have a script to delete unwanted admin accounts  from our computers. I put the names into an array and then compare each member to a specific user name. If they don't match, I intend to delete them, but when my script enters the if statement I encounter an error.  

"M:\Work Docs\Scripts\AutoIT\ChangeLocalAdmin\DeleteLocalAdminTest.au3" (13) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $aNames[$i] <> "(desired account name)" Then
If ^ ERROR

It then exits with code 1.

#include<LocalAccount.au3>
#include<Array.au3>

Global $aNames = ''
Global $Output = ''

$aNames = _AccountEnum()

_ArrayDisplay($aNames)

For $i = 0 to UBound($aNames) - 1
    Next
    If $aNames[$i] <> "(desired name)" Then

        _AccountDelete($aNames[$i])

    EndIf

What is my mistake here? 

Link to comment
Share on other sites

  • Developers

What is the next doing at that spot?
Is this better?:

For $i = 0 to UBound($aNames) - 1
    If $aNames[$i] <> "(desired name)" Then
        _AccountDelete($aNames[$i])
    EndIf
Next

Jos

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

  • Moderators

@Tumulus Your for loop goes from 1 to Ubound($aNames) - 1, do a ConsoleWrite(Ubound($aNames)) to see if that number matches up.

"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

The array returned by _AccountEnum returns a 1-based array with the item count in 0 element.  

For $i = 1 to $aNames[0]

    If $aNames[$i] <> "(desired name)" Then

        _AccountDelete($aNames[$i])

    EndIf
Next

Also, by using _AccountEnum, it returns all local accounts admin or not.  Just look through the members of the local Administrators group to get Admin accounts.  

$aNames = _GroupEnumMembers("Administrators")

 

Adam

Link to comment
Share on other sites

I fixed both the Next positioning and checked the Ubound. It gives the correct number I need. I don't get the If syntax error anymore with the next fixed, but I am still not getting the accounts to actually delete. I've used this command in the UDF before, so it has to be something else. 

 

Link to comment
Share on other sites

And you are running it with a DA account, and #requireadmin?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Alright, I looked over the code and made a few changes based on all these suggestions. Looks like I was not quite correct in a number of places. 

Here is what I've got. I included #require admin (oops), used _GroupEnumMembers, checked the Ubound, and moved the Next. 

#RequireAdmin
#include<LocalAccount.au3>
#include<Array.au3>

Global $aNames = ''

$aNames = _GroupEnumMembers('Administrators')

_ArrayDisplay($aNames)

ConsoleWrite(UBound($aNames))

For $i = 1 to UBound($aNames) - 1

    If $aNames[$i] <> "(desired name)" Then

        _AccountDelete($aNames[$i])

    EndIf
Next

Still doesn't delete what I want, and oddly enough, I am not seeing my console write anymore...

Link to comment
Share on other sites

I added a ConsoleWrite so you can see what you are deleting.  Also, by your logic, you are deleting all accounts that do not match "(desired name)."  Is that what you want?  

RequireAdmin
#include<LocalAccount.au3>
#include<Array.au3>

Global $aNames = ''

$aNames = _GroupEnumMembers('Administrators')

_ArrayDisplay($aNames)

ConsoleWrite(UBound($aNames))

For $i = 1 to UBound($aNames) - 1

    If $aNames[$i] <> "(desired name)" Then
        ConsoleWrite("Account Deleted: " & $aNames[$i] & @CRLF)
        
        _AccountDelete($aNames[$i])

    EndIf
Next

 

Adam 

Link to comment
Share on other sites

Yes. We are trying to get rid of all admin accounts other than a desired account. We have a lot of computers that have different accounts or multiple accounts, so we are setting a default account on all computers and then running this script. 

Hmm... that would write exactly what I want to the console, but the odd thing is that nothing is being written there from either ConsoleWrite. Do you have any idea why? Did I perhaps change a setting by accident?

This is what my console shows after running the script:

>"C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "M:\Work Docs\Scripts\AutoIT\ChangeLocalAdmin\DeleteLocalAdminTest.au3" /UserParams    
+>15:47:21 Starting AutoIt3Wrapper v.14.801.2025.0 SciTE v.3.4.4.0   Keyboard:00000409  OS:WIN_81/  CPU:X64 OS:X64    Environment(Language:0409)
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper
>Running AU3Check (3.3.12.0)  from:C:\Program Files (x86)\AutoIt3  input:M:\Work Docs\Scripts\AutoIT\ChangeLocalAdmin\DeleteLocalAdminTest.au3
+>15:47:21 AU3Check ended.rc:0
>Running:(3.3.12.0):C:\Program Files (x86)\AutoIt3\autoit3.exe "M:\Work Docs\Scripts\AutoIT\Chan
geLocalAdmin\DeleteLocalAdminTest.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
+>15:47:21 AutoIt3.exe ended.rc:0
+>15:47:21 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.4974

Edited by Tumulus
Link to comment
Share on other sites

1 hour ago, Tumulus said:

but the odd thing is that nothing is being written there from either ConsoleWrite.

If you are also not seeing an arraydisplay, then blank consolewrites would be expected.   Add a couple of literal characters or some @CRLFs in the consolewrite, then you will see those regardless of the variable contents.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Still seeing nothing in the console. The array display does appear however...

#RequireAdmin
#include<LocalAccount.au3>
#include<Array.au3>

Global $aNames = ''

$aNames = _GroupEnumMembers('Administrators')

_ArrayDisplay($aNames)

ConsoleWrite(@CRLF & 'The bound is: ' & UBound($aNames) & @CRLF)

For $i = 1 to UBound($aNames) - 1

    If $aNames[$i] <> '(desired name)' Then
        ConsoleWrite(@CRLF & 'Account Deleted: ' & $aNames[$i] & @CRLF)

        _AccountDelete($aNames[$i])

    EndIf
Next

Any idea why?

Link to comment
Share on other sites

  • Developers

Close SciTE and start it again in Admin mode and try run the script again...  you will see a lot more!

Jos

Edited by Jos

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

Interesting. That totally fixed the problem and made the script run correctly... Why would that be? Does #requireadmin need to run in an administrative SciTE? I don't remember needing it before.

Link to comment
Share on other sites

  • Developers

The reason you need to have SciTE run at the same level is to be able to retrieve the STDOUT &STDERR information, else SciTE thinks the script finished immediately.

Jos 

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

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