1

Basically the cygwin CLI (or bash in particular, if that helps) equivalent of this question. I want to start cygwin as administrator, but then get my name, not the administrator's name, somehow. (All the environment variables like $USER, $USERNAME, etc. get set to the administrator.)

This could include any utility that comes with Windows, but it has to come with all editions, not just Pro.

Community
  • 1
  • 1
Kev
  • 15,899
  • 15
  • 79
  • 112
  • I assume you don't want to grant administrative permissions to your account only enable them when needed ? – Marged Nov 01 '15 at 19:51
  • Why can't you use the answer to the linked question? It should work just as well in a Cygwin program as in a Windows one. (If you mean you want to use built-in tools, there probably isn't one.) – Harry Johnston Nov 01 '15 at 21:01
  • @HarryJohnston, because I'm not coding in C++, I'm making a bash script. I just want to know the current user so I can get their correct Windows user directory. – Kev Nov 01 '15 at 21:27
  • In other words, you're asking for a built-in tool. It seems unlikely to me that the base Cygwin distribution includes such obscure functionality, but I could be wrong. – Harry Johnston Nov 01 '15 at 21:33
  • 1
    Incidentally, there is no straightforward way to reliably derive the path to the user's profile directory from their username. (`c:\users\` isn't always the right path.) But depending on your scenario that might not matter. – Harry Johnston Nov 03 '15 at 04:41
  • You can execute a win32 executable from within cygwin right? If so you can use quser.exe then parse the output. One user will have session status "active" and that should be the one you need. – Syberdoor Nov 03 '15 at 08:53
  • @HarryJohnston built-in meaning bundled with Windows would also work. +1 for your comment about directories vs. user names, I was not aware of this. – Kev Nov 03 '15 at 13:18
  • @Syberdoor, this seemed promising, but alas, I need to support Home edition users too, and, well...http://superuser.com/questions/508708/quser-does-not-exist-on-windows-7 – Kev Nov 03 '15 at 13:19
  • I would have said that Windows had no need for such functionality and is therefore just as unlikely to have it, if Syberdoor hadn't already pointed out that it kind of does. :-) Although note that that solution won't work if multiple users are active at the same time, or if the batch file is running in the background. – Harry Johnston Nov 03 '15 at 20:29
  • Would it be acceptable to launch Cygwin via a shortcut? Because the shortcut should be able to incorporate the current user name (or the current profile directory, for that matter) into the command line, and presumably the Cygwin shell's command line can set environment variables or run a specified command on startup or something. I'm not absolutely certain what happens when a shortcut contains environment variable references *and* is configured to run-as-admin, but my guess is that the environment variables would be parsed first. – Harry Johnston Nov 03 '15 at 20:32
  • I see no solution that is not somewhat hacky so how about something like this: Query the processinfo for your own running process and get the sessionid (can be done e.g. in wmi with Win32_Process) and then get the explorer.exe process with the same session id and determine it's owner. – Syberdoor Nov 04 '15 at 10:13
  • 1
    Wmic command (i hope this one is included I can not check because all our windows are pro or enterprise) would be: "wmic PATH Win32_Process WHERE "Name='explorer.exe' AND SessionId=1" CALL GetOwner". An alternative to wmi would be "tasklist /V /FO list" and parse that – Syberdoor Nov 04 '15 at 10:27

1 Answers1

1
realCurrentUser=$(wmic PATH Win32_Process WHERE "Name='explorer.exe' AND SessionId=1" CALL GetOwner | grep User | sed 's/^\s*User = "//' | sed 's/";$//')
Kev
  • 15,899
  • 15
  • 79
  • 112