Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

automating a command-line process

5 views
Skip to first unread message

Tom Roberts

unread,
May 26, 1999, 3:00:00 AM5/26/99
to

Hi,

I need to write a program that sits on my NT 4 Server and checks a
text file every few minutes. When the text file exists, it opens it, and
grabs the user name/password in the file. (ie. Jimmy, pass2738)

Next, how do I, from within the program, run a command-line utility that
will add that username/password to the NT Domain Users (ie. User Manager for
Domains)?

What are the best/easiest tools for this job? (VB, AutoMgr, WSH, VBScript,
COM, etc.)

I need to be pointed in the right direction here.

Thanks, Tom Roberts
(t...@tc2000.com)


Alfredo Morresi

unread,
May 27, 1999, 3:00:00 AM5/27/99
to

Hi Tom

Your problem is not so hard...

1)Reading a text file:
For this you can use only the WSH skills, it's simply a string
manipolation joke... :)

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objWshShell
Dim objFileSystem
Dim fsUser
Dim strLine
Dim strUser, strPwd

Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
'open the file with the user information
set fsUser = objFileSystem.OpenTextFile("filename.txt", ForReading")
do while not fsUser.AtEndOfStream
strLine = fsUser.ReadLine
strUser = Left(strLine, InStr(strLine, ",") - 1)
strPwd = Mid(strLine, InStr(strLine, ",") + 1, Len(strLine))
createUser(strUser strPwd)
loop


2)Create the user account
For this you need an external tool, maybe ADSI (free, Microsoft),
installed on the machine in which the script run.
In this example i use ADSI.

sub createUser(strUser, strPwd)
Dim objDomain
Dim objUser

Set objDomain = GetObject("WinNT://DomainName")

On Error Resume Next
Set objUser = objDomain.Create("user", strUser)
objUser.SetInfo
objUser.SetPassword(strPwd)

Set objDomain = Nothing
Set objUser = Nothing
end sub


3)Repeat the task every few minutes
Two ways for this:
- Leave the program always in execution and within it wait some seconds
before repeat all the procedure. For this you need or WSH 2.0 or a
component that allow you to perform a sleep or wait function. See on
Clarence or Gunter Born site for them.
- Run the program once for each control of the text file. I think that
this is the better solution. How to automate this? Simply, use the
Schedule service. Just add this piece of code at the end of your program

objWshShell.Run "AT " & TimeSerial(Hour(Now), Minute(Now) + 5, 0) & " "
& "WScript.exe " & WScript.ScriptFullName, 0, True

This code will reexecute the program after a delay of 5 minutes.

Links:
Clarence Site:
http://cwashington.netreach.net/Home.asp?Page=/site/news.html
(ADSI program and samples, external component for a sleep function)

Microsoft ADSI site:
http://www.microsoft.com/ntserver/nts/default.asp
(and search for ADSI)

Microsoft WSH (scripting in general) site:
http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/default.htm


I hope that this will help you, but pay attention: i did not tested the
code, maybe that samewhere there are errors (but i hope no) ;)


-Enjoy-
Legolas

Mark Stang

unread,
May 27, 1999, 3:00:00 AM5/27/99
to
WSH is overkill for this IMO.

Batch can do this handily:
Note: all lines start indented 3 spaces, any that start before that are
wrapped from the previous line

::Addusers.bat
:top
if exist user.txt call :adduser
if exist user.txt del user.txt > nul
:: the sleep command is part of the NT reskit.
:: 300 seconds = 5 minutes
sleep 300
goto top

:adduser
for /f "tokens=1,2 delims=, " %%i in (user.txt) net user %%i %%j /domain
/add >nul
:: note the space between the comma and
:: the quote in the delims=section
goto :eof


Tom Roberts wrote in message ...

Mark Stang

unread,
May 27, 1999, 3:00:00 AM5/27/99
to

A correction. Add the word 'do' (no quotes) after the parentheses in the
for command:

for /f "tokens=1,2 delims=, " %%i in (user.txt) do net user %%i %%j
/domain /add >nul

above is one line

Mark Stang wrote in message ...

Michael Harris

unread,
May 27, 1999, 3:00:00 AM5/27/99
to
No offense (I admire anyone with the perseverance to master DOS batch
language - I _never_ did)...

Using WSH in some sense may be overkill, but people prefer to use it and
vbscript or jscript simply because it's almost immediately understandable to
anyone who reads the code as to what it's doing and how.

Just think what it would be like to write a complicated ASP application with
the DOS batch language <grin>...

--
Michael Harris

Mark Stang <mst...@limited.com> wrote in message
news:OZOVOxIq#GA....@cppssbbsa02.microsoft.com...

Mark Stang

unread,
May 27, 1999, 3:00:00 AM5/27/99
to
No offense taken.
The original poster was asking what tool to use. Since batch is included,
requires no install, and is pretty full featured under NT, and could do the
job he wanted easily, why go anywhere else?

I do disagree with the statement:

"...but people prefer to use it and vbscript or jscript simply because it's


almost immediately understandable to anyone who reads the code as to what
it's doing and how."

Only to people with VB (or javascript) programming experience, IMO. To the
person used to BASIC or DOS programming the whole Object.Property.Method
paradigm is hard to get your head around, IME. And I don't think it makes
it easier. Look at all the people on this NG that have syntax troubles,
because this method does it this way, that object doesn't support this
property, etc. So I disagree that it's easy to understand and follow.

Plus I still think batch is suited to this problem. I answered his question
with 8 lines of code, 2 of which are labels. With WSH I wouldn't be out of
the DIM statements yet. :-)

Mark

Michael Harris wrote in message <#53oxmJq#GA.239@cppssbbsa03>...

Michael Harris

unread,
May 27, 1999, 3:00:00 AM5/27/99
to
Hey... I'm a 15 year mainframe COBOL programmer recently converted to things
VB and Windows ;-)

I admit, it took a while to get this old dog's head around the object
paradigm, but once the light bulb went off, there was no turning back...

--
Michael Harris

Mark Stang <mst...@limited.com> wrote in message

news:O3JFyvJq#GA.52@cppssbbsa03...

Tom Roberts

unread,
May 27, 1999, 3:00:00 AM5/27/99
to

I like the batch approach even though I don't know it very well.

Questions:
I assume I will have to have an MSDOS command window open on my NT
machine while this batch is running?

Where can I get info on how this batch file works? (ie. syntax, format,
functions, etc.)


Thanks, Tom Roberts

Michael Hines

unread,
May 27, 1999, 3:00:00 AM5/27/99
to
you can get info for each DOS command from the command prompt...Example:

c:\> for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be
used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead of
%variable.

Of course, you could go spend money on a book...but it's more fun to
figure it out. :->

Mark Stang

unread,
May 28, 1999, 3:00:00 AM5/28/99
to
Yes there will be a CMD.EXE window open while this is running.

for help with batch commands the online help is suprisingly useful.

go to Start -> Help -> Windows NT Commands. There is an alphabetical list
of all the commands, plus a section entitled batch commands. Lots of usage
examples. Also, if you have questions in the future, the alt.msdos.batch
newsgroup has a lot of good batch people to help you out.


Mark


Tom Roberts wrote in message ...
>

Mark Stang

unread,
May 28, 1999, 3:00:00 AM5/28/99
to
I guess I still need to "get" it. I am usually frustrated when I try to do
things with WSH, plus to do any cool stuff I have to ensure ADSI is
installed. I just can't always be sure of that. So I fall back to batch
where I'm comfortable. I am not a professional programmer (obviously), just
mainly as a hobby, and then usually simple utilities. I grew up learning
DOS-BASIC and procedural programming. The whole object thing is just hard
to grasp for me, in the sense that it comes naturally, like procedural does
for me.

As a consultant, my utility solutions need to be as generic as possible, and
assume as little as possible about the target machine. Until WSH is a part
of the base installed OS, I will probably continue to use batch. I cannot
always force that WSH and ADSI be installed on my client's machines. I am
participating in this NG because I know that WSH is the future, and I want
to understand it. But sometimes it's not the right tool for the job.

Mark, who's WSHing the light bulb would go off in HIS head...


Michael Harris wrote in message ...

>>>Tom Roberts wrote in message ...
>>>>

0 new messages