Archive for the ‘batch file’ Tag

Courion Tip dsquery / dsget formatting output… vbscript…

Courion Tip – Collecting group membership information from Active Directory….
I found this batch file (below) on the internet (and will paste a link whenever I can find where I got it from!!!) HOWEVER, the format of the output was less than idea. What I REALLY wanted was something like this: {Please note that if you copy and paste any of this, you will probably get the wrong quotation marks and the code won’t work. So, paste it into notepad, then manually edit it to put the correct quotation marks back in!!}

GroupName1 Member1
GroupName1 Member2
GroupName2 Member1
GroupName2 Member2
GroupName2 Member3
etc… etc…. etc…

Here’s the code to generate the group membership information

@echo off
if exist membership.txt del membership.txt
dsquery group -limit 0 >groups.txt

::The FOR command is one single line
for /f “tokens=*” %%g in (groups.txt) do @echo Group Name: %%g >>membership.txt && echo Members: >>membership.txt && dsget group %%g -members -l >>membership.txt && echo **************************************** >>membership.txt

::display result
notepad membership.txt

Here’s the visual basic script I wrote to reformat it….

Dim fso
Dim InputFile
Dim OutputFile
Dim keyword
Dim retstring
Dim GroupName
Dim keywordlength

InputFile =”c:\membership.txt”
OutputFile =”c:\outputfile.txt”
keyword =”Group Name: ”
keywordlength = len(keyword)

Const ForReading = 1
Const ForWriting = 2
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set infile = fso.OpenTextFile(InputFile, ForReading)
Set outfile = fso.CreateTextFile(OutputFile, ForWriting)

Do until infile.AtEndOfStream = True
retstring = infile.ReadLine
If (len(retstring)>2) then ‘skip blank lines
If left(retstring, keywordlength) = keyword Then ‘look for whatever text was defined at the top of this file as the keyword
numofchars = (len(retstring)-keywordlength)
Groupname = right(retstring, numofchars)
retstring =”” ‘since this is the GroupName and not a member line set retstring to blank
Elseif left(retstring, 1) = “M” Then ‘skip lines that start with the word Member…
retstring =””
Elseif left(retstring, 1) = “*” Then ‘skip lines that are just astericks…
retstring =””
end if

retstring = Groupname&retstring&VbCrLf
If len(retstring)>len(Groupname)+3 Then outfile.Write(retstring) ‘if the output line would just contain the group name then don’t bother
end if


Now I have the output that I want


Also check out this


Posted March 24, 2010 by mmdmurphy in Courion Tip, freeware, tips, windows

Tagged with , , ,

Command Line SMTP Mailer for Batch Jobs

Sometimes you find something that hasn’t been updated, and realize it doesn’t need updated.

You need to relay it off of something, I am using an internal one at work so not so sure you can use this unless you have a mail server….

Posted June 2, 2008 by mmdmurphy in freeware, windows

Tagged with , , , ,