Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read list of server then get their local drives and read last 12 lines

Status
Not open for further replies.

bolobaboo

MIS
Aug 4, 2008
120
US
Hi
VB guru .....I have a list of server to connect to them. Then get their local drives names and also read log file ( e:\program files\tivoli\tsm\baclient\dsmsched.log) ,last 12 lines. Once got these two information, write them to a output file in local computer.

Don't know what is wrong ?

my code
=======
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' **** Constants for the OU information ****
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' **** Declare global variables ****
Dim objInputFile, strText, arrComputers, strComputer ' Used for the input & output of the textfile
Dim objFSO, wshNetwork, wshShell ' Some global stuff
Dim objWMIService, SWBemlocator ' WMI connect to class stuff (Used for rive & memory info)
Dim objService, objRegistry ' WMI connect to register stuff (used for SQL & ISS)
Dim UserName, Password ' Credentials for login to WMI service
Dim objItem, colItems ' Needed for the loops

' **** User credentials ****
UserName = ""
Password = ""

' **** Declare standard sets ****
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")

' **** Select the inputfile ****
If objFSO.FileExists("servers.txt") then
Set objInputFile = objFSO.OpenTextFile("servers.txt", ForReading)
Else
MsgBox ("File servers.txt not found!")
WScript.Quit
End if


' **** Read inputfile ****
strText = objInputFile.ReadAll
objInputFile.Close
' Split the inputfile for computernames, which are seperated by a enterkey
arrComputers = Split(strText, vbCrLf)


' **** Remove outputfiles if exist ****
For Each strComputer In arrComputers
If objFSO.FileExists(strComputer & ".txt") Then
objFSO.DeleteFile(strComputer & ".txt")
End If
Next


' **** Create the textfile with server information ****
Function CreateTextFile(AppendText)
Dim f, ts
If Not (objFSO.FileExists(strComputer & ".txt")) Then
objFSO.CreateTextFile strComputer & ".txt"
Set f = objFSO.GetFile(strComputer & ".txt")
Set ts = f.OpenAsTextStream(ForAppending, TristateUseDefault)
ts.WriteLine "Document created on: " & date
ts.WriteLine ""
ts.WriteLine "Server information for " & strComputer
ts.WriteLine "---------------------------------------"
ts.WriteLine ""
ts.Close
End If

Set f = objFSO.GetFile(strComputer & ".txt")
Set ts = f.OpenAsTextStream(ForAppending, TristateUseDefault)
ts.WriteLine AppendText
ts.Close
End Function


' **** Start reading the actions that need to be executed for each computer in the inputfile *****
For Each strComputer In arrComputers

' -------------------------------------------------------------------------------
' **** Gather drive information ****
On Error Resume Next

Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",UserName,Password)
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk",,48)

For Each objItem in colItems
If objItem.Description <> "CD-ROM Disc" Then
CreateTextFile("Drive "& objItem.DeviceID & "\" & " Size: " & _
Round( objItem.Size / 1073741824 ) ) & " GB"
End If
Next
Set objWMIService = Nothing
On Error Goto 0

Set colItems = Nothing


' -------------------------------------------------------------------------------
' **** Gather last 12 lines from schedule.log ****
Set ReadText=FsoObject.OpenTextFile("e:\program files\tivoli\tsm\baclient\dsmsched.log", 1, "True")
Answer=ReadText.ReadAll

'after the Answer line
set ReadText=nothing
set FsoObject=nothing
aAnswer=split(Answer,vbcrlf)
sTrimAnswer=""
for i=ubound(aAnswer)-12+1 to ubound(aAnswer)-1
sTrimAnswer=sTrimAnswer & aAnswer(i) & vbcrlf
next
sTrimAnswer=sTrimAnswer & aAnswer(ubound(aAnswer))
Answer=sTrimAnswer '<<<the desired trimmed down answer

' ---------------------------------------------------------------------------------

' **** End reading the actions that need to be executed for each computer in the inputfile ****
Next

WScript.Echo ("Done!")
 
What doesn't work? Do you get any errors?

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
How do i read file from other computer ?
Looks like following code is not correct ...

Set ReadText=FsoObject.OpenTextFile("e:\program files\tivoli\tsm\baclient\dsmsched.log", 1, "True")

How do i tell script to open on other computer and read last 12 lines ?
 
Instead of "e:\program files\tivoli\tsm\baclient\dsmsched.log", with proper permissions you should be able to use:

"\\" & strComputer & "\e$\program files\tivoli\tsm\baclient\dsmsched.log
 
As guitarzan inferred, use UNC paths to access remote machines (\\server\share). Also, you shouldn't hardcode file locations, especially if they are subject to change.

Code:
' **** Select the inputfile ****
[red]strServersFile = "C:\path\to\servers.txt"[/red]
If objFSO.FileExists([red]strServersFile[/red]) then
   Set objInputFile = objFSO.OpenTextFile([red]strServersFile[/red], ForReading)
Else
   MsgBox ("File [red]" & strServersFile & "[/red] not found!")
   WScript.Quit
End if

same with remote paths

Code:
strRemoteLog = "\\" & strComputer & "\e$\program files\tivoli\tsm\baclient\dsmsched.log"
Set ReadText=FsoObject.OpenTextFile(strRemoteLog, 1, "True")

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
It's probably trying to open an empty or non-existing file. Thus, Answer = "" and aAnswer is empty and ubound(aAnswer) -12 results in an error. Apply the suggestions above and below.

Your code for reading the last 12 lines is correct given ideal circumstances. Account for the unexpected.


Code:
if (ubound(aAnswer) <> -1) then
    intStart = 0
    intEnd = ubound(aAnswer)
    if (ubound(aAnswer) > 11) then
        intStart = ubound(aAnswer) - 11
    end if
    for i = intStart to intEnd
        sTrimAnswer = sTrimAnswer & aAnswer(i) & vbCrLf
    next
else
    sTrimAnswer = "empty"
end if

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi
Geates
Looks like following needs user name and password ...
strRemoteLog = "\\" & strComputer & "\e$\program files\tivoli\tsm\baclient\dsmsched.log"
'
How do i provide these ?
 
You'll need to be granted permissions to that share (e$) on that computer. This is often done by your active directory administrator adding you to an admin group.

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Can i use following variables instead ?
UserName = "rb\xxxxxx"
Password = "xxxxxx"

Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",UserName,Password)
strRemoteLog = "\\" & strComputer & "\e$\program files\tivoli\tsm\baclient\dsmsched_tsm62v6.log"
Set ReadText=FsoObject.OpenTextFile(strRemoteLog, 1, "True")
 
I'm not sure. I've not encountered this set of circumstances.

You could map a network drive and provide credentials.

Code:
set objNetwork = CreateObject("WScript.Network")
strDrive = "x:"
strShare = "\\" & strComputer & "\e$\program files\tivoli\tsm\baclient"
strUser = "rb\UserName"
strPass ="password"
objNetwork.MapNetworkDrive strDrive, strShare, false, strUser, strPass

strRemoteLog = "X:\dsmsched_tsm62v6.log"
set objLogFile = objFSO.OpenTextFile(strRemoteLog, 1, true, 0)

-Geates



"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I have to connect hundreds of servers ... maping then removing mapping is good idea ? Can i use SWBemlocator ? i need just 12 lines from each servers log. By the way how to remove mapping ?
 
object.RemoveNetworkDrive Device [, Force] [, UpdateProfile]

Code:
objNetwork.RemoveNetworkDrive "X:", true

I'm not comfortably familiar with SWBemlocator to make a suggestion. It looks like you can but I'm not sure how you would access the remote file system with it.


-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi
geates
Now i am able to read log entry. Now following code is not working to get local drive information from each server ..

Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\cimv2",username,password)

Set colItems = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objItem in colItems
If objItem.DriveType = 3 Then
ts.writeline objItem.Name
End If
Next
 
Got it fixed. Used following ..
Set env = CreateObject("Microsoft.SMS.TSEnvironment")
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUser,strpass)

Thank you geates for your help.
 
I was about to say, GetObject() does not accept more than one argument. You need to use .ConnectServer to pass username and password credentials.

What's "env" for?

Glad you got it working

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Don't know about "env" , it was there in one of posting so i copy and pasted in my script. Quick question How do i skip rest of "FOR each ... next " loop if one of experssion encounter error ? I also came accross few servers which use different user name and password , how do i accomate that ?
 
Don't know about "env"
I'd get rid of it. It's not used

How do i skip rest of "FOR each ... next " loop if one of experssion encounter error ?
Use OERN. It will continue with the next command in the scope it was placed. If placed outside the for loop, the whole loop will bail on error.

Code:
on error resume next
for each objItem in objItems
   'an error occurred   
   msgbox "inside"
next
msgbox "outside"

[green]output: outside[/green]

for each objItem in objItems
   on error resume next
   'an error occurred   
   msgbox "inside"
next
msgbox "outside"

[green]output: inside[/green]

I also came accross few servers which use different user name and password , how do i accomate that

figure out which servers use which credentials and use a select..case

Code:
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")

select case lcase(strComputer)
   case "server1", "server3", "production"
      strUser = "bob" : strPass = "dog"
   case "server2", "xchange"
      strUser = "will" : strPass = "cat"
   case else
      strUser = "dan" : strPass = "fish"
end select

Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUser,strPass)

-Geates


"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Geates:

The scope of On Error Resume Next is the entire Sub/Function you are in (or the main body), not whether you are inside or outside the loop. Once it's been turned "on", it is on forever inside that scope until it's turned "off" with On Error Goto 0

I would recommend:
Code:
for each objItem in objItems
   ' ... some code
   On Error Resume Next
   Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUser,strpass)
   If Err.Number = 0 Then
      ' open the log, retrieve last 12 lines, etc.
   Else
      ' log which server failed
   End If
   On Error Goto 0
   ' ... some code

next
 
I thought otherwise, but you're right

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
got confused ...
here is code .. if can't get to server using username/passsword , how do i skip excuting "ts.writeline objItem.Name
" ?

Set env = CreateObject("Microsoft.SMS.TSEnvironment")
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUser,strpass)
Set colItems = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objItem in colItems
If objItem.DriveType = 3 Then
ts.writeline objItem.Name
End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top