1

My VB.net written application is being executed by an MSI file, and I need to get the currently logged on user (who is running the MSI). This is because I am importing xml files into the task scheduler and without the correct usersname, there is a mapping error. Currently, because the application is being run through the MSI or windows installer the System user is being used. This is causing a mapping error so I was wondering if there is any other way to find the logged on user.

MsgBox(Environment.UserName)

Dim WSHNetwork = CreateObject("WScript.Network")
MsgBox(WSHNetwork.Username)

Both message boxes return "SYSTEM", whereas I need it to return the actual logged on user.

  • Then you are running the custom action in system context. If you run the custom action with user impersonation you should get the user name. What do you need the scheduled tasks for? [There may be alternatives](https://stackoverflow.com/a/48426736/129130). – Stein Åsmul Jun 17 '19 at 14:49
  • The tasks are for running applications on idle mode – David Rohweder Jun 17 '19 at 15:16
  • Sounds like you need a Windows service instead? [These guys are service experts](https://www.coretechnologies.com/WindowsServices/FAQ.html#ServiceVsTaskScheduler). Just wanted to give you that link, not sure what you really need. – Stein Åsmul Jun 17 '19 at 21:57
  • Your just trying to create a scheduled task? – Christopher Painter Jun 19 '19 at 00:27
  • @ChristopherPainter Yes, but the system account being used is throwing off the logged on user data when I am getting and setting configurations. – David Rohweder Jun 19 '19 at 00:56
  • Try running the CA with Impersonate set to false. Personally I've created many scheduled tasks using custom actions but I do it by calling schtasks.exe /create /RU [username] /RP [password] to configure the task to use a specific service account. – Christopher Painter Jun 19 '19 at 01:03

1 Answers1

0

You could potentially try using WMI:

Dim username, objItem
Dim objWMIService  : Set objWMIService = GetObject( "winmgmts:\\.\root\cimv2" )
Dim colItems  : Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem" )

For Each objItem in colItems
    username = objItem.UserName
    if (instr(username ,"\") > 0) Then
        username = Split(username, "\")(1)
    end if   
Next
msgbox username

Or Quser:

Dim strCmd : strCmd = "cmd /q /c for /f ""skip=1 tokens=1"" %a in ('quser console') do @echo %a"
username = CreateObject("WScript.Shell").Exec(strCmd).StdOut.ReadAll()
username = Right(username,Len(username)-1)
msgbox username
Captain_Planet
  • 1,228
  • 1
  • 12
  • 28