1

I've found several posts on this site and the internet describing how to log out the current user, but how can I log in a specific user?

Log out (cygwin) can be accomplished this way:

echo "\n" | powershell -ExecutionPolicy RemoteSigned "(gwmi Win32_OperatingSystem).Win32Shutdown(0)"
dsummersl
  • 6,588
  • 50
  • 65

2 Answers2

0

Well here's how you would log out a specific user:

function getsessionid($username, $server)
{
   query session $username "/SERVER:$server" |?{$_ -match "$userName\s+(\d+)\s"} |%{ $matches[1] }
}

$id = getsessionid john db123.contoso.net
logoff $id /SERVER:db123.contoso.net

To log someone in, it seems tougher. Take a look here and here.

Community
  • 1
  • 1
latkin
  • 16,402
  • 1
  • 47
  • 62
0

You can log in a user with the AutoAdminLogon feature.

To store the password in encrypted rather than plaintext form, see here.

After configuring automatic logon, you will need to reboot the computer in order to make the logon happen. You can bypass the need for a reboot by using a custom GINA, although this is a bit complicated and does not work on Vista or later.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • I'm specifically interested in explicitly logging out user X and logging user X back in again in some scripted way - I'm using the auto admin for the normal function of this image. I guess worse case I could simply 'restart' the computer to log in again, but that isn't ideal. Is there really no programmatic way to login and out of windows XP? – dsummersl Aug 25 '12 at 00:52
  • If the user account was logged in automatically, and you've set the ForceAutoLogon option, it should log back in automatically each time you log off. Does that help? – Harry Johnston Aug 25 '12 at 00:57
  • For Windows XP only (not Vista or later) one alternative is a custom GINA. I used to successfully use this approach to avoid needing a reboot. If you email me (see my profile) I'll send you a copy of my old code when I get back to work on Monday. – Harry Johnston Aug 25 '12 at 01:02
  • Note also that non-interactive logons (if you need to run code in a particular user's context but don't need a GUI) are reasonably straightforward. From context, however, I assume that you're only interested in interactive logons. – Harry Johnston Aug 25 '12 at 01:12
  • I'm trying to automate system setup like this: one account is setup with a login script that starts my supported program (using force autologin). Our current upgrade process is to manually log out, do our upgrade, and then login. I was hoping an automated logout and login could be accomplished, but if not I guess I could change the automated version of an upgrade so that our app isn't started during the login process, and just put it all back and logout again to start things up again... – dsummersl Aug 25 '12 at 01:24
  • I'm not sure if the GINA approach would work; it sounds as if it might. I'd like to explore that route if possible...I'll email you. Thanks! – dsummersl Aug 25 '12 at 01:25