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!

Getting the Users list of Win 7 with VFP

Status
Not open for further replies.

dkean4

Programmer
Feb 15, 2015
282
US
Is there a way with WMI or so to list all users on a PC (VFP code of course)?



Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
This may be a starting point:

Code:
#DEFINE JUST_EXPLORER_USERS	.F.

LOCAL WMIService
LOCAL Processes
LOCAL Process
LOCAL Username AS String
LOCAL Domain AS String

CREATE CURSOR LoggedUsers (Username Varchar(64), Domain varchar(64))

m.WMIService = GETOBJECT("winmgmts:\\.\root\cimv2")
#IF JUST_EXPLORER_USERS
m.Processes = m.WMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Caption = 'explorer.exe'",,48)
#ELSE
m.Processes = m.WMIService.ExecQuery("SELECT * FROM Win32_Process",,48)
#ENDIF

FOR EACH m.Process IN m.Processes

	m.Username = ""
	m.Domain = ""

	IF m.Process.GetOwner(@m.Username, @m.Domain) = 0
		
		LOCATE FOR Username == m.Username AND Domain == m.Domain

		IF !FOUND()
			INSERT INTO LoggedUsers VALUES (m.Username, m.Domain)
		ENDIF
	ENDIF

ENDFOR

BROWSE
 
dkean4 said:
list all users on a PC

What do you mean by "all users on a PC"?
* All users in the Active Directory?
* All users who use a specific workstation?
* All users who run a specific VFP application?

For the last 2, you could use VFP's own SYS(0).

For #2 you could build a VFP EXE and ran it as a Windows Scheduled task to periodically (for example: every 5 or 10 minutes) to quickly examine the Windows user and log it to a data table then quit.

For #3 you could call it whenever the VFP application was launched and log the info to a data table.

For #1, you might try Mike Gagnon's reply in
READING ACTIVE DIRECTORY USERLIST (LDAP) IN VFP 8
thread184-1143741

Good Luck,
JRB-Bldr



 
Atlopes will list all active users, if you have sufficient privilieges to read all users processes.
JRB-Bldr has the most professional idea with pointing to active directory.

There is an intermediate way, to find all PC users by ADIR of User profile directory folders, that'll give a list of all users ever logged in at a computer, including he current one.
Disadvantages: It can obviously contain inactive users, as locally copied user profiles are not deleted with the removal of a domain user. It lists several default and server users.
Advantages: It doesn't contain all domain users, but only the ones using this client. It also works in small workgroups not joined to a domain.

Code:
Local lcAllProfiles, lnRow
lcAllProfiles = Fullpath("..\",ADDBS(GETENV("USERPROFILE")))
FOR lnRow = 3 TO ADIR(laUsers,lcAllProfiles+"*","D",1)
   ? laUsers[lnRow,1]
ENDFOR

Note: The FOR loop starts with lnRow index 3, because the array created by ADIR will return the pseudo paths "." and "..", too.
You can filter out "Public", the folder for all users and many more, eg all containing "mssql" or "server" willl filter out most system accounts.

Bye, Olaf.
 
Yes, sorry. Renamed and didn't replace all. Now fixed that for all later readers.
 
atlopes, jrbbldr and Olaf

Sorry for the delay, guys. I have been called away for a whole week. I'm back today.

All the answers are great and useful and they each work just fine.

atlopes' code was good enough, but others gave me even more. I so appreciate each one. Thank you for the quick reply. You guys are great.

I am curious about the following list which came up with your code, Olaf

Code Output of Olaf said:
Classic .NET AppPool
DefaultAppPool
Dennis
Public
Simply
wordpress
xDocMessaging]

The red listings Simply, wordpress and xDocMessaging are registered as users and come up as such with your code. I believe that they all belong to WordPress. But I do not have any WordPress application installed on my PC... not that I know of.

I ask just in case you have any ideas. The WEB does not say much about these user accounts. And these appear as subdirectories in my Users directory. Is there any way to flush them out? Their directories have lots of subdirectories and crap in them, though many are empty and some resist attempt to enter or delete them. They are in the C:\Users directory, to be clear.

Users_Acct_h0hzk1.png



Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Well, you can take ownership over them and THEN delete them.
On the other hand, something that you didn't think of MAY stop to work
 
drdolittle,

Paranoia, Doc. Do you have a pill for that? B-)

