Sign in to follow this  
Followers 0
kylomas

@username returns authority running script, not logged on user

4 posts in this topic

Hi,

I am running a script at system startup using the task schedular (for elevated authority). @username is returning the name of the authority that the script is running under, NOT the logged on user. Anyone know of a way to get the logged on user givern this scenario? Running Win 7 (64bit) at latest maintenance levels.

Script:

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=..\EXE\watcher.exe
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <array.au3>
#include <date.au3>
#cs
 watchlist structure
 n,0 = process to watch for
 n,1 = program to run
 n,2 = program to run parm #1
 n,3 = this slot intentionally left blank
 n,4 = run program when process starts
 n,5 = run program when process ends
#ce
local $a_watchlist[20][6]
$a_watchlist[0][0] = "iexplore.exe"
$a_watchlist[0][1] = "c:\program files\ccleaner\ccleaner64.exe"
$a_watchlist[0][2] = "/auto"
$a_watchlist[0][3] = ""
$a_watchlist[0][4] = false
$a_watchlist[0][5] = true
$a_watchlist[1][0] = "outlook.exe"
$a_watchlist[1][1] = "c:\program files\ccleaner\ccleaner64.exe"
$a_watchlist[1][2] = "/auto"
$a_watchlist[1][3] = ""
$a_watchlist[1][4] = false
$a_watchlist[1][5] = true
$a_watchlist[2][0] = "wlmail.exe"
$a_watchlist[2][1] = "c:\program files\ccleaner\ccleaner64.exe"
$a_watchlist[2][2] = "/auto"
$a_watchlist[2][3] = ""
$a_watchlist[2][4] = false
$a_watchlist[2][5] = true
local $a_curractive, $a_prevactive = processlist()
while 1
 $a_curractive = processlist()
 checkstarts()
 checkends()
 $a_prevactive = $a_curractive
 sleep(5000)
wend
func checkstarts()
 local $hit
 for $i = 1 to $a_curractive[0][0]
  $hit = false
  for $j = 1 to $a_prevactive[0][0]
   if $a_curractive[$i][0] = $a_prevactive[$j][0] and $a_curractive[$i][1] = $a_prevactive[$j][1] then
    $hit = True
    exitloop
   endif
  next
  if $hit = false then
   ; yes, got something new now check if we are watching for it
   ;dbgarrays()
   for $k = 0 to ubound($a_watchlist,1) - 1
    if $a_curractive[$i][0] = $a_watchlist[$k][0] then
     ;yes, we are watching for it, now what to do???
     if $a_watchlist[$k][4] then
      shellexecute($a_watchlist[$k][1],$a_watchlist[$k][2])
      lf(" Event: " & @username & @tab & $a_watchlist[$k][1] & $a_watchlist[$k][2] & " initiated by program start [" & $a_curractive[$i][0] & ']')
     endif
    EndIf
   next
  endif
 next
endfunc
func checkends()
 local $hit
 for $i = 1 to $a_prevactive[0][0]
  $hit = false
  for $j = 1 to $a_curractive[0][0]
   if $a_prevactive[$i][0] = $a_curractive[$j][0] and $a_prevactive[$i][1] = $a_curractive[$j][1] then
    $hit = True
    exitloop
   endif
  next
  if $hit = false then
   ; yes, something ended now check if we are watching for it
   ;dbgarrays()
   for $k = 0 to ubound($a_watchlist,1) - 1
    if $a_prevactive[$i][0] = $a_watchlist[$k][0] then
     ;yes, we are watching for it, now what to do???
     if $a_watchlist[$k][5] then
      shellexecute($a_watchlist[$k][1],$a_watchlist[$k][2])
      lf(" Event: " & @username & @tab & $a_watchlist[$k][1] & $a_watchlist[$k][2] & " initiated by program stop [" & $a_prevactive[$i][0] & ']')
     endif
    EndIf
   next
  endif
 next
endfunc
func dbgarrays()
 ; this will create a 2d 4 column array of 2 2d arrays side by side (for debugging arrays created by processlist())
 if $a_curractive[0][0] > $a_prevactive[0][0] then
  local $a_tmp[$a_curractive[0][0]][4]
 Else
  local $a_tmp[$a_prevactive[0][0]][4]
 endif
 local $a_tmp_curractive = $a_curractive
 local $a_tmp_prevactive = $a_prevactive
 _arraysort($a_tmp_curractive)
 _arraysort($a_tmp_prevactive)
 for $i = 0 to ubound($a_tmp) - 1
  if $i < ubound($a_tmp_curractive) then
   $a_tmp[$i][0] = $a_tmp_curractive[$i][0]
   $a_tmp[$i][1] = $a_tmp_curractive[$i][1]
  EndIf
  if $i < ubound($a_tmp_prevactive) then
   $a_tmp[$i][2] = $a_tmp_prevactive[$i][0]
   $a_tmp[$i][3] = $a_tmp_prevactive[$i][1]
  endif
 next
 _arraydisplay($a_tmp)
endfunc
Func lf($le)
 Local $file
 $file = FileOpen('c:\watcher_event_log', 1)
 If $file = -1 Then
  MsgBox(0,"Watcher Error", "Error Unable to open log file")
  exit
 EndIf
 FileWriteLine($file, _Now() & $le)
 FileClose($file)
EndFunc

Thanks,

Kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites



The problem lies in the credentials you specify in Task Scheduler. When you specify administrator credentials, it runs your executable as if the administrator executed the program from the administrator account. If you want Ccleaner to run for a specific account, you must specify that account's credentials.


#include <ByteMe.au3>

Share this post


Link to post
Share on other sites

A similar questions has been discussed before. Please check this


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@sleepydvdr,

I figured such, was hoping that I was missing something obvious...

@water,

Thanks, the example will work perfectly...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0