Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Find users in a group and find the created dates

Hi,

I want a scrip to find all the users in a specific group and find the users created dates in that group alone.As i need to keeps users for 3 months after the user is created in the group.So is there a way to do thse 2 tasks in 1 script.

regards
Sharath
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

:: Copy and Paste below code in notepad and save it with any name having .cmd extension.
:: SCRIPT START
@Echo Off
SET /P GroupDN=Enter Group Name or DN (enclosed in double quotes):
IF [%GroupDN%]==[] Echo No Group specified&&Goto :EndScript
Echo %GroupDN% |FIND /I "CN=" >NUL
IF NOT ERRORLEVEL 1 (
      DSQuery Group %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
) ELSE (
      DSQuery Group -Name %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
      DSQuery Group -Name %GroupDN% >GroupDN.tmp
      SET /p GroupDN=<GroupDN.tmp
      DEL /f /q GroupDN.tmp
)
Echo.
Echo Please wait while system generate report...
Echo.

DSQuery * -Filter "(&(sAMAccountType=805306368)(memberOf=%GroupDN%))" -Attr distinguishedName whenCreated >WhenCreatedRpt.Txt

Echo Script finish check 'WhenCreatedRpt.Txt' file.
Echo.
:EndScript
IF EXIST GroupDN.tmp DEL /F /Q GroupDN.tmp
:: SCRIPT END
Avatar of bsharath

ASKER

Farhan

I tried When i enter the groupname it just closes in prompt without any output.
"Groupname"
Enter
Closes the screen

What group name did you enter?
"Chennai-fs" is the group name
Apply following command and paste results

Click Start -> Run -> Cmd.exe
DSQuery Group -Name "Chennai-fs"
No output.
These groups are in the root domain....
I have tried for the local groups a file is created "WhenCreatedRpt.Txt" but no data inside...
What local group name did you type? Type that group name in following command and post results.

DSQuery Group -Name "GroupName"
                                             ^--------------- Group Name here
I have tried with a local group.

Chennai_FS-SG
It shows the Ou path
The same when i try with the .cmd file it creates a file but no data in it.
Can you plz post the output of following:

DSQuery Group -Name "Chennai_FS-SG"
C:\>DSQuery Group -Name "Chennai_FS-SG"
"CN=Chennai_FS-SG,OU=Security Groups,OU=IND,OU=Countries,DC=Development,DC=Group,DC=co,DC=uk"
Did you try this script with "Chennai_FS-SG" group name? and also try with "CN=Chennai_FS-SG,OU=Security Groups,OU=IND,OU=Countries,DC=Development,DC=Group,DC=co,DC=uk"
Farhan for both the file is created but no data to the file.

This is a security group....
I tried with many groups and many machines same thing happens.File gets created but no data.
Post results of following script.

:: Copy and Paste below code in notepad and save it with any name having .cmd extension.
:: SCRIPT START
@Echo Off
SET /P GroupDN=Enter Group Name or DN (enclosed in double quotes):
IF [%GroupDN%]==[] Echo No Group specified&&Goto :EndScript
Echo %GroupDN% |FIND /I "CN=" >NUL
IF NOT ERRORLEVEL 1 (
      DSQuery Group %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
) ELSE (
      DSQuery Group -Name %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
      DSQuery Group -Name %GroupDN% >GroupDN.tmp
      SET /p GroupDN=<GroupDN.tmp
      DEL /f /q GroupDN.tmp)
Echo.
Echo Group Name: %GroupDN%
Echo %GroupDN%
Echo.
Echo Members:
DSGet Group %GroupDN% -members
:EndScript
:: SCRIPT END
Farhan i get this.

Enter Group Name or DN (enclosed in double quotes): "chennai_fs-sg"

Group Name: "CN=Chennai_FS-SG,OU=Security Groups,OU=IND,OU=Countries,DC=Developm
ent,DC=Group,DC=co,DC=uk"
"CN=Chennai_FS-SG,OU=Security Groups,OU=IND,OU=Countries,DC=Development,DC=
Group,DC=co,DC=uk"

Members:
"CN=Fer,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countries,DC=dev
,DC=Group,DC=co,DC=uk"
"CN=Mj kumar,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countries,DC=dev
pment,DC=Group,DC=co,DC=uk"
"CN=Rh k. Kannan,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countries,DC=D
evelopment,DC=Group,DC=co,DC=uk"
"CN=Man Kay,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countri
es,DC=dev,DC=Group,DC=co,DC=uk"
"CN=Vpal Ke,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countries,DC=D
evelopment,DC=Group,DC=co,DC=uk"
"CN=Smar Ye,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countries,D
C=dev,DC=Group,DC=co,DC=uk"
"CN=Biri,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countries,DC=Devel
opment,DC=Group,DC=co,DC=uk"
"CN=Ani Ku,OU=Named Accounts,OU=User Accounts,OU=IND,OU=Countries,DC=Dev
elopment,DC=Group,DC=co,DC=uk"

Press any key to continue . . .


When i try for a root group do not get anything...

