Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 2nd, 2007, 12:27 PM
Authorized User
 
Join Date: Jun 2005
Posts: 42
Thanks: 0
Thanked 1 Time in 1 Post
Default LoginID

How can I a script to get a user LogingID for a user that is logged in another computer using the compure's name?

arnniema
__________________
arnniema
 
Old March 2nd, 2007, 04:40 PM
Authorized User
 
Join Date: Jun 2005
Posts: 42
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry,

I want to write a script that I can runs it from my local PC and I will input the user's computer name I will get the logged user's Login ID. How can I write a script to get the user's LogingID for a user that is logged in another computer using the compure's name?

arnniema
 
Old March 5th, 2007, 11:14 AM
Authorized User
 
Join Date: Jun 2005
Posts: 42
Thanks: 0
Thanked 1 Time in 1 Post
Default


Sorry again,

Here is a better explanation:

Administrator network Client
   A------------------------------------B

I want to write a script that the system administrator can use to obtain the loginID from a user that is logged at a remote workstation and its station is frozen by just typing the workstation name.


arnniema
 
Old March 5th, 2007, 01:29 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Assuming you have administrative rights to the other computer, this might get you there:

sComputer = InputBox("Please enter a computer name")

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}\\" & sComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Environment", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
      sUserName = objItem.UserName
Next
WScript.Echo sUserName

The loop returns quite a few usernames, but they would be like...

<SYSTEM>
NT AUTHORITY\SYSTEM
NT AUTHORITY\SYSTEM
NT AUTHORITY\LOCAL SERVICE
NT AUTHORITY\LOCAL SERVICE
NT AUTHORITY\NETWORK SERVICE
NT AUTHORITY\NETWORK SERVICE

So you can eliminate them from the code by checking for them and then only taking the variable where the objItem.Name <> one of those values.

Did that help? BTW, you can run this from Access. Is that why you are posting here?


mmcdonal
 
Old March 5th, 2007, 02:57 PM
Authorized User
 
Join Date: Jun 2005
Posts: 42
Thanks: 0
Thanked 1 Time in 1 Post
Default

I there!

I have tried your sample code. However, the system return with a different userID not the person presently logged in. It seems that if the system has few user profiles it picks up one at random. Not the current user.

arnniema

arnniema
 
Old March 5th, 2007, 03:23 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Try this and see if you can narrow down what is going on. Remember there are always more than one "user" (service" running on each PC, and this runs through the lot of them and only returns the last one in the list, which is usually the real user:

sComputer = InputBox("Please enter a computer name")

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}\\" & sComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Environment", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
i = 0
For Each objItem In colItems
      UserArray(i) = objItem.UserName
      i = i + 1
Next

i = 0
Do Until i = UBound(UserArray) + 1
   WScript.Echo UserArray(i)
   i = i + 1
Loop

This will show you the whole list of users. Then perhaps you can elminate some of them from the selection.

mmcdonal
 
Old March 5th, 2007, 03:32 PM
Authorized User
 
Join Date: Jun 2005
Posts: 42
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry, I got an error on line 8 Char 7 Type mismatch: 'UserArray'

arnniema
 
Old March 5th, 2007, 04:47 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Put this at the top of your code:

Dim UserArray(0)

Then add this code here:

i = 0
For Each objItem In colItems
      ReDim Preserve UserArray(i) 'add this line
      UserArray(i) = objItem.UserName
      i = i + 1
Next

Did that help?




mmcdonal
 
Old March 5th, 2007, 05:11 PM
Authorized User
 
Join Date: Jun 2005
Posts: 42
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry, no luck

this is the code:

Dim UserArray(0)
sComputer = InputBox("Please enter a computer name")

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}\\" & sComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Environment", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
i = 0
For Each objItem In colItems
      ReDim Preserve UserArray(i) 'add this line
      UserArray(i) = objItem.UserName
      i = i + 1
Next

i = 0
Do Until i = UBound(UserArray) + 1
   WScript.Echo UserArray(i)
   i = i + 1
Loop


I am getting an error: Line 9 Char 7 ERROR: This array is fixed or temporarily locked.

arnniema
 
Old March 6th, 2007, 08:54 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

How about this instead:

Dim UserArray(10)
sComputer = InputBox("Please enter a computer name")

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}\\" & sComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Environment", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
i = 0
For Each objItem In colItems
      If Left(objItem.UserName, 3) <> "NT " And objItem.UserName <> "<SYSTEM>" Then
         UserArray(i) = objItem.UserName
         i = i + 1
      End If
Next

i = 0
Do Until i = UBound(UserArray) + 1
    If UserArray(i) <> "" Then
           WScript.Echo UserArray(i)
       End If
   i = i + 1
Loop

This will give you 10 chances. Then it only outputs strings.


mmcdonal









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.