Windows Server - Exception handling

Asked By Georg
02-Apr-07 03:24 AM
Hi ,

I am running a script to find the last restart time for a group of servers.
This script reads the server names from a text file and connect to that
server and returns the last restart time. This info will be written to a
file. Attached at the end of this.

But some of the servers will be not reachable/accessable. I want the
indication that those servers are not reachable.

For example Output should like this:

IP address   Last restart time
10.10.10.1   20070331115922.500000-240
10.10.10.2   Not reachable

I like to take the output to an excel file . Restart time should be in the
second cell.

Current running script:
---------------------------
On error resume Next
Const ForReading = 1
Const ForAppending = 8

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

Set objTextFile1 = objFSO.OpenTextFile _
("C:\last_reboot.csv", ForAppending, True)

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")


strComputer = arrServiceList(0)

Set objWMIService = GetObject("winmgmts:" & _
strComputer & "\root\cimv2")


Set colOS = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")

For Each objOS in colOS
objTextFile1.Writeline arrServiceList(0) & vbtab & objOS.LastBootUpTime & vtab
Next

Loop


----------------------

Can you please help me out.

Regards,
George
ObjTextFile
(1)
ArrServiceList
(1)
ObjWMIService
(1)
StrComputer
(1)
ObjFSO
(1)
OpenTextFile
(1)
StrNextLine
(1)
ImpersonationLevel
(1)
  Marcus Schmitt replied...
02-Apr-07 04:49 AM
George schrieb:

Hi George,

I added a ping:

'--------------------------------------------
On error resume Next
Const ForReading = 1
Const ForAppending = 8

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

Set objTextFile1 = objFSO.OpenTextFile _
("C:\last_reboot.csv", ForAppending, True)

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")


