Batch file: run command in a cmd.exe launched as a different user using batch file

假如想象 提交于 2019-12-24 04:31:37

问题


Hi Folks I am a newbie to batch files and I am facing a challenge. I have to do following steps using a batch file automatically. Steps 1 and 3 I am able to figure out, however for step 2 i need your help:

A. Run command prompt using a user:

Comment: This I have figured out, I can do it using:


            @Echo Off               
            runas /profile /savecred /user:xyzasa\asdasq "cmd"

B. Make some registry changes using the command prompt which opened in the previous step

Comment: I know this can be done using the reg add command however when i am putting this command in the batch file it is not getting executed. Here is the current shape of my batch file:

            @echo off
            runas /profile /savecred /user:xyzasa\asdasq "cmd"
            reg add <Machine Name><Registry path and the changes to be made>

C. Launch a rdp file

Comment: This also I can do by simply mentioning the name of the rdp file as the last step.

            @echo off
            runas /profile /savecred /user:xyzasa\asdasq "cmd"
            reg add <Machine Name><Registry path and the changes to be made>
            asd.rdp

Question: How to make sure that after the cmd is launched as the result of the previous step the reg add command executes in the recently launched prompt?


回答1:


The way you have it, the runas command will launch a separate cmd.exe instance running as that user, that you can't interact with in any practical way from the original cmd.exe instance.

I recommend you create two scripts: one that contains only the "runas" command (say, "main.bat"), and another that contains everything you want done under that user account (say, "sub.bat"). Then do something like this in main.bat:

runas /profile /savecred /user:xyzasa\asdasq "cmd /c sub.bat"


来源:https://stackoverflow.com/questions/14998991/batch-file-run-command-in-a-cmd-exe-launched-as-a-different-user-using-batch-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!