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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retreiving Computer Name

Status
Not open for further replies.

BlundaKat

Technical User
Sep 20, 2009
2
GB
Hello

I am new to scripting and have been teaching myself how to script. My goal is to create a script which searches our log files for a key word. I have pretty much achieved this but would like the results to be printed out to a text document. The problem is there are many log files on many servers and all the log files are named the same. So when writing the results out to a text document I need a meaningful name to save each of them as. Ideally I would like it to be "(computer name)(Log file name).txt" but I cant work out how to extract the computer name using my existing code. Please see script below. As I said, I am new to scripting so I apologies if it is badly written, but it does work as I need it to.

Code:
Option Explicit
'On Error Resume Next

Dim objFSO
Dim arrServers	'Array Variables
Dim objTextFile
Dim strNextLine
Dim i
Dim TxtFile
Const ForReading = 1
Dim fNewest	'Find Newest file variables
Dim oFolder
Dim aFile
Dim strSearch	'Search Veriables
Dim ObjFile
Dim strLine
Dim SearchResult
Dim Results

'Reference Section

TxtFile = "textfile.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSearch = "error"

Set objTextFile = objFSO.OpenTextFile(TxtFile, ForReading)				'Array
Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.Readline
	arrServers = Split(strNextLine , ",")
	For i = 0 To Ubound(arrServers)							'Find Newest File
		set oFolder = objFSO.getfolder(arrServers(i))
		For Each aFile In oFolder.Files
		    If fNewest = "" Then
		        Set fNewest = aFile
		    Else
		        If fNewest.DateLastModified < aFile.DateLastModified Then
		            Set fNewest = aFile
		        End If
		    End If
		Next
		
		WScript.Echo(arrServers(i) & "\" & fNewest.name)
		
	
	Set objFile = objFSO.OpenTextFile(arrServers(i) & "\" & fNewest.name)
	strLine = objFile.ReadLine
	
		Do Until objFile.AtEndofStream
			strLine = objFile.ReadLine
			SearchResult = InStr(strLine, strSearch)
				If SearchResult <> 0 Then
					WScript.Echo(arrServers(i).name)

				End if
		Loop
	Wscript.Echo("")
	WScript.Echo("******SEARCH COMPLETE******")
	objFile.close 
	Next
	
	
Loop


 
arrServers is an array - not an object. It doesn't have a .name property.

Code:
WScript.Echo(arrServers(i)[s].name[/s])

-Geates
 
Ooops, I was meant to remove that bit. was just a test. The data in the array is the ip address and path of the log file. So any ideas how I can get the computer name of each of the entrys in the array? Thanks
 
Use the IP address to access the machines' registry. Then use .GetStringValue to extract the ComputerName from the local machine hive.

Code:
CONST HKEY_LOCAL_MACHINE =  &H80000002
set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & arrServers(i) & "\root\default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE, "system\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", strComputerName

msgbox arrServers(i) & " has the name " & strComputerName

-Geates
 
If we're going WMI, then I'd have though that interrogating the Win32_ComputerSystem class might be better than accessing the registry, since it contains a lot more info about the remote PC that might come in handy at a later date.
Code:
[blue]Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & arrServers(i) & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

For Each objComputer In colComputer
    strComputerName = objComputer.Name ' NETBIOS name
Next
msgbox arrServers(i) & " has the name " & strComputerName[/blue]
 
I considered mentioning that, however, if a computer does not have a healthy WMI component, an interrogation may come up empty handed. I recently experienced this on machines running SMS.

-Geates
 
>if a computer does not have a healthy WMI component

I think I'd be looking to fix WMI in that case
 
Our logon script runs locally and attempts to connect to the WMI component. If successful, the script uses WMI objects. If the WMI connection cannot be established, the script looks at the registry to get most of the requested properties. 'ComputerName' is one of the properties.

Of course, rereading what I posted, I realize that my method actually uses the WMI object to read the registry. If the WMI component is faulty, I suppose DOS commands (REG, ping -a, etc.) would be the alternative for aquiring remote system properties via vbs.

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top