Hi Guys,
This one has me scratching my head. I got some code from the web and trying to put it together to work. When I run the following code it runs properly and I get the desired results. The code simply gets the current logged in user.
When I put the above code into a sub function I get the permission denied error. In the code below AD computers are queried and the above code just attempts to get current logged in user. But I get the run time error: permission denied:"GetObject" at the line where 'Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")' is called. Any insights?
This one has me scratching my head. I got some code from the web and trying to put it together to work. When I run the following code it runs properly and I get the desired results. The code simply gets the current logged in user.
Code:
' PARAMETERS
'
strComputer = "xpz" ' use "." for local computer
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'
Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 2")
If colSessions.Count = 0 Then
' No interactive session found
'
Wscript.Echo "No interactive user found"
Else
'Interactive session found
'
For Each objSession in colSessions
Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
' Show user info
'
For Each objItem in colList
WScript.Echo "User: " & objItem.Name
WScript.Echo "FullName: " & objItem.FullName
WScript.Echo "Domain: " & objItem.Domain
Next
' Show session start time
'
Wscript.Echo "Start Time: " & objSession.StartTime
Next
End If
' ------------------------------------------------------------------
When I put the above code into a sub function I get the permission denied error. In the code below AD computers are queried and the above code just attempts to get current logged in user. But I get the run time error: permission denied:"GetObject" at the line where 'Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")' is called. Any insights?
Code:
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, strTele
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user objects.
strFilter = "(objectCategory=computer)"
' Comma delimited list of attribute values to retrieve.
strAttributes = "name"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
Wscript.Echo adoRecordset.Fields("name").Value
strcomputer = adoRecordset.Fields("name").Value
CheckWhosLoggedIN(adoRecordset.Fields("name").Value)
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
Sub CheckWhosLoggedIN( strComputer)
' PARAMETERS
'
'strComputer = "c140" ' use "." for local computer
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'
Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 2")
If colSessions.Count = 0 Then
' No interactive session found
'
Wscript.Echo "No interactive user found"
Else
'Interactive session found
'
For Each objSession in colSessions
Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
' Show user info
'
For Each objItem in colList
WScript.Echo "User: " & objItem.Name
WScript.Echo "FullName: " & objItem.FullName
WScript.Echo "Domain: " & objItem.Domain
Next
' Show session start time
'
Wscript.Echo "Start Time: " & objSession.StartTime
Next
End If
' ------------------------------------------------------------------
End Sub
wscript.echo "DONE"