Question : modify vb script to also get last logged on username or display name

I would like to add to a script created by sungenwang

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23324842.html

I would like to get a column with the last logged on (username or display name) to this computer

so there are 3 columns in the finished script (Computername  -  Service Tag  -  Last User Logon)

Is this possible with vbs?

Thank You
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
Const strSourceFile = "C:\computers.txt"
Const strDestFile = "C:\output.txt"
Const ForReading = 1
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFile = objFSO.OpenTextFile(strSourceFile, ForReading)
Set objDestFile = objFSO.CreateTextFile(strDestFile, True)
objDestFile.WriteLine("Computername       Service Tag")
 
Do While Not objSourceFile.AtEndOfStream
        strComputer = objSourceFile.ReadLine
        If Ping(strComputer) Then
            objDestFile.WriteLine(strComputer & "     " & GetSerial(strComputer))
        End If
Loop
objDestFile.Close
 
msgbox "done!"
 
 
Function GetSerial(strComputer)
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set colSMBIOS = objWMIService.ExecQuery _
        ("Select * from Win32_SystemEnclosure")
        
        GetSerial = ""
        For Each objSMBIOS in colSMBIOS
                GetSerial = objSMBIOS.SerialNumber
                Exit For
        Next
End Function
 
 
Function Ping(strComputer)
	Dim objPing, objStatus
	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._  
				ExecQuery("SELECT Replysize FROM Win32_PingStatus WHERE address = '" & strComputer & "'")  
 
	Ping = False
	For Each objStatus in objPing  
		If Not IsNull(objStatus.ReplySize) Then Ping = True
	Next  
End Function

Answer : modify vb script to also get last logged on username or display name

Please try this.

Regards,

Rob.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
Const strSourceFile = "computers.txt"
Const strDestFile = "output.csv"
Const ForReading = 1
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFile = objFSO.OpenTextFile(strSourceFile, ForReading)
Set objDestFile = objFSO.CreateTextFile(strDestFile, True)
objDestFile.WriteLine """Computername"",""Service Tag"",""User Name"""
 
Do While Not objSourceFile.AtEndOfStream
	strComputer = objSourceFile.ReadLine
	If Ping(strComputer) Then
		objDestFile.WriteLine """" & strComputer & """,""" & GetSerial(strComputer) & """,""" & GetUsername(strComputer) & """"
	End If
Loop
objDestFile.Close
 
msgbox "done!"
 
Function GetSerial(strComputer)
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        'Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
        Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS")
        
        'GetSerial = ""
        For Each objSMBIOS In colSMBIOS
			'GetSerial = objSMBIOS.SerialNumber
			GetSerial = objSMBIOS.SerialNumber
			Exit For
        Next
End Function
 
Function GetUsername(strComputer)
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set colComputer = objWMIService.ExecQuery _
        ("Select Username from Win32_ComputerSystem")
        
        'GetSerial = ""
		For Each objComputer In colComputer
			GetUsername = objComputer.Username
			Exit For
		Next
End Function
 
Function Ping(strComputer)
	Dim objPing, objStatus
	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._  
				ExecQuery("SELECT Replysize FROM Win32_PingStatus WHERE address = '" & strComputer & "'")  
 
	Ping = False
	For Each objStatus in objPing  
		If Not IsNull(objStatus.ReplySize) Then Ping = True
	Next  
End Function
Random Solutions  
 
programming4us programming4us