0

I'm currently working on an application that checks whether a APP with a certain name exist on an IIS server. I use PsExec to execute this command.

While I was testing this through the command line, I noticed that when an APP does not exist the appcmd exits with ExitCode 1. Likewise it exits with 0 when an APP does exist.

I wanted to use this behavior too so I don't need to do output redirection. I only care about whether the APP exists or not. PsExec uses the exitcode of the command it calls as its own exitcode. I tried this with the command line and checked the result with echo %errorlevel% and it works just fine.

But I run into a problem with the following code.

  Process process = new Process();
  process.StartInfo.FileName = @"psexec";

  string appcmd = @"C:\windows\system32\inetsrv\appcmd";
  process.StartInfo.Arguments = String.Format(@"{0} -u {1} -p {2} -S {3} LIST APP ""{4}""",
                                              ip, username, password, appcmd, appname);

  process.Start();
  process.WaitForExit();
  Console.WriteLine(process.ExitCode);

This executes just fine. But it does not return the exitcode that PsExec (should?) gives. It always returns 0. So now I can't use the trick I found earlier to check whether an APP exists or not.

I there a known solution for this? Is there a similar solution? Or should I go with output redirection?

Chrono
  • 1,433
  • 1
  • 16
  • 33

1 Answers1

0

Not really a solution to the problem. But because web applications in IIS must have a unique name, creating an application that already exists will result in an error but nothing will have changed.

This means that it works perfectly fine if I always try to create a new APP. If it doesn't exist it will be made, if it already exists nothing will happen.

Chrono
  • 1,433
  • 1
  • 16
  • 33