#188856 - 2008-07-23 10:03 AM
how to catch the output of a script, which is executed under runnas?
|
xFrog
Fresh Scripter
Registered: 2008-06-05
Posts: 12
|
Hello everybody,
I've got a script, that collects some information from my PC and stores this information in a file using the redirectoutput() function. But there is a little problem: Executing a script with runnas, creates a new console which output isnt caught from the redirectoutput function.
How can I get the output of the runnas-console in my txt-file too?
Thanks in advance 
Greetz, xFrog
|
|
Top
|
|
|
|
#188857 - 2008-07-23 11:33 AM
Re: how to catch the output of a script, which is executed under runnas?
[Re: xFrog]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
In the sub-script that you call using RUNAS,
- Use RedirectOutput() to redirect to a new file - if the output file is already open for output in a parent script you won't be able to open it again.
- Make sure that you are in the correct folder. When you execute under RUNAS your context will change, and you may end up in (say) C:\Windows\System32 so use the KiXtart "GO" and "CD" command to ensure that you are in the right folder before you issue the RedirectOutput()
If you want to append to the same file you need to close the RedirectOutput() in the parent before calling the script then re-open it afterwards.
|
|
Top
|
|
|
|
#188858 - 2008-07-23 12:18 PM
Re: how to catch the output of a script, which is executed under runnas?
[Re: Richard H.]
|
xFrog
Fresh Scripter
Registered: 2008-06-05
Posts: 12
|
Thanks for your answer, Richard.
Can you tell me, how to close the file, I created at the beginning of my parent-script with $out=redirectoutput("...")?
Thanks
Edited by xFrog (2008-07-23 12:19 PM)
|
|
Top
|
|
|
|
#188859 - 2008-07-23 12:29 PM
Re: how to catch the output of a script, which is executed under runnas?
[Re: xFrog]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Sure - it's documented in the manual, but basically this is what you need:
|
|
Top
|
|
|
|
#188860 - 2008-07-23 12:31 PM
Re: how to catch the output of a script, which is executed under runnas?
[Re: xFrog]
|
BradV
Seasoned Scripter
  
Registered: 2006-08-16
Posts: 687
Loc: Maryland, USA
|
Hi xFrog,
It should be just:
IF RedirectOutput("") = 0
? "Closed log at " + @TIME ?
ENDIF
Regards,
Brad V
|
|
Top
|
|
|
|
#188862 - 2008-07-23 03:19 PM
Re: how to catch the output of a script, which is executed under runnas?
[Re: Glenn Barnas]
|
xFrog
Fresh Scripter
Registered: 2008-06-05
Posts: 12
|
Thanks everybody, I'll try and give a response when I'm done with this task.
Another question: How can I hide the console window which is opened by the runnas tool?
Edited by xFrog (2008-07-23 03:19 PM)
|
|
Top
|
|
|
|
#188866 - 2008-07-23 06:47 PM
Re: how to catch the output of a script, which is executed under runnas?
[Re: Richard H.]
|
xFrog
Fresh Scripter
Registered: 2008-06-05
Posts: 12
|
@Richard
You're right, it's documented like that:
If this parameter is an empty string (""), output is redirected to the screen.
But it wasn't clear for me, that this is an equivalent to "closing the file-datastream" 
So far... I got my script working, but during the tests of my script I realized something else I don't understand.
I'll give a little overview to my script, that you are able to relate on my "little problem".
- I used kix2exe to "pack" all the necessary files. - The logonscript.exe is placed on the logon share of our domain-controller
As I wrote above, sometimes it was necessary to create a temporary file, which gets placed in the "main logfile" later in the script, using the display() function.
I tried to place this tempfile in the %temp% directory of the current user like that:
$out=redirectoutput("%temp%\tempfile.txt") But the tempfile didn't appear, except I ran the script from my local hard disk drive.
At the moment, I redirect the tempfile to a share on our server. This works fine.
Somebody any idea?
Thanks, xFrog
|
|
Top
|
|
|
|
#188870 - 2008-07-24 09:51 AM
Re: how to catch the output of a script, which is executed under runnas?
[Re: Glenn Barnas]
|
xFrog
Fresh Scripter
Registered: 2008-06-05
Posts: 12
|
Everything works fine, when using the @scriptdir folder to save the temp-files. Thanks Glenn!
Now, there's just this little question left, how to hide the console window, created by the runnas process?
Edit: I solved this little problem by using the tool "cpau" instead of runnas. cpau provides a switch called -hide which hides the consolewindow. (cpau is also used by kix2exe)
Thanks @ all!
Edited by xFrog (2008-07-24 10:45 AM)
|
|
Top
|
|
|
|
#188871 - 2008-07-24 09:54 AM
Re: how to catch the output of a script, which is executed under runnas?
[Re: Glenn Barnas]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
As Glenn implies, using any of the RUNAS type tools will change your environment.
It makes sense when you think about it - if you are running the job as someone else, you would expect to have their environment, as if you had logged on as them.
Try the following to demonstrate the process:
- Open a command prompt (Start->Run->CMD)
- CD to the root of your C: drive (CD \)
- Start a new command prompt using your RUNAS user (RUNAS /USER:domain\user cmd)
- Notice that the RUNAS command prompt is in a different directory.
- At each of the two command prompt type "SET" to view the environment variables - notice that some are missing and many are different, including TEMP.
- (if you have never used regedit then skip this step as you can badly damage the OS and applications by fooling around in the registry) At each prompt start regedit, and take a look at "HKEY_CURRENT_USER" - notice again these are very different
So that demonstrates the problem, now how do you get around it?
Well, the easiest way is to pass the variable on the command line to the child script. For example, in non-Kix2exe scenario you would do this:
Dim $MYLOGFILE
$MYLOGFILE=%TEMP%+"\logfle.txt"
; Pass log file to child script SHELL '"'+%COMSPEC%+'" /C kix32.exe $$MYLOGFILE="'+$MYLOGFILE+'" childscript.kix"
|
When "childscript.kix" starts the variable MYLOGFILE will be set. Kix2Exe probably provides a similar mechanism for passing parameters to the script.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|