strComputer = arrServiceList(0)


Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colPings = objWMIService.ExecQuery ("Select * From Win32_PingStatus
where Address = '" & strComputer & "' AND Timeout=100")
For Each objStatus in colPings
If IsNull(objStatus.StatusCode) or objStatus.StatusCode <> 0 Then
IsOnline = False
Else
IsOnline = True
End If
Next


If IsOnline Then
Set objWMIService = GetObject("winmgmts:" & _
strComputer & "\root\cimv2")


Set colOS = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")

For Each objOS in colOS
objTextFile1.Writeline arrServiceList(0) & vbtab &
objOS.LastBootUpTime & vtab
Next
else
objTextFile1.Writeline arrServiceList(0) & vbtab & "Not reachable"
& vtab
end if

Loop
'--------------------------------------------
  Georg replied...
02-Apr-07 08:08 AM
Hi Marcus,

Thanks. I had run this script. But not getting the desired output. Ping
status shows as 'Not reachable ' always.
Can please suggest on this.

Regards,
George.
  Marcus Schmitt replied...
03-Apr-07 01:34 AM
Hi George,

don't know why - I've testet it in our network and it works.
What happens if you ping of the machine in the dos prompt ?
Try to use only the ping lines as new script for testing.

Marcus





George schrieb:
  Georg replied...
03-Apr-07 10:00 AM
Thanks Marcus. The script is giving the desired output in our network
without any change in code.
Thanks for your help.

George.
help
not skilled with vbs scripting. Malcolm = = = = Full script below = = = = = = = Option Explicit Dim objNetwork, objLocalGroup, strFile, objFSO, objFile ' These attributes must be declared in the main program, ' so they are global in scope. Dim objTrans, strComputer, strNetBIOSDomain ' Constants for the NameTranslate object. Const ADS_NAME_INITTYPE_GC = 3 Const ADS_NAME_TYPE_NT4 = 3 Const ADS_NAME_TYPE_1779 = 1 NetBIOS names of computers. strFile = "Servers.lst" ' Open the text file for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Determine NetBIOS name of domain. Set objNetwork = CreateObject("Wscript.Network") strNetBIOSDomain = objNetwork UserDomain ' Read computer names from the file. Do Until objFile.AtEndOfStream strComputer = Trim(objFile.ReadLine) ' Skip blank lines. If (strComputer <> "") Then ' Output computer name. ' Wscript.Echo "Computer: " & strComputer ' Bind to local Administrators group on remote computer. ' Trap error if computer not available (or group does not exist). On Error Resume Next Set objLocalGroup = GetObject("WinNT: / / " & strComputer _ & " / Administrators, group") If (Err.Number = 0) Then On Error GoTo 0 ' No Error. Enumerate
False·NL = Chr(10)·A ccessDenied = False· ·main()··' = = = = = = = = = = = = = = ·'Main module·' = = = = = = = = = = = = = = ·Sub m ain·· Dim Help, objFSO, objArgs· Set obj FSO = CreateObject("Scripting.FileSystem Object")· Set ObjWshShell = WScript.Crea teObject("WScript.Shell ObjWshShell.Environment ("PROCESS")("temp")· Tempname1 = temp & .tmp"· · Err.Clear· On Error Resume Next · Set objStream = objFSO.OpenTextFile(Te mpname1, ForWriting, True)· If Err.numbe r <> 0 Then· WScript.Echo ("Cannot open Do While Not WScript.S tdIn.AtEndOfLine· Line = WScript.StdIn .Read(1)· Loop· End If· · objFSO.Del eteFile(TempName1)· objFSO.DeleteFile(Te mpName2)· Set objStream = Nothing· Set o bjFSO = Nothing·End Sub·'- -- -- -- -- -- - - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --·'Com pile a list of all files in tempfile2.tx t·'- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -- -- -- -- -·Sub GetMainDirectory(Folder)· · Dim objFSO, files, iSize, path· Set o bjFSO = CreateObject("Scripting.FileSyst emObject")· ·' Process main directory· files = 0 Close··End Sub ·'- -- - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - - -- -·'Recursively process each subdirect ory·'- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -- -- -- -- -- -·Sub GetSubdirectory(path, f iles, iSize)·· Dim objFSO, p, f, s, deni ed· · Set objFSO = CreateObject("Scripti ng.FileSystemObject")· Set objFldr = obj FSO.GetFolder(path)· f = 0· s = 0· denie ut file·'- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -- -- -- -- -- -- -- -·Sub GenerateOutput·· Co nst name = 0, size = 1, files = 2· · Dim ObjEx ec, objFSO, ObjWshShell· Dim file, Input File, index· Dim line, i, j, k, p, q, r· Dim
Server How can I changed this output to excel column format? On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel = impersonate}! \ " & strComputer & " \ root \ cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor") For Each objItem in colItems Wscript.Echo "Address Width: " & objItem.AddressWidth Caps: " & objItem.VoltageCaps Next Windows Server Scripting Discussions Microsoft Excel (1) PNPDeviceID (1) Excel (1) ObjWMIService (1) L2CacheSize (1) DeviceID (1) IntWMI (1) IntPOS (1) You could use something like this Excel.Application") objExcel.DisplayAlerts = False Set objWorkbook = objExcel.Workbooks.Add() Set objSheet = objWorkbook.Sheets (1) strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel = impersonate}! \ " _ & strComputer & " \ root \ cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_Processor") intColumn = 1 For Each objItem
On Error Resume Next Const ForReading = 1 Const WbemAuthenticationLevelPktPrivacy = 6 strUser = "Administrator" strPassword = "domainadminpassword" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile(". \ computers.txt", ForReading) Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator") ' strComputerUser = "Computer" & vbTab & "Logged in User" Do Until objTextFile.AtEndOfStream strComputer = objTextFile.Readline ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ' Insert your code here ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Set objWMIService = objwbemLocator.ConnectServer (strComputer, strNamespace, strUser, strPassword) objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ' Stop the spooler service. ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Set colServices = objWMIService.ExecQuery
is the base script that I got from Microsoft Script Center and it works fine: strComputer = "atl-ws-o1" Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel = impersonate}! \ " & strComputer & " \ root \ cimv2") Set colComputer = objWMIService.ExecQuery _ ("Select * from Win32_ComputerSystem") For Each objComputer in colComputer Wscript.Echo "Logged-on user It gives 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