You are not logged in.

#1 13 Feb 2007 12:52

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Find empty Workgroups

Here's a handy script to list all empty workgroups in a domain

@echo off
:: Find Workgroups and distributions lists with no members
setlocal ENABLEDELAYEDEXPANSION

:: List all the groups in Active Directory
FOR /f "Tokens=*" %%G in ('dsquery group -limit 5000') Do (
 call :sub_check_group %%G >nul 2>&1
 if "!_member_found!" EQU "N" @echo %%G
)

endlocal
pause
goto :EOF

:sub_check_group
:: Set a flag if this group contains one or more members
set _member_found=N
FOR /f "Tokens=*" %%Z in ('dsget group %1 -members') Do (
 set _member_found=Y
)

Offline

#2 17 Feb 2007 11:34

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Find empty Workgroups

Whilst I do like "enabledelayedexpansions" I tend to avoid using it unless it's really necessary, just in case a rogue "!" in a file name, or whatever, trips me up.

Therefore, I'd have written it more like this:

@echo off
:: Find Workgroups and distributions lists with no members

:: List all the groups in Active Directory
FOR /f "Tokens=*" %%G in ('dsquery group -limit 5000') Do (
 call :sub_check_group %%G >nul 2>&1
 if not defined _member_found echo %%G
)

pause
goto :EOF

:sub_check_group
:: Set a flag if this group contains one or more members
set _member_found=
FOR /f "Tokens=*" %%Z in ('dsget group %1 -members') Do (
 set _member_found=Y
)

*untested*

But that's just me! smile

~bxm

Last edited by bluesxman (17 Feb 2007 11:35)


cmd | *sh | ruby | chef

Offline

#3 17 Feb 2007 11:53

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Find empty Workgroups

yes your version is probably easier to follow
I'm wondering how a rogue filename could ever affect this though?

Even if I put a file called !_member_found! in the same folder, you'd have to be using the syntax

IF exist !_member_found! ...

instead of

IF !_member_found! EQU...

Offline

#4 17 Feb 2007 12:13

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Find empty Workgroups

Sorry, I didn't mean explicitly for that script (though a "!" in the name of an AD Group could prove troublesome -- please note the "or whatever" qualifier used above smile).  Try this simple script and you'll see what I mean:

@echo off

type nul>asd!fgh.txt
type nul>asd!fghjkl!.txt
type nul>asd.txt

echo:Real:
dir /b asd*

echo:

setlocal enabledelayedexpansion

echo:Fubar:
for %%a in (*.txt) do echo:%%~a

endlocal

del asd.txt
del asd!fgh.txt
del asd!fghjkl!.txt

pause

Last edited by bluesxman (17 Feb 2007 12:14)


cmd | *sh | ruby | chef

Offline

#5 17 Feb 2007 12:31

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Find empty Workgroups

Yes that is nasty, and if you try to do

Del "!some_text!"

It will evaluate as DEL "" (because the variable hasn't been set) and Windows will assume that's a wildcard delete!!

Offline

#6 17 Feb 2007 12:48

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Find empty Workgroups

Eek, hadn't even entertained that prospect -- I just put the "del" commands at the end so that the script would be well behaved and tidy up after itself (hence them being without the setlocal/endlocal environment).

But yes, this "feature" is a complete arse-pain if one is trying to write an entirely robust script.  Though, drifting OT a little further, there is at least one thing I've never found a way to mitigate.  Actually, I'll start a new thread for it.


cmd | *sh | ruby | chef

Offline

Board footer

Powered by