香港新浪網 MySinaBlog
« 上一篇 | 下一篇 »
Emily | 26th Feb 2007, 08:49 AM | Script | (1496 Reads)

WMI Example: Win32_Process (4): List Logon User

All Windows Platforms

'---------------------------------------------------------------------------------------
' Vbscript  , not VBA
' Save text file and execute  : List_LogonUser.vbs
' DateTime  :
' Author    :
' Purpose   : List Logon User
'---------------------------------------------------------------------------------------

strComputer = InputBox("Enter Computer Name")
Err.Clear

If Len(strComputer) = 0 Then
    Wscript.Quit
End If

On Error Resume Next

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!//" & strComputer & "/root/cimv2")

If Err.Number <> 0 Then
    Wscript.Echo "Computer not found or Unavailable"
Else
    Set colComputer = objWMIService.ExecQuery _
        ("Select * from Win32_ComputerSystem")
For Each objComputer In colComputer
    Wscript.Echo "Logged-on user: " & objComputer.UserName
Next
End If

' Vbscript End 


 

Sub ListLogonUser()

'WMI Example: Win32_Process: List Logon User
'All Windows Platforms
'---------------------------------------------------------------------------------------
' VBA Version
' DateTime  :
' Author    :
' Purpose   : List Logon User
'---------------------------------------------------------------------------------------

Dim strComputer As String
Dim objWMIService As Object
Dim colComputer As Object
Dim objComputer As Object
strComputer = InputBox("Enter Computer Name", "Get Computer Name", ".")
Err.Clear

If Len(strComputer) = 0 Then
    Exit Sub
End If

On Error Resume Next

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!//" & strComputer & "/root/cimv2")

If Err.Number <> 0 Then
    MsgBox "Computer not found or Unavailable"
Else
    Set colComputer = objWMIService.ExecQuery _
        ("Select * from Win32_ComputerSystem")
For Each objComputer In colComputer
    MsgBox "Logged-on user: " & objComputer.UserName
Next
End If
End Sub