Solved

clean up output in log file

Posted on 2015-01-23
11
138 Views
Last Modified: 2015-02-04
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
Comment
Question by:samiam41
  • 5
  • 3
  • 3
11 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 40567234
for /f "tokens=1-4 delims=,=" %%a in (%ou%-group-members.csv) do echo %%d
0
 
LVL 9

Author Comment

by:samiam41
ID: 40567237
Thanks NewVillageIT!  Could you dumb it down for me and show me what the script would look like with your code inserted, please?
0
 
LVL 23

Assisted Solution

by:NVIT
NVIT earned 250 total points
ID: 40567240
@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
 
LVL 23

Assisted Solution

by:NVIT
NVIT earned 250 total points
ID: 40567267
I revised it, sending the results to %ou%-group-members2.csv
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40567275
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 83

Expert Comment

by:oBdA
ID: 40567319
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
 
LVL 9

Author Comment

by:samiam41
ID: 40571021
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
 
LVL 9

Author Comment

by:samiam41
ID: 40571023
@NewVillageIT, I'm testing your recommendation further.  Updates forthcoming.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 250 total points
ID: 40571057
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
 
LVL 9

Author Closing Comment

by:samiam41
ID: 40588798
Thanks for the help and script!  Amazing work and appreciate all of your time.

-Aaron
0
 
LVL 9

Author Comment

by:samiam41
ID: 40589624
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now