How to get current user name from process started using Ru..

G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.configuration_manage,microsoft.public.windowsxp.general,microsoft.public.windowsxp.perform_maintain,microsoft.public.windowsxp.setup_deployment (More info?)

Hi all

How to get current user name (name of the user that logged on) from process
that was started using "RunAs" command?

For example

User "MarkSmith" has started a VB application using RunAs command where user
name was "Admin". Now VB app needs to copy some file to MarkSmith
application data folder (C:\Documents and Settings\marksmith\Application
Data\). I need to know how to get user name to do that. If I use %username%
or WshMetwork.UserName I am getting "Admin" instead "MarkSmith".

Maybe I could read user name from registry?

Thank you for your help

Marek Suski
 
G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.configuration_manage,microsoft.public.windowsxp.general,microsoft.public.windowsxp.perform_maintain,microsoft.public.windowsxp.setup_deployment (More info?)

"Marek Suski" <None@none.pl> wrote in message
news:cu7ppp$3l8$1@inews.gazeta.pl...
> Hi all
>
> How to get current user name (name of the user that logged on) from
process
> that was started using "RunAs" command?
>
> For example
>
> User "MarkSmith" has started a VB application using RunAs command where
user
> name was "Admin". Now VB app needs to copy some file to MarkSmith
> application data folder (C:\Documents and Settings\marksmith\Application
> Data\). I need to know how to get user name to do that. If I use
%username%
> or WshMetwork.UserName I am getting "Admin" instead "MarkSmith".
>
> Maybe I could read user name from registry?
>
> Thank you for your help
>
> Marek Suski

I suspect you're querying the parent process for %username%
instead of the child process. When you spawn a child process
at the Command Prompt then %username% returns the name
of the account used for runas.exe.
 
G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.configuration_manage,microsoft.public.windowsxp.general,microsoft.public.windowsxp.perform_maintain,microsoft.public.windowsxp.setup_deployment (More info?)

Hi

Yes. I would like to get parent process user name with ... Visual Basic
Script (from MSI installer). Is there a way how to do that?

Marek Suski

"Pegasus (MVP)" <I.can@fly.com> wrote in message
news:OZ65ljRDFHA.936@TK2MSFTNGP12.phx.gbl...
>
> "Marek Suski" <None@none.pl> wrote in message
> news:cu7ppp$3l8$1@inews.gazeta.pl...
> > Hi all
> >
> > How to get current user name (name of the user that logged on) from
> process
> > that was started using "RunAs" command?
> >
> > For example
> >
> > User "MarkSmith" has started a VB application using RunAs command where
> user
> > name was "Admin". Now VB app needs to copy some file to MarkSmith
> > application data folder (C:\Documents and Settings\marksmith\Application
> > Data\). I need to know how to get user name to do that. If I use
> %username%
> > or WshMetwork.UserName I am getting "Admin" instead "MarkSmith".
> >
> > Maybe I could read user name from registry?
> >
> > Thank you for your help
> >
> > Marek Suski
>
> I suspect you're querying the parent process for %username%
> instead of the child process. When you spawn a child process
> at the Command Prompt then %username% returns the name
> of the account used for runas.exe.
>
>
 
G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.configuration_manage,microsoft.public.windowsxp.general,microsoft.public.windowsxp.perform_maintain,microsoft.public.windowsxp.setup_deployment (More info?)

Marek Suski wrote:

> Yes. I would like to get parent process user name with ... Visual
> Basic Script (from MSI installer). Is there a way how to do that?
Hi

Using Win32_Process to get the owner of 'explorer.exe' where
SessionID = 0 is an option (see VBScript below).

Note that there is not always a one to one connection between the user
name and profile path, sometimes you will find that the profile folder
is MarkSmith.000 or MarkSmith.<somedomain> etc.


'--------------------8<----------------------

sUser = ConsoleUser(".") ' use "." for local computer

MsgBox "Console user: " & sUser, _
vbInformation + vbSystemModal, "Get user name"


Function ConsoleUser(sHost)
' Returns name of user logged on to console
' If no users are logged on, returns ""
Dim oWMI, colProc, oProcess, sUser, sDomain
Set oWmi = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(debug)}!\\" _
& sHost & "\root\cimv2")

Set colProc = oWmi.ExecQuery("Select Name from Win32_Process" _
& " Where Name='explorer.exe' and SessionID=0")

ConsoleUser = ""
For Each oProcess In colProc
lRet = oProcess.GetOwner(sUser, sDomain)
If lRet = 0 Then
ConsoleUser = sUser
End If
Next
End Function
'--------------------8<----------------------




--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
 
G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.configuration_manage,microsoft.public.windowsxp.general,microsoft.public.windowsxp.perform_maintain,microsoft.public.windowsxp.setup_deployment (More info?)

Torgeir Bakken (MVP) wrote:

> Marek Suski wrote:
>
>> Yes. I would like to get parent process user name with ... Visual
>> Basic Script (from MSI installer). Is there a way how to do that?
>
> Hi
>
> Using Win32_Process to get the owner of 'explorer.exe' where
> SessionID = 0 is an option (see VBScript below).
>
> Note that there is not always a one to one connection between the user
> name and profile path, sometimes you will find that the profile folder
> is MarkSmith.000 or MarkSmith.<somedomain> etc.
Hi

For Win2k3 Server and Windows XP (as long as the "Terminal Services"
service is not disabled), to obtain the name of the current console
user, another option is to parse the output of Qwinsta.exe (comes
builtin with the OS).

To list the console user, run the following command (to see the output,
run it in a command prompt)

QWINSTA.EXE console /SERVER:%COMPUTERNAME%

(the /SERVER: is not really necessary for local computer)


Here is a VBScript that obtains the console user name and puts it in a
variable:

'--------------------8<----------------------
Set oWshNet = CreateObject("WScript.Network")
' use current computer
sComputerName = oWshNet.ComputerName

sUserName = GetConsoleUser(sComputerName)
If sUserName <> "" Then
MsgBox "Console user name: " & sUserName
Else
MsgBox "Console user name not found with QWINSTA.EXE"
End If

Function GetConsoleUser(sHost)
' Function will return console user name from QWINSTA.EXE
' Windows XP and Win2k3 Server only

Set oShell = CreateObject("Wscript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

sTempFile = oFS.GetSpecialFolder(2).ShortPath & "\" & oFS.GetTempName

'Run command via Command Prompt and use for /f to extract user name
'Dump results into a temp text file
oShell.Run "%ComSpec% /c for /f ""skip=1 Tokens=2"" %i in " _
& "('%SystemRoot%\System32\QWINSTA.EXE console /SERVER:" _
& sHost & "') do echo %i >" & sTempFile, 0, True

GetConsoleUser = "" 'init value

If oFS.FileExists(sTempFile) Then
'Open the temp Text File and Read out the Data
Set oTF = oFS.OpenTextFile(sTempFile)

'Parse the text file
Do While Not oTF.AtEndOfStream
GetConsoleUser = Trim(oTF.ReadLine)
Loop

'Close it
oTF.Close
'Delete It
oFS.DeleteFile sTempFile
End If

End Function
'--------------------8<----------------------

--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx