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!

randomMicrosoft VBScript runtime error: Permission denied: 'GetObject'

Status
Not open for further replies.

rod602

Technical User
May 16, 2011
66
US
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.
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"
 
Perhaps you should define your objects as you have "option explicit" at the top

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
@Geates,

Thanks for your reply. Even when I commented it out, the issue persist.
 
The remote registry service may not be running on computer c140 or you simply don't have permissions on that machine. does it work on your locally?

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top