def Connect(id): current = Ts.WTSGetActiveConsoleSessionId() if current == id: print "Already connected to session %d." % id return True # because we *are* connected to the requested session domain, user = GetUsername(id) target = "%s\\%s" % (domain, user) target, passwd, persist = GetPassword(target) domain, user = Cred.CredUIParseUserName(target) print ` domain `, ` user `, ` passwd ` try: Wts.winstation.Connect(None, id, None, passwd, True) except Wts.Win32Error as e: print "[%s] %s" % (e.err, e.message) if e.args[0] == 1326: Cred.CredUIConfirmCredentials(target, False) return False else: Cred.CredUIConfirmCredentials(target, True) return True
} win32cred.CredWrite(cred) pwd = None print win32cred.CredRead(target, win32cred.CRED_TYPE_DOMAIN_PASSWORD) ## Marshal saved credential and use it to log on mc = win32cred.CredMarshalCredential(win32cred.UsernameTargetCredential, target) th = win32security.LogonUser(mc, None, '', win32con.LOGON32_LOGON_INTERACTIVE, win32con.LOGON32_PROVIDER_DEFAULT) win32security.ImpersonateLoggedOnUser(th) print 'GetUserName:'******'s profile. (first check if user has a roaming profile) username, domain = win32cred.CredUIParseUserName(target) user_info_4 = win32net.NetUserGetInfo(None, username, 4) profilepath = user_info_4['profile'] ## LoadUserProfile apparently doesn't like an empty string if not profilepath: profilepath = None ## leave Flags in since 2.3 still chokes on some types of optional keyword args hk = win32profile.LoadUserProfile(th, { 'UserName': username, 'Flags': 0, 'ProfilePath': profilepath }) ## Get user's environment variables in a form that can be passed to win32process.CreateProcessAsUser env = win32profile.CreateEnvironmentBlock(th, False)