How to run in C# CMD with admin rights with password and then save all output to a string?











up vote
0
down vote

favorite












I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.



Here is my code:



        // admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";

//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;

// save all output data to string
p.Start();


Why second argument not working to run C:Info.bat?



How can I save all cmd output text to a string?



Thanks for help.










share|improve this question
























  • what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
    – ADyson
    Nov 19 at 11:58






  • 1




    try this and let me know => startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
    – er-shoaib
    Nov 19 at 12:03










  • Thanks @er-shoaib that run C:Info.bat.
    – Luka Toni
    Nov 19 at 12:13










  • should i add above code as answer to your question?
    – er-shoaib
    Nov 19 at 12:27






  • 1




    @LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
    – er-shoaib
    Nov 19 at 13:57















up vote
0
down vote

favorite












I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.



Here is my code:



        // admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";

//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;

// save all output data to string
p.Start();


Why second argument not working to run C:Info.bat?



How can I save all cmd output text to a string?



Thanks for help.










share|improve this question
























  • what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
    – ADyson
    Nov 19 at 11:58






  • 1




    try this and let me know => startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
    – er-shoaib
    Nov 19 at 12:03










  • Thanks @er-shoaib that run C:Info.bat.
    – Luka Toni
    Nov 19 at 12:13










  • should i add above code as answer to your question?
    – er-shoaib
    Nov 19 at 12:27






  • 1




    @LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
    – er-shoaib
    Nov 19 at 13:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.



Here is my code:



        // admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";

//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;

// save all output data to string
p.Start();


Why second argument not working to run C:Info.bat?



How can I save all cmd output text to a string?



Thanks for help.










share|improve this question















I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.



Here is my code:



        // admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";

//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;

// save all output data to string
p.Start();


Why second argument not working to run C:Info.bat?



How can I save all cmd output text to a string?



Thanks for help.







c# batch-file cmd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 13:09

























asked Nov 19 at 11:40









Luka Toni

145313




145313












  • what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
    – ADyson
    Nov 19 at 11:58






  • 1




    try this and let me know => startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
    – er-shoaib
    Nov 19 at 12:03










  • Thanks @er-shoaib that run C:Info.bat.
    – Luka Toni
    Nov 19 at 12:13










  • should i add above code as answer to your question?
    – er-shoaib
    Nov 19 at 12:27






  • 1




    @LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
    – er-shoaib
    Nov 19 at 13:57


















  • what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
    – ADyson
    Nov 19 at 11:58






  • 1




    try this and let me know => startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
    – er-shoaib
    Nov 19 at 12:03










  • Thanks @er-shoaib that run C:Info.bat.
    – Luka Toni
    Nov 19 at 12:13










  • should i add above code as answer to your question?
    – er-shoaib
    Nov 19 at 12:27






  • 1




    @LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
    – er-shoaib
    Nov 19 at 13:57
















what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58




what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58




1




1




try this and let me know => startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03




try this and let me know => startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03












Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13




Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13












should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27




should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27




1




1




@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57




@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You need to modify your process argument like below



startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";


/K argument, which tells CMD.exe to open, run the specified command, then keep the window open.



You may also use.



/C argument, which tells CMD.exe to open, run the specified command, then close when it's done.



Edit:



Here you can read output of your info.bat file in string variable.



var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";

startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
p.StartInfo = startInfo;

p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373872%2fhow-to-run-in-c-sharp-cmd-with-admin-rights-with-password-and-then-save-all-outp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You need to modify your process argument like below



    startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";


    /K argument, which tells CMD.exe to open, run the specified command, then keep the window open.



    You may also use.



    /C argument, which tells CMD.exe to open, run the specified command, then close when it's done.



    Edit:



    Here you can read output of your info.bat file in string variable.



    var pass = new SecureString();
    pass.AppendChar('p');
    pass.AppendChar('a');
    pass.AppendChar('s');
    pass.AppendChar('s');

    Process p = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
    startInfo.Verb = "runas";

    startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
    startInfo.Password = pass;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    p.StartInfo = startInfo;

    p.Start();

    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      You need to modify your process argument like below



      startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";


      /K argument, which tells CMD.exe to open, run the specified command, then keep the window open.



      You may also use.



      /C argument, which tells CMD.exe to open, run the specified command, then close when it's done.



      Edit:



      Here you can read output of your info.bat file in string variable.



      var pass = new SecureString();
      pass.AppendChar('p');
      pass.AppendChar('a');
      pass.AppendChar('s');
      pass.AppendChar('s');

      Process p = new Process();
      ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
      startInfo.Verb = "runas";

      startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
      startInfo.Password = pass;
      startInfo.UseShellExecute = false;
      startInfo.RedirectStandardOutput = true;
      p.StartInfo = startInfo;

      p.Start();

      string output = p.StandardOutput.ReadToEnd();
      p.WaitForExit();





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You need to modify your process argument like below



        startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";


        /K argument, which tells CMD.exe to open, run the specified command, then keep the window open.



        You may also use.



        /C argument, which tells CMD.exe to open, run the specified command, then close when it's done.



        Edit:



        Here you can read output of your info.bat file in string variable.



        var pass = new SecureString();
        pass.AppendChar('p');
        pass.AppendChar('a');
        pass.AppendChar('s');
        pass.AppendChar('s');

        Process p = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
        startInfo.Verb = "runas";

        startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
        startInfo.Password = pass;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        p.StartInfo = startInfo;

        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();





        share|improve this answer














        You need to modify your process argument like below



        startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";


        /K argument, which tells CMD.exe to open, run the specified command, then keep the window open.



        You may also use.



        /C argument, which tells CMD.exe to open, run the specified command, then close when it's done.



        Edit:



        Here you can read output of your info.bat file in string variable.



        var pass = new SecureString();
        pass.AppendChar('p');
        pass.AppendChar('a');
        pass.AppendChar('s');
        pass.AppendChar('s');

        Process p = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
        startInfo.Verb = "runas";

        startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
        startInfo.Password = pass;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        p.StartInfo = startInfo;

        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 5:39

























        answered Nov 19 at 12:35









        er-shoaib

        4,3092516




        4,3092516






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373872%2fhow-to-run-in-c-sharp-cmd-with-admin-rights-with-password-and-then-save-all-outp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?