Windows Server - Need Source file to be edited with failed ping

Asked By Rbhurs
28-Nov-07 04:25 PM
I am currenly using the following script to change the local admin pword on
all my pcs.  I want the servers.txt file to let me know which pcs were not
pingable.  My script is in ADSI.  Could someone please help figure out how to
do this?


On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\scripts\servers.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
Set objExecObject = objShell.Exec(strCommand)

Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then



set objComputer = GetObject("WinNT://" & strcomputer & "")
objComputer.Filter = Array("User")
Set objUser = GetObject("WinNT://" & strComputer &
Wscript.Echo objUser.Name
objUser.SetPassword("password")


Else
Wscript.Echo strComputer & " could not be reached."
End If
Loop
Loop

objTextFile.Close
ObjExecObject.StdOut.AtEndOfStream
(1)
ObjExecObject.StdOut.ReadAll
(1)
Scripting.FileSystemObject
(1)
ObjTextFile.AtEndOfStream
(1)
ObjFSO.OpenTextFile
(1)
WScript.Shell
(1)
ObjTextFile.Readline
(1)
ObjUser.SetPassword
(1)
  Tom Lavedas replied...
29-Nov-07 01:58 AM
'------------------ changes ------------------
strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer _
& " | find ""TTL="" "
nRes = objShell.Run(strCommand, 0, true)

If nRes = 0 Then
'------------------ changes ------------------
& "/administrator")
'------------------ change ------------------
' deleted     Loop
'------------------ change ------------------

See the embedded changes above.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
  Rbhurs replied...
28-Nov-07 10:37 PM
I am sorry, but i am new to this, and first, i don't understand what the
changes were that you made, and second, when i ran it, it didn't append my
file called servers.txt.  The appending i want to accomplish is when a
machine is pinged and it doesn't respond, i would like for the servers.txt
file to reflect that the machine that was being pinged didn't respond.  I
have about 1500 machines to reach, so i don't want to have to guess which
ones have been affected by the script and which ones haven't.
  Al Dunbar replied...
29-Nov-07 01:11 AM
His code was only intended to determine the pingability of each computer and
act accordingly. Perhaps he did not deduce that wanting "the servers.txt
file to tell you: something meant that something was to be appended to it.

Anyway, there are a couple of reasons why it would not be a good idea to try
to append information other than what the script expects to find in this
input file:

- you would need to code it so that the status information was not assumed
by the script to be the name of the next computer to work with;
- having a file open for both reading and writing, and doing both of those
things seems to me to easily result in confusion.

I would suggest that your script open a separate file for appending, and
write the name of each non-pingable file to it. The output of one run could
then be the input of the next until the number of unpingable computers found
was zero. If I were doing this, I might have the file manipulation done by a
batch file in order to keep the vbscipt simple.

So, Tom gave you code that you had trouble understanding, and I have tried
to give you understanding without giving you the code. Which styel do you
prefer?


/Al
  Rbhurs replied...
29-Nov-07 09:43 AM
Al,

Thanks so much, that is information that I needed.  I was kind of coming to
the conclusion that my servers.txt file was only a static file, not
appendable.

I like both approaches put together.  I like the answer, but i also want to
know why the answer is the way it is, not just the answer by itself.  Do you
know of any really good books for a beginner to get some knowledge of this
stuff?  I am really just getting started in scripting, and am liking it.
  Al Dunbar replied...
30-Nov-07 01:01 PM
If you are talking about the issue of trying to append to the file you have
open for reading, here is how I understand it:

When a file is open for reading, it is generally locked, and unable to be
modified or appended to. This is because the reading and writing is, more or
less, sequential and synchronous[*]. It is possible for a file to have
multiple concurrent instances of being accessed, but that would need to be
through some sort of indexed I/O, such as is done in some database systems.
The overhead for doing so is not built into vbscript, and would be quite a
problem to script a solution. You might be able to use one of the COM
objects available for accessing database files with queries, but even ADO
does sequential reads (I think).

An alternative approach would be to maintain the computer list in a
spreadsheet, and access the required info by row/cell reference. While
technically feasible, I think that the complexity of the approach would
increase the scripting effort well beyond the benefits that might be
realized from this approach.

[*] another way to look at this is that when you read from a file
line-by-line, each character read does not come directly from the file
itself, but through some kind of buffering system. And when a file is
modified (i.e. changes are committed back to the storage device), this is
often done internally by writing a completely new copy of the file with
changes applied. So line one of the modified file might not be at the same
location it was when the unmodified file was first opened for reading.

First rule of scripting: keep it relatively simple; it's only scripting.


There are a lot of good references for the scripting language itself
("VBScript Programmer's Reference" from WROX), as well as some for specific
application areas (for example, "Windows NT/2000 ADSI Scripting for System
Administration" by Thomas Eck), and even a few general purpose books dealing
with the discipline of programming/app development ("Code Complete" by Steve
McConnell). Most of these will show you something about how certain tasks
could be scripted or programmed, but very few will concentrate on the sorts
of misconceptions the beginner might have about how things work.

Scripting is an arcane activity to say the least. Having an interest or
affinity for it as you do is a definite advantage. Much of the available
information is fact-oriented and targetted narrowly, leaving it to the
practitioner to develop his/her own understanding of how it all fits
together, how or why it works, and what is feasible versus what is not. My
best advice at this point can be summarized as:

- follow this and other related newsgroups
(microsoft.public.scripting.vbscript, microsoft.public.scripting.wsh);
- check out the available web sites, including the MS scripting centre;
- ask questions;
- challenge your own understanding about what you think you know.

The last point is the trickiest, as our own assumptions can be so ingrown
and self-evident that we often do not realize what they are let alone that
we have made them. When stuck on some point, learn to ask yourself why you
think a particular way.

I once had a supervisor who wanted me to develop a simple database to keep
track of something (I forget what it was). When I tried to explain to him
that there was no possible solution to the problem this was intended to
solve he said: "I can't imagine how it would be impossible to do this, as it
seems simple to me". He dropped the idea when I suggested that, in order to
actually do it, at least one of us would have to be able to imagine how it
*could* be done!


/Al
Create New Account
help
name on command line has maximum length of 28 characters / / = = = = = = = = = = = = = = = = = = = = = = = = = = = [ Variables ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = var fso = new ActiveXObject("Scripting.FileSystemObject"); var FileHandle, ExitStatus = 0, BackupStatus = 0; var ForReading = 1, ForWriting = 2, ForAppending = 8; var EmailBody var ExitCode, LogF = 0x2; if (FsLogFlag(LogF)) IoLogEvent("open", LogHandle, "FsCheckFileExists"); var fso = new ActiveXObject("Scripting.FileSystemObject"); ExitCode = fso.FileExists(FileName) if (FsLogFlag(LogF)) { if (ExitCode) IoLogEvent("content", LogHandle, FileName +" does exist LogHandle) { var ExitCode, LogF = 0x200; if (FsLogFlag(LogF)) IoLogEvent("open", LogHandle, ""); var fso = new ActiveXObject("Scripting.FileSystemObject"); ExitCode = fso.FolderExists(FolderName) if (FsLogFlag(LogF)) { if (ExitCode) IoLogEvent("content", LogHandle, "FsCheckFolderExists success: else FileName, LogHandle) && Overwrite) | | (!FsCheckFileExists(FileName, LogHandle))) { if (FsLogFlag(LogF)) IoLogEvent("open", LogHandle, ""); fso = new ActiveXObject("Scripting.FileSystemObject"); f = fso.CreateTextFile(FileName, Overwrite, Unicode); f.Close(); if (FsLogFlag(LogF)) { IoLogEvent("content", LogHandle, "FsFileCreate
anybody knows how to do this? Thanks in advance, Marcus Windows 2000 Active Directory Discussions Scripting.FileSystemObject (1) ObjFSO.OpenTextFile (1) StrNetBIOSDomain (1) ObjFile.AtEndOfStream (1) NetBIOS (1) ObjTextFile.WriteLine (1) What is the format strFile = "c: \ scripts \ organization.csv" ' Open the text file for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Read each line of the file. Do Until objFile.AtEndOfStream strLine = Trim(objFile
could try this script: sServerList = "d: \ temp \ Servers.txt" sOutputFolder = "d: \ temp \ " Set oFSO = CreateObject("Scripting.FileSystemObject") Set oServerList = oFSO.OpenTextFile(sServerList) While Not oServerList.AtEndOfStream ProcessServer(oServerList.ReadLine) Wend oServerList.Close C: \ DST" Set objNet = CreateObject("WScript.NetWork") Set objComputer = CreateObject("Shell.LocalMachine") Set oFSO = CreateObject("Scripting.FileSystemObject") Set oServerList = oFSO.OpenTextFile(sServerList) While Not oServerList.AtEndOfStream ProcessServer(oServerList.ReadLine) Wend oServerList.Close root \ cimv2") Set objNet = CreateObject("WScript.NetWork") Set objComputer = CreateObject("Shell.LocalMachine") Set objFSO = CreateObject("Scripting.FileSystemObject") Set oFilesys = CreateObject("Scripting.FileSystemObject") Set oFiletxt = oFilesys.CreateTextFile("C: \ DST \ server_dst.log", True) sPath = oFilesys.GetAbsolutePathName("C: \ DST \ server_dst
an error: Line:10, Char:5, Error: Type mismatch Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c: \ test \ test2 \ scanlist.txt", ForReading) Do Until objTextFile.AtEndOfStream strNextline = objTextFile.readline strComputer = Split(strNextline , ", ") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel = impersonate}! \ " & strComputer & " \ root Range. " _ & "This might take several minutes to complete." Const ForAppending = 8 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile1 = objFSO.OpenTextFile _ ("c: \ test \ test2 \ helpdesk_output2.txt", ForAppending, True) intStartingAddress = 1 intEndingAddress