<edited by SouthMod per https://www.experts-exchange.com/questions/26631502/26-Nov-10-15-Automated-Request-for-Attention-Q-22793506.html?cid=1572&anchorAnswerId=34215138#a34215138>
:: Copy and Paste below code in notepad and save it with any name having .cmd extension.
:: SCRIPT START
@Echo Off
SET /P GroupDN=Enter Group Name or DN (enclosed in double quotes):
IF [%GroupDN%]==[] Echo No Group specified&&Goto :EndScript
Echo %GroupDN% |FIND /I "CN=" >NUL
IF NOT ERRORLEVEL 1 (
      DSQuery Group %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
) ELSE (
      DSQuery Group -Name %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
      DSQuery Group -Name %GroupDN% >GroupDN.tmp
      SET /p GroupDN=<GroupDN.tmp
      DEL /f /q GroupDN.tmp)
Echo.
Echo Please wait while system generate report...
Echo.
DSQuery * -Filter "(memberOf=%GroupDN%)" -Attr distinguishedName whenCreated >WhenCreatedRpt.Txt
Echo Script finish check 'WhenCreatedRpt.Txt' file.
Echo.
:EndScript
:: SCRIPT END

Farhan i dont get any thing.After i enter the group name the scree just disappears...

I get this...

Enter Group Name or DN (enclosed in double quotes): "Chennai_FS-SG"

Please wait while system generate report...

dsquery failed:Value for `startnode' has incorrect format.
type dsquery /? for help.Script finish check 'WhenCreatedRpt.Txt' file.

Press any key to continue . . .
:: Copy and Paste below code in notepad and save it with any name having .cmd extension.
:: SCRIPT START
@Echo Off
IF EXIST WhenCreatedRpt.Txt DEL /F /Q WhenCreatedRpt.Txt
SET /P GroupDN=Enter Group Name or DN (enclosed in double quotes):
IF [%GroupDN%]==[] Echo No Group specified&&Goto :EndScript
Echo %GroupDN% |FIND /I "CN=" >NUL
IF NOT ERRORLEVEL 1 (
      DSQuery Group %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
) ELSE (
      DSQuery Group -Name %GroupDN% 2>NUL |FIND /I "CN=" >NUL
      IF ERRORLEVEL 1 Echo Invalid Group DN&&Goto :EndScript
      DSQuery Group -Name %GroupDN% >GroupDN.tmp
      SET /p GroupDN=<GroupDN.tmp
      DEL /f /q GroupDN.tmp)
Echo.
Echo Please wait while system generate report...
Echo.
FOR /F "delims=*" %%m IN ('DSGet Group %GroupDN% -Members') DO DSQuery User %%m -disabled >>WhenCreatedRpt.Txt
Echo Script finish check 'WhenCreatedRpt.Txt' file.
Echo.
:EndScript
:: SCRIPT END
Farhan i get this.

Enter Group Name or DN (enclosed in double quotes):"chennai_fs-sg"

Please wait while system generate report...

Script finish check 'WhenCreatedRpt.Txt' file.

Press any key to continue . . .
When i check the file it has no data...
Inside "chennai_fs-sg" i have 7 users
When i enter a root domain's group says invalid group...
Are you sure "chennai_fs-sg" group contain users that are disabled?
Apply following and post results:

Click Start -> Run -> Cmd.exe

DSQuery * -Filter "(memberOf="CN=Chennai_FS-SG,OU=Security Groups,OU=IND,OU=Countries,DC=Developm
ent,DC=Group,DC=co,DC=uk")" -Attr distinguishedName whenCreated
Farhan will this output only users that are disabled...
Then can you change the script to accept even root groups...
Farhan i get this.

C:\>DSQuery * -Filter "(memberOf="CN=Chennai_FS-SG,OU=Security Groups,OU=IND,OU=
Countries,DC=Development,DC=Group,DC=co,DC=uk")" -Attr distinguishedName wh
enCreated
dsquery failed:Value for `startnode' has incorrect format.
type dsquery /? for help.
C:\>
farhankazi:Are you sure "chennai_fs-sg" group contain users that are disabled?
I dont get this.I just need to get all the users in a group with there created dates of all the members
Any help...
Sorry for my last comment, I think I mixed two posts!!  
Modifying the script and will post in few minutes...
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Farhan i checked with some security groups it works but not on Parent domain Groups....
Search with Group DN instead of its name.
Like this.

Cn=admin, OU=Service Accounts DC=Development,DC=Group,DC=co,DC=uk
yes, you can get exact DN using following command.

Click Start -> Run -> Cmd.exe
DSQuery Group -Name GroupName

Now above command will display exact DN of the Group. Copy and paste this DN to script input.
Thanks a lot Farhan.I was just about to ask you for this but you understood...:)
Farhan
How can i get the root domain's Groups....
What is the command to get all groups Dn of root domain
Click Start -> Run -> Cmd.exe

DSQuery Group ForestRoot -Name * -Limit 0
Above command will display all groups from forest root.

DSQuery Group ForestRoot -Name GroupName
Above command will search for group name in forest root