You are not logged in.
Pages: 1
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
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!
~bxm
Last edited by bluesxman (17 Feb 2007 11:35)
cmd | *sh | ruby | chef
Offline
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
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 ). 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
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
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
Pages: 1