Page 1 of 1 1
Topic Options
#188856 - 2008-07-23 10:03 AM how to catch the output of a script, which is executed under runnas?
xFrog Offline
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 Offline
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 Offline
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 Offline
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:
 Code:
$out=RedirectOutput("")

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 Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 687
Loc: Maryland, USA
Hi xFrog,

It should be just:

 Code:
IF RedirectOutput("") = 0
  ? "Closed log at " + @TIME ?
ENDIF


Regards,

Brad V

Top
#188861 - 2008-07-23 01:29 PM Re: how to catch the output of a script, which is executed under runnas? [Re: BradV]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
The MSg() UDF library on my web site can be used in both parent and child scripts to write to the same log.

I think Msg() is posted here on KORG, but the MSG library on my site has 5 UDFs in one file, all related to various forms of logging.

You can simply embed the same UDF in both scripts, and define the same log file value and everything will work fine.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

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 Offline
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 Offline
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:
 Code:
$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
#188867 - 2008-07-23 07:11 PM Re: how to catch the output of a script, which is executed under runnas? [Re: xFrog]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
To diagnose, display the value of %TEMP% - is it rooted?

If you're using the user's temp folder, it has spaces in the path - are you properly quoting the path to TEMP? I avoid using the user's temp at all costs for this reason. Also - is the TEMP relative to all scripts the same? Same user? You mention using runnas, so there might be different paths for different processes.

Since it works for a network path, I'm guessing that the TEMP environment values are not the same between all script parts. See if the @SCRIPTDIR value is the same - you might use that instead. Would be faster than using a network share.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

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 Offline
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 Offline
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:
  1. Open a command prompt (Start->Run->CMD)
  2. CD to the root of your C: drive (CD \)
  3. Start a new command prompt using your RUNAS user (RUNAS /USER:domain\user cmd)
  4. Notice that the RUNAS command prompt is in a different directory.
  5. 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.
  6. (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
#188876 - 2008-07-24 03:15 PM Re: how to catch the output of a script, which is executed under runnas? [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
That's it!! No more implications for me.. next time I'll just be blunt! \:D

Excellent bluntness on your part, too, Richard! I'll be stealing the RegEdit demonstration for my next class. \:\)

As I just taught in my class yesterday.. a child process will inherit the environment of its parent. Thus, if you have any problems passing an argument to the child process via Kix2Exe or CPAU, you can define several environment variables (using SetL) before calling the child process. The child procecss will have COPIES of those variables. I emphasize COPIES because the child can modify their values without affecting the Parent environment - very much unlike the real world. ;\)

Not for the casual process, but - using the registry and storing a value in HKLM\Software\%USERDOMAIN%\appid:value is an alternative way of passing data. Using an INI file, with read/writeprofilestring() functions is also an option that's easy to implement and easier to clean up afterwards - just delete the file.

So, now you have a couple of ways to solve your problem.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.077 seconds in which 0.038 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org