Question

I am trying to save output to an array, and print first element, but nothing is displayed

logins = []

for us in c.Win32_LogonSession():
    try:
        for user in us.references("Win32_LoggedOnUser"):
            logins.append(user.Antecedent.Domain, user.Antecedent.Name, sep="\\")
            print(logins[0])
    except:
        pass

How to correct this?

EDIT

No exceptions are being raised. If I re-write the code

for us in c.Win32_LogonSession():
    try:
        for user in us.references("Win32_LoggedOnUser"):
            print(user.Antecedent.Domain, user.Antecedent.Name, sep="\\")
    except:
        pass

It lists all the users

DOMAIN\Glowie
DOMAIN\service_account
DOMAIN\service_account
DOMAIN\service_account
DOMAIN\service_account
Was it helpful?

Solution

Here is final working code

user_list = []
for us in c.Win32_LogonSession():
    try:
        for user in us.references("Win32_LoggedOnUser"):
            #print(user.Antecedent.Domain, user.Antecedent.Name, sep="\\")
            user_logins = user.Antecedent.Domain + "\\" + user.Antecedent.Name
            user_list.append(user_logins)

    except:
        pass


userID = user_list[0]
print(userID)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top