• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

clean up output in log file

Hi again Experts.

While using the code below, I get this output in the log file:

CN=a,OU=groupname,OU=b,OU=c,OU=d,DC=e,DC=f,DC=g,DC=h

[I only want OU=groupname to be displayed in the log file]

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=a,OU=b,OU=c,OU=d,DC=e,DC=f,DC=g,DC=h" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% > %ou%-group-members.csv
::pause
for /f "tokens=*" %%b in ('dsget group %ougroup% -members ^|sort') do (call :usersamid %%b)
goto :eof

:usersamid
set userid=%1
for /f "tokens=*" %%c in ('dsquery user %userid% -o samid') do echo ,%%c >> %ou%-group-members.csv
goto :eof

Open in new window


How can this be done?  Thanks for your help!
0
samiam41
Asked:
samiam41
  • 5
  • 3
  • 3
3 Solutions
 
NVITCommented:
for /f "tokens=1-4 delims=,=" %%a in (%ou%-group-members.csv) do echo %%d
0
 
samiam41Author Commented:
Thanks NewVillageIT!  Could you dumb it down for me and show me what the script would look like with your code inserted, please?
0
 
NVITCommented:
@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=a,OU=b,OU=c,OU=d,DC=e,DC=f,DC=g,DC=h" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% > %ou%-group-members.csv
::pause
for /f "tokens=*" %%b in ('dsget group %ougroup% -members ^|sort') do (call :usersamid %%b)
goto :eof

:usersamid
set userid=%1
for /f "tokens=*" %%c in ('dsquery user %userid% -o samid') do echo ,%%c >> %ou%-group-members.csv
for /f "tokens=1-4 delims=," %%a in (%ou%-group-members.csv) do echo %%b >> %ou%-group-members2.csv
goto :eof

Open in new window

0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
NVITCommented:
I revised it, sending the results to %ou%-group-members2.csv
0
 
oBdACommented:
The output will actually contain
CN=groupname,OU=a,OU=b,OU=c,OU=d,DC=e,DC=f,DC=g,DC=h
So when you say I only want OU=groupname to be displayed in the log file, do you want the group name (which I assume in the script below), or really the OU name (which would be the same for all groups, so you'd lose the group information.
Then the variable "ou" you're using in the output file name isn't set, and you're always overwriting the output file in line 10, so you'll only ever get the last group in the output file.
Finally, the current output isn't really csv; do you want that rearranged, or can you work with what you have?
Here's a slightly enhanced version:
@echo off
cls
set ou=test
set CsvFile=%ou%-group-members.csv
if exist "%CsvFile%" del "%CsvFile%"
for /f "tokens=*" %%a in ('dsquery group "OU=a,OU=b,OU=c,OU=d,DC=e,DC=f,DC=g,DC=h" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
for /f "tokens=1 delims=," %%a in (%ougroup%) do (>>"%CsvFile%" echo "%%a")
::pause
for /f "tokens=*" %%b in ('dsget group %ougroup% -members ^|sort') do (call :usersamid %%b)
goto :eof

:usersamid
set userid=%1
for /f "tokens=*" %%c in ('dsquery user %userid% -o samid') do >>"%CsvFile%" echo ,%%c
goto :eof

Open in new window

0
 
oBdACommented:
OK, I couldn't help it; try that for size.
It has the OU query from your other question already integrated (and the "DC=" part will be built automatically), and produces a real csv with a header, and the name of the main OU in the file name.
@echo off
cls
set OUPath=
set /p "OUPath=Please enter the OU path as 'OU=x,OU=y', without quotes: "
if "%OUPath%"=="" goto :eof
set OUDN=%OUPath%,DC=%UserDnsDomain:.=,DC=%
for /f "tokens=2 delims=,=" %%a in ("%OUDN%") do (set LastOU=%%a)

set CsvFile=%LastOU%-group-members.csv

echo Running query against '%OUDN%'.
echo Output file will be '%CsvFile%'.

>"%CsvFile%" echo "Group","Member","OU"
for /f "tokens=*" %%a in ('dsquery.exe group "%OUDN%" ^| sort.exe') do (call :oumembers %%a)
exit /b

:oumembers
set GroupDN=%~1
for /f "tokens=2 delims=,=" %%a in ("%GroupDN%") do (set GroupName=%%a)
echo Processing group '%GroupName%' ...
for /f "tokens=*" %%b in ('dsget.exe group "%GroupDN%" -members ^| sort.exe') do (call :usersamid "%%~b")
goto :eof

:usersamid
set UserDN=%~1
for /f "tokens=*" %%c in ('dsquery.exe user "%UserDN%" -o samid') do (
	>>"%CsvFile%" echo "%GroupName%","%%~c","%OUDN%"
)
goto :eof

Open in new window

0
 
samiam41Author Commented:
Hey oBdA!

In the script you put in your last post, can I manually put the OU/DC entries so that all I have to enter when I start the script is the OU I want audit the groups of?

If this were my domain structure, [OU-Texas,OU-NorthAm,DC-Business,DC-com], I would like the script to already have those entries in the script.  Then when the script runs, I can enter one of the OU's that are nested under OU-Texas.  The OU that I manually enter would be audited.

Hopefuly I didn't make that too muddy.  Thanks again!
0
 
samiam41Author Commented:
@NewVillageIT, I'm testing your recommendation further.  Updates forthcoming.
0
 
oBdACommented:
How about this:
@echo off
cls
REM *** Default OU path in the format 'OU=...,OU=...'
set DefaultOU=OU-Texas,OU-NorthAm
REM *** Domain components part; use 'set DomainComponents=DC=%UserDnsDomain:.=,DC=%' to auto-generate based on the user's logon domain
set DomainComponents=DC=%UserDnsDomain:.=,DC=%
set OUName=
set /p "OUName=Please enter the OU name under '%DefaultOU%': "
if "%OUName%"=="" goto :eof
set OUDN=OU=%OUName%,%DefaultOU%,%DomainComponents%

set CsvFile=%OUName%-group-members.csv

echo Running query against '%OUDN%'.
echo Output file will be '%CsvFile%'.

>"%CsvFile%" echo "Group","Member","OU"
for /f "tokens=*" %%a in ('dsquery.exe group "%OUDN%" ^| sort.exe') do (call :oumembers %%a)
exit /b

:oumembers
set GroupDN=%~1
for /f "tokens=2 delims=,=" %%a in ("%GroupDN%") do (set GroupName=%%a)
echo Processing group '%GroupName%' ...
for /f "tokens=*" %%b in ('dsget.exe group "%GroupDN%" -members ^| sort.exe') do (call :usersamid "%%~b")
goto :eof

:usersamid
set UserDN=%~1
for /f "tokens=*" %%c in ('dsquery.exe user "%UserDN%" -o samid') do (
	>>"%CsvFile%" echo "%GroupName%","%%~c","%OUDN%"
)
goto :eof

Open in new window

0
 
samiam41Author Commented:
Thanks for the help and script!  Amazing work and appreciate all of your time.

-Aaron
0
 
samiam41Author Commented:
If any of you legends can spare a minute with my new question, I would appreciate it!

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28610287.html
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now