Jump to content

Stumped...


Recommended Posts

I am writing a windows service (with AutoIT) that loops through the system and pulls all the local system accounts into an array. I got all of that working. The part I am stumped on is ...

The array returns all the accounts and I only want to deal with the accounts that have a specific naming convention:

xy123456

All of the accounts contain 8 characters and they always start with 2 letters and end with 6 numbers.

I need the code to create a new array that contains only the accounts with that naming convention ( or just trim off the elements that don't meet that convention ).

Does anyone have any suggestions?

Thanks!

Link to comment
Share on other sites

You could go thru them and test with StringRegExp():

Global $aInput[5] = ["123456", "xy", "xy123456", "xy12345", "xy1234567"]
For $n = 0 to UBound($aInput) - 1
    If StringRegExp($aInput[$n], "\A[[:alpha:]]{2}\d{6}\Z", 0) Then
        ConsoleWrite("[" & $n & "] = " & $aInput[$n] & " = MATCH" & @LF)
    Else
        ConsoleWrite("[" & $n & "] = " & $aInput[$n] & " = NOT MATCH" & @LF)
    EndIf
Next

How did you get the list of accounts? If it was by WMI, you might be able to craft your WQL to just return those in the first place. Something like (not tested):

'SELECT * FROM Accounts WHERE Name LIKE "[a-z][a-z][1-9][1-9][1-9][1-9][1-9][1-9]"'

:idea:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You could go thru them and test with StringRegExp():

Global $aInput[5] = ["123456", "xy", "xy123456", "xy12345", "xy1234567"]
For $n = 0 to UBound($aInput) - 1
    If StringRegExp($aInput[$n], "\A[[:alpha:]]{2}\d{6}\Z", 0) Then
        ConsoleWrite("[" & $n & "] = " & $aInput[$n] & " = MATCH" & @LF)
    Else
        ConsoleWrite("[" & $n & "] = " & $aInput[$n] & " = NOT MATCH" & @LF)
    EndIf
Next

How did you get the list of accounts? If it was by WMI, you might be able to craft your WQL to just return those in the first place.

:idea:

Thank You! Yes it is a WMI call:

;--------------------------------------------------------------------------------
; Function Declaration: SYSTEMUSERS()
;   This Function Loops through and returns an array of local account names.
;--------------------------------------------------------------------------------
Func _SystemUsers($AccountType = 0)
    Local $aSystemUsers
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $strComputer = "localhost"

    If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '')
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $Output = StringSplit($objItem.PartComponent, ',')
            If IsArray($Output) Then
                $Temp1 = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '')
                If $AccountType = 0 Or ($AccountType = 1 And @ComputerName = $Temp1) Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                ElseIf $AccountType = 2 And @ComputerName <> $Temp1 Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                EndIf
            EndIf
        Next
        $aSystemUsers = StringTrimRight($aSystemUsers, 1)
        If $aSystemUsers = '' Then Return(SetError(1, 1, $aSystemUsers))
        Return(SetError(0, 0, StringSplit($aSystemUsers, '|')))
    Else
        $aSystemUsers = ''
        Return(SetError(2, 2, $aSystemUsers))
    EndIf
EndFunc   ;==>_SystemUsers

I'm not very well versed in WMI - I actually got that snippet of code from a forum post here and I am just using it as a function. This same bit of code is also used to match up profile names and what not.

Basically I am writing a service that will loop through and remove any student accounts and move their profiles to C:\Temp and then purge them out of there when they expire. I creating an expiration date by creating a file in the root of the profile named: expire.txt which has a numeric value for the extirpation date.

The goal is to clean up and prevent machines across our school district from piling up profiles and accounts for students.

I've attached the .au3 file that I am in middle of coding. :)

Thanks again for the help! :(

PPS-ProfileMgmt.au3

Edited by aseitz
Link to comment
Share on other sites

Thanks again - that StrRegExp saved my life. I'd have torn out all my hair and started in on my co-workers.

If StringRegExp($CurrentAccount, "\A[[:alpha:]]{2}\d{6}\Z", 0) Then
            ConsoleWrite("[" & $Counter & "] = " & $CurrentAccount & " = MATCH" & @LF)
            MsgBox(1,"Match","Matched: " & $CurrentAccount )
    Else
            ConsoleWrite("[" & $Counter & "] = " & $CurrentAccount & " = NOT MATCH" & @LF)
            MsgBox(1,"No Match","Not Matched: " & $CurrentAccount )
    EndIf

THANKS!

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