Just kidding... How do you take ownership over a user account like that? RightClick and mess with the front subdirectory security? Or do I have to do that for all the directories which resist my paranoia? That usually takes half the afternoon... Any killer shortcut?

Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Of course these are directories in C:\Users, that's what ADIR is listing, directory names of all folder parallel to your own user profile folder, that's what '..\' does.
Edit: Notice there is a base directory difference in Vist and later and XP and earlier:
XP-: %SystemDrive%\Documents and Settings\{username} on earlier OSes
Vista+: SystemDrive%\Users\{username}

But in all systems %USERPROFILE%\.. is the directory having all profile folders.
From what I see with querying Active Directory besides System account, also Services and Usergroups can have a profile folder. The deletion of such folders will not delete the accounts, services, etc. and the gain in free disc space would seldom outweigh possible problems of things stopping to work. Some software might simply write into a self made directory within C:\USers without Windows itself maintaining that, but I wouldn't fiddle. If you have reason to assume the wordpress or xDocMessaging are malware in disguise you shouldn't remove the folder but find the root cause of these accounts. There are many more types than domain/local user accounts.

So: Deleting a user profile directory doesn't delete a user profile. Go to user account control and delete unwanted accounts there. If you don't find it there, it might be a group or other system/hidden account or service...

DefaultAppPool and Classic .NET Pool should come from IIS, that should be OK. They would also come up, but not with normal usual user account control, as those are system accounts buried more deep into the system as a normal workgroup or domain or local user account. There are more default system accounts aslo not showing up as profile folder.

You shouldn't be too worried.

Bye, Olaf.
 
Those user accounts do not come up at all in the windows User account in Control Panel.

Thanks for the tips, Olaf...




Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Yes, but they will come up, if you query Active Directory, and they come up, if you edit folder/file security and search for accounts...

Bye, Olaf.
 
I must have tested Word press at one time or another. Much obliged, Olaf.

Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Fetching user accounts through WMI, logged or not:

Code:
#DEFINE JUST_LOCAL_ACCOUNTS	.T.

LOCAL WMIService
LOCAL Accounts
LOCAL Account
LOCAL Username AS String
LOCAL Domain AS String

CREATE CURSOR UserAccounts (Username Varchar(64), Domain varchar(64))

m.WMIService = GETOBJECT("winmgmts:\\.\root\cimv2")
#IF JUST_LOCAL_ACCOUNTS
m.Accounts = m.WMIService.ExecQuery("SELECT * FROM Win32_UserAccount WHERE LocalAccount = 1",,48)
#ELSE
m.Accounts = m.WMIService.ExecQuery("SELECT * FROM Win32_UserAccount",,48)
#ENDIF

FOR EACH m.Account IN m.Accounts

	INSERT INTO UserAccounts VALUES (m.Account.Name, m.Account.Domain)

ENDFOR

BROWSE
 
atlopes,

Thanks, I now see three accounts, Administrator, Dennis, Guest. I took out all other accounts.

By the way, your first code ran fine when I applied True to JUST_LOCAL_ACCOUNTS. You sent it to me with .F.

Code:
#DEFINE JUST_LOCAL_ACCOUNTS	.F.

Unless it is set to True I get this error...

Errora1324_zbmria.png


Is that an indication of something abnormal in my PC? Do you get a different response on your PC?

Also, when I allow someone else to log into my PC remotely, how can I see their logins, since there are no accounts other than mine into which they can log in? Yet I have allowed other colleagues, many times, to log into my PC. Do they simply register as my account or Administrator? My Guest account is always kept blocked, so that is not to worry.

I want to know via VFP if anyone else has remoted into my PC, so I can accommodate them automatically, maybe call back or just shut them down.



Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
In regard of atlopes script: I get the same list for both settings on my home PC, which isn't joined to a domain. Have to test on another PC tomorrow.
No C5 error so far.

In regard of remote logins: There are several ways of remoting. Teamviewer? RDP? What did you do?

The typical login is via some Windows Account in the end, which needs to be part of the RemotedesktopUsers. Teamviewer needs no account, if Teamviewer ID and password are known, anyone can login, when Teamviewer is set up to wait for connections.

You should know what you do and allow.

Bye, Olaf.
 
Olaf,

I use Teamviewer occasionally. But I am curious about MicroSoft's "Remote Desktop Connection Manager 2.7". Sometimes others using my PC do things I am not aware of and I'm tightening the rope.



Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top