Jump to content

Help with stringregexp


 Share

Recommended Posts

Hi am using the AD UDF by @water trying to get a list of users in a certain group however the list of users that gets returned has the FQDN and I would like to return only their Name in this case "John Doe".  I am trying to use StringRegExp to get that but I can't get it to work.

Here is a test string: CN=John Doe,OU=Standard,OU=Users

Link to comment
Share on other sites

Not working

#RequireAdmin
#include <AD.au3>
#include <StringConstants.au3>
#include <String.au3>


_AD_Open("user", "pass")
If @error Then
    MsgBox(0, "", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

Local $Users = _AD_GetGroupMembers("ESISoftwareUsers")
If @error Then
    MsgBox(0, "", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

_ArrayDisplay($Users) ;Debug

For $i = 1 To UBound($Users) - 1
    Local $Name = _StringBetween($Users[$i], "CN=", ",")
    ConsoleWrite($Name & @CRLF)
Next


_AD_Close()

I'm missing something

Link to comment
Share on other sites

You could try something like:

#include <Constants.au3>
#include <Array.au3>

example()

Func example()
    Const $kDATA = "CN=John Doe,OU=Standard,OU=Users"
    Local $aArray = StringRegExp($kDATA, "\bCN=([^,]*)", $STR_REGEXPARRAYMATCH)
    _ArrayDisplay($aArray)
EndFunc

 

Link to comment
Share on other sites

I'm having issues looping through the array and I'm getting a blank message box with nothing.

#RequireAdmin
#include <AD.au3>
#include <StringConstants.au3>
#include <String.au3>
#include <Array.au3>


_AD_Open("", "")
If @error Then
    MsgBox(0, "AD Connection", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

Local $Users = _AD_GetGroupMembers("ESISoftwareUsers")
If @error Then
    MsgBox(0, "Get Group Members", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

;_ArrayDisplay($Users) ;Debug

For $i = 1 To $Users[0]
    Local $Name = _StringBetween($Users[$i], "CN=", ",")
    MsgBox(0, "", $name)
Next

_AD_Close()

 

Link to comment
Share on other sites

According to the help file, _StringBetween returns "a 0-based array - element [0] contains the first found string".

 

change:

Local $Name = _StringBetween($Users[$i], "CN=", ",")
    MsgBox(0, "", $name)

 

To:

Local $aName = _StringBetween($Users[$i], "CN=", ",")
    MsgBox(0, "", $aName[0])

 

The help file is your friend :)

Link to comment
Share on other sites

1 hour ago, TheXman said:

According to the help file, _StringBetween returns "a 0-based array - element [0] contains the first found string".

 

change:

Local $Name = _StringBetween($Users[$i], "CN=", ",")
    MsgBox(0, "", $name)

 

To:

Local $aName = _StringBetween($Users[$i], "CN=", ",")
    MsgBox(0, "", $aName[0])

 

The help file is your friend :)

 

Hi @TheXman

I am receiving this error:

"C:\Temp\AD.au3" (25) : ==> Subscript used on non-accessible variable.:
MsgBox(0, "", $Name[0])
MsgBox(0, "", $Name^ ERROR

 

#RequireAdmin
#include <AD.au3>
#include <StringConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <AutoItConstants.au3>


_AD_Open("", "")
If @error Then
    MsgBox(0, "AD Connection", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

Local $Users = _AD_GetGroupMembers("ESISoftwareUsers")
If @error Then
    MsgBox(0, "Get Group Members", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

_ArrayDisplay($Users) ;Debug

For $i = 0 To UBound($Users) - 1
    Local $Name = _StringBetween($Users[$i], "CN=", ",")
    MsgBox(0, "", $Name[0])
Next

_AD_Close()

 

Edited by antmar904
Link to comment
Share on other sites

I didn't add any error checking.  You probably are encountering a line which has no matches.  You need to ensure that you have a valid array result before referencing an array element.

You can use IsArray() or checking to see if the _StringBetween function succeeded.

Edited by TheXman
Link to comment
Share on other sites

Wish I found _AD_FQDNToSamAccountName before trying RegEx or StringSplit :)

This is working for me.

Thanks again all.

#RequireAdmin
#include <AD.au3>
#include <StringConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <AutoItConstants.au3>


_AD_Open("user", "pass")
If @error Then
    MsgBox(0, "AD Connection", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

Local $Users = _AD_GetGroupMembers("ESISoftwareUsers") ;Returns the users FQDN. Exp: CN=First Last,OU=Standard,OU=Users,OU=Mexico,DC=na,DC=domain,DC=com
If @error Then
    MsgBox(0, "Get Group Members", "Error" & @error & "Extended" & @extended)
    Exit
EndIf

;_ArrayDisplay($Users) ;Debug

For $i = 1 To UBound($Users) - 1
    Local $Name = _AD_FQDNToSamAccountName($Users[$i])
    If @error Then
        MsgBox(0, "_AD_FQDNToSamAccountName error", "Error" & @error)
        Exit
    EndIf

    Local $Results = _AD_IsObjectDisabled($Name)
    If $Results = @error Then
        ConsoleWrite($Name & " - enabled" & @CRLF)
    Else
        ConsoleWrite($Name & " - disabled" & @CRLF)
    EndIf
Next

_AD_Close()

 

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