2

I have created a windows service that checks a database for errors and if a specific one shows up I want it to perform an IISRESET command.

The problem is, if I run and IISRESET command without the elevated privileges then it won't actually do the reset. So I have my code doing all I want, but I'm not sure if the IISRESET command is being run as an administrator and I don't know how to verify that.

Here is the code I have

        ErrorCheckerEventLog.WriteEntry("Performing IISReset", EventLogEntryType.Warning);
        Process process = new Process();
        process.StartInfo.Verb = "runas";
        process.StartInfo.FileName = "iisreset.exe";
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.CreateNoWindow = false;
        process.StartInfo.RedirectStandardError = false;
        process.StartInfo.RedirectStandardOutput = false;
        process.Start();
        process.WaitForExit();
        ErrorCheckerEventLog.WriteEntry("IISReset finished", EventLogEntryType.Information);

In the Application event log I get these:

Listener Adapter protocol 'net.tcp' successfully connected to Windows Process Activation Service.

Listener Adapter protocol 'net.pipe' successfully connected to Windows Process Activation Service.

In the system event log I get these:

IIS start command received from user testing\neil.kenny. The logged data is the status code.

It all looks good to me, but I'm still not sure it actually did the reset. It could have just ran the iisreset command which then output the access denied message.

How do I properly verify this?

One Man Crew
  • 9,420
  • 2
  • 42
  • 51
Neil
  • 2,659
  • 7
  • 35
  • 57

2 Answers2

3

If you want to test it manually:

  • First thing come to my mind is keep the browser open with any site on the IIS being reset. Press F5 all time. Site will go down and soon will be up again.
  • Check in event viewer and look for event entries indicating IIS has been restarted (Check System logs). This question might help. here you have a pic. One image is better than thousand words:

enter image description here

What to look for exactly:

  • Several events indicating that some services has been stopped. Source is Service Control Manager. Among them look for the one saying "The World Wide Web Publishing Service entered stopped state"
  • Event from source IIS-IISReset with content "IIS stop command received from user xxxx"
  • Several events indicating that some services has been started. Source is Service Control Manager. Among them look for the one saying "The World Wide Web Publishing Service entered running state"
  • Event from source IIS-IISReset with content "IIS start command received from user xxxx"
Community
  • 1
  • 1
Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
  • Ah, so if that IIS-IISReset appears then the reset successfully occurred? Or does it just mean that it was attempted – Neil Apr 03 '13 at 08:46
  • I have edited question to indicate what you should look for. If you read carefully the events you will see clearly if it has been restarted or not. If not sure, make yourself a IISReset from your elevated cmd and check your logs. – Oscar Foley Apr 03 '13 at 08:55
  • 1
    Thanks, exactly what I wanted, it was successfully doing a reset :) – Neil Apr 03 '13 at 09:06
  • Cool. Some karma for me today :) – Oscar Foley Apr 03 '13 at 11:22
0

Just a thought ... depending on your requirements you could do something like ...

1. Call up a web page or wcf endpoint that lives on a site hosted on the iis instance that you wrote to put something in the application state.

2. Run your code.

3. Call it again with params telling it to retrieve the application variable, if its null and takes a while chances are that app got reloaded due to an iisreset.

Not ideal if your code has nothing to do with the apps hosted on the box so then I figured ... why not just retrieve that event log information?

There's an example of that on this: Read event log in C# ... question, admittedly a very simple one but you're looking in a specific time period that was very recent and logs read from the most recent down, thus rendering your search very quick (e.g. should be in the top say 100 events on the busiest of servers).

Community
  • 1
  • 1
War
  • 8,539
  • 4
  • 46
  • 98