Redirect text to a file (runas command result)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Redirect text to a file (runas command result)

#1 Post by avizee » 14 Jan 2020 01:58

Thank you for reading first.

------------------------------------------------------------------

I need runas result text.

But this redirect command does not work.

"echo .|runas /user:administrator echo %date% > test.txt"

------------------------------------------------------------------

This is the text data what I need.

Attempting to start echo 2020-01-14 as user "WIN2012\administrator" ...
RUNAS ERROR: Unable to run - echo 2020-01-14
1326: The user name or password is incorrect.

------------------------------------------------------------------

How can I redirect these text data to .txt file.

Thanks a lot

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Redirect text to a file (runas command result)

#2 Post by penpen » 14 Jan 2020 03:41

I actually can't test if redirecting is possible with runas at all, but if it were then i assume you need both streams (stdio and stderr); untested:

Code: Select all

echo.|((runas /user:administrator echo %date%" 2>&1) >"test.txt")
penpen

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Redirect text to a file (runas command result)

#3 Post by avizee » 14 Jan 2020 08:29

Appreciate the quick response.

But it doesn't work. :)

echo .|((runas /user:nopasstest "echo %date%" 2>&1) > test.txt)

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Redirect text to a file (runas command result)

#4 Post by avizee » 14 Jan 2020 08:37

echo .|((runas /user:nopasstest "echo %date%" 2>&1) > "test.txt")
((echo .|runas /user:nopasstest "echo %date%" 2>&1) > "test.txt")
(echo .|(runas /user:nopasstest "echo %date%" 2>&1) > "test.txt")
echo .((|runas /user:nopasstest "echo %date%") > "test.txt" 2>&1)
((echo .|runas /user:nopasstest "echo %date%") > "test.txt" 2>&1)
(echo .|(runas /user:nopasstest "echo %date%") > "test.txt" 2>&1)

None of them work.

But thank you :)

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: Redirect text to a file (runas command result)

#5 Post by Eureka! » 14 Jan 2020 14:05

Some remarks:
  1. runas.exe does not support entering a password through a pipe ("|").
    If you want to pass a password, look at utilities like sanur (runas spelled backwards; basically a wrapper around runas.exe that feeds the password) or CPAU (joeware.net).
    (IIRC; must be at least 10 years since I used them the last time ..)
  2. The command you give (echo %date%) is passed on to Explorer (the shell). That doesn't know how to handle the echo command.
    Use cmd.exe /c echo %date% instead. Or somescript.cmd; Explorer can run that too if you got the right file associations in place.
  3. If you run some program as a different user, you have no guarantees what the working directory of that program will be. And thus no idea where your text.txt will be written. Use a full path instaed: c:\test\test.txt.

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Redirect text to a file (runas command result)

#6 Post by avizee » 14 Jan 2020 17:56

Hi! Eureka!

1. I know '|' do not support entering password 'runas.exe' command :)
2. I only need 'runas.exe' error message ( 1326: The user name or password is incorrect. )
and does not work 'cmd /c' command

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Redirect text to a file (runas command result)

#7 Post by jeb » 15 Jan 2020 01:20

Hi avizee,

I tested

Code: Select all

echo( | runas /user:asdf "echo %date%" > test.txt
It works, the complete text is in test.txt.
The only difference to your first example, is the enclosing of echo %date% into quotes

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Redirect text to a file (runas command result)

#8 Post by avizee » 15 Jan 2020 15:04

Thanks for the help :)

But this doesn't work either

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Redirect text to a file (runas command result)

#9 Post by jeb » 15 Jan 2020 15:53

How it fails, what is the error message or the unexpected behavior?

Do you can write files in the current directory at all?

Did you test it on the command line or somewhere inside a batch file?

Which Wndows version do you use?

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Redirect text to a file (runas command result)

#10 Post by avizee » 15 Jan 2020 17:47

I tested Windows 10, Windows Server 2012 R2, Windows Server 2016.

There are no error message.

Test results are this. :)

-------------------------------------------

Enter the password for nopasscheck:

Enter the password for asdf:

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: Redirect text to a file (runas command result)

#11 Post by Eureka! » 16 Jan 2020 04:55

avizee wrote:
14 Jan 2020 17:56
and does not work 'cmd /c' com
Please provide better feedback than "does not work" !!

When you provide no password or the wrong one, the program will not be started, of course.
When you enter (manually!) the password when entering this command:

Code: Select all

runas /user:administrator "cmd /c pause"
You can see what happens: a separate process is started (as administrator), executing the pause command.
When that command was echo %date%, that separate CMD window would close right after executing that command.

So what exactly is not working? What is the output? What else happens on your screen? Did you get an error beep? Did your power supply explode?



It turns out I didn't fully get what you want to accomplish. You are solely interested in the error message:
avizee wrote: This is the text data what I need.

Attempting to start echo 2020-01-14 as user "WIN2012\administrator" ...
RUNAS ERROR: Unable to run - echo 2020-01-14
1326: The user name or password is incorrect.
Well, runas.exe doesn't write it's output to the standard stream or error stream, so it can't be redirected that way.
There are probably (complicated!) ways around that, but there is an alternative:

PSEXEC (Sysinternals) uses the same errormessages and error codes. And this output can be redirected:

Code: Select all

+ T:\>psexec64 -nobanner -u administrator -p wrong cmd.exe
PsExec could not start cmd.exe:
The user name or password is incorrect.

+ T:\>echo %errorlevel%
1326

+ T:\>
Last edited by Eureka! on 16 Jan 2020 07:32, edited 1 time in total.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Redirect text to a file (runas command result)

#12 Post by jeb » 16 Jan 2020 07:17

Eureka! wrote:
16 Jan 2020 04:55
Well, runas.exe doesn't write it's output to the standard stream or error stream, so it can't be redirected that way.
This can't be correct, I tested it with Win10 10.0.17134.1184 and I can redirect it without problems.
And also with Win7 6.1.7601 the redirection works.

In both cases the content of test.txt is

Code: Select all

C:\> echo( | runas /user:asdf "echo %date%" > test.txt

c:\> type test.txt
Enter the password for asdf:

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: Redirect text to a file (runas command result)

#13 Post by Eureka! » 16 Jan 2020 07:28

jeb wrote:
16 Jan 2020 07:17
This can't be correct [...]
I *knew* this was coming!
But it would take me too much time to specify when and what would be redirected under which circumstances.
I assumed @avizee would get the picture that the error message could not be redirected that way ...

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Redirect text to a file (runas command result)

#14 Post by avizee » 16 Jan 2020 14:47

This is what I making

Code: Select all

    net user | findstr /v /i "change profile Memberships User's \\ successfully -------------------------------------------------------------------------------" > [#####]_useracc_temp1.txt 2> nul

    type [#####]_useracc_temp1.txt | findstr . > [#####]_useracc_temp2.txt 2> nul

    for /f "tokens=1,2,3" %%a in ([#####]_useracc_temp2.txt) do (
        net user %%a | findstr /v /i "ECHO.is" | findstr /i "Account.active.*Yes Account.active.*Locked" && echo %%a>> [#####]_active_acc_temp1.txt 2> nul
        net user %%b | findstr /v /i "ECHO.is" | findstr /i "Account.active.*Yes Account.active.*Locked" && echo %%b>> [#####]_active_acc_temp1.txt 2> nul
        net user %%c | findstr /v /i "ECHO.is" | findstr /i "Account.active.*Yes Account.active.*Locked" && echo %%c>> [#####]_active_acc_temp1.txt 2> nul
    ) > nul 2> nul

    type [#####]_active_acc_temp1.txt | findstr . > [#####]_active_acc.txt 2> nul

    for /f "delims=" %%a in ([#####]_active_acc.txt) do (
        echo. Set WshShell = WScript.CreateObject^("WScript.Shell"^) > [#####]_nopass_check.vbs 2> nul
        echo. WshShell.SendKeys ".\tools\psexec -u %%a cmd" >> [#####]_nopass_check.vbs 2> nul
        echo. WScript.Sleep 500 >> [#####]_nopass_check.vbs 2> nul
        echo. WshShell.SendKeys "{ENTER}" >> [#####]_nopass_check.vbs 2> nul
        echo. WScript.Sleep 500 >> [#####]_nopass_check.vbs 2> nul
        echo. WshShell.SendKeys "{ENTER}" >> [#####]_nopass_check.vbs 2> nul
        echo. WScript.Sleep 500 >> [#####]_nopass_check.vbs 2> nul
        echo. WshShell.SendKeys "if {%%}errorlevel{%%}==1327 echo %%a" >> [#####]_nopass_check.vbs 2> nul
        echo. WScript.Sleep 500 >> [#####]_nopass_check.vbs 2> nul
        echo. WshShell.SendKeys "{ENTER}" >> [#####]_nopass_check.vbs 2> nul
        echo. WshShell.SendKeys "echo {%%}errorlevel{%%} %%a" >> [#####]_nopass_check.vbs 2> nul
        echo. WScript.Sleep 500 >> [#####]_nopass_check.vbs 2> nul
        echo. WshShell.SendKeys "{ENTER}" >> [#####]_nopass_check.vbs 2> nul

       cscript [#####]_nopass_check.vbs
    ) > nul 2> nul

I'm looking for a way by using psexec with vbs.

But I think the best way is use runas command.


---------------------------------------------------------------

Edited!
Last edited by Squashman on 17 Jan 2020 08:53, edited 1 time in total.
Reason: MOD EDIT: Please use CODE TAGS.

Post Reply