Note that there are some explanatory texts on larger screens.

plurals
  1. POappcmd.exe and dnscmd.exe behaving different when returning StandardOutput
    primarykey
    data
    text
    <p>I'am developing something which should work like a hosting panel for a self deploying application. I created a method which has file name and arguments as parameters and should give the output on the panel web page when executed.</p> <p>Here is my method;</p> <pre><code>private string ExecuteCmd(string sysUser, SecureString secureString, string argument, string fileName) { using (Process p = new Process()) { p.StartInfo.FileName = fileName; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UserName = sysUser; p.StartInfo.Password = secureString; p.StartInfo.Arguments = argument; p.Start(); p.WaitForExit(); StreamReader sr = p.StandardOutput; p.Close(); string message = sr.ReadToEnd(); return message; } } </code></pre> <p>When I use this method to create sites on IIS (with appcmd.exe) I get all the output as I execute this executable on command promt. But when it comes to dnscmd.exe to create entries on DNS, I get nothing! StandardOutput just comes out empty. I use administrator credentials to execute these executables. By the way, I am on Windows Server 2012 Standart. I didn't test this method on Server 2008 R2 yet, but I believe the result would be the same, anyway.</p> <p>It's kind of strange for me to see appcmd and dnscmd executables behave differently on the same method.</p> <p>What is it I am missing here?</p> <p>Thanks!</p> <p>Edit: Both StandardOutput and StandardError are returning error for dnscmd.exe.</p> <p>Edit2: I changed ReadLine() to ReadToEnd(). That was something I changed when I was playing around, trying things. The original code had ReadToEnd().</p> <p>Edit3: Full methods with filepath and arguments. That is for IIS, which shows output with no problems;</p> <pre><code>private string ExecuteAppCmd(string sysUser, SecureString secureString) { using (Process p = new Process()) { p.StartInfo.FileName = @"C:\Windows\System32\inetsrv\APPCMD.EXE"; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.UserName = sysUser; p.StartInfo.Password = secureString; p.StartInfo.Arguments = " list site domain.com"; p.Start(); p.WaitForExit(); StreamReader sr = p.StandardOutput; p.Close(); string message = sr.ReadToEnd().Replace("\n", "&lt;br /&gt;"); return message; } } </code></pre> <p>"appcmd list site domain.com" shows the iis site configuration for domain.com on the command promt. If the domain.com is not in iis, it shows an error. Either way, there is an output and it works fine with this code.</p> <p>And this is for dnscmd. This one does the job on asp.net page, but does not show it's output with StandardOutput. However, the output is shown on command prompt.</p> <pre><code>private string ExecuteDnsCmd(string sysUser, SecureString secureString) { using (Process p = new Process()) { p.StartInfo.FileName = @"C:\Windows\System32\DNSCMD.EXE"; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.UserName = sysUser; p.StartInfo.Password = secureString; p.StartInfo.Arguments = " /zoneadd domain.com /primary"; p.Start(); p.WaitForExit(); StreamReader sr = p.StandardError; p.Close(); string message = sr.ReadToEnd().Replace("\n", "&lt;br /&gt;"); return message; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload