views:

623

answers:

2

Hi

Is there a way to detect whether IIS is enabled or not?

I know how to check if it is INSTALLED, but I need to know if it's installed but not enabled.

Also, can this be done natively via InstallShield? Checking this via .NET would be acceptable as we can write custom actions, but if there is an IS call then that would be ideal.

Any hints/tips are appreciated, thanks

+1  A: 

To check for service status, use the ubiquitous WMI (the code is VBScript, just to give you the idea of the necessary WMI query):

IISrunning = false
wql        = "SELECT state FROM Win32_Service WHERE name = 'W3SVC'"
Set w3svc  = GetObject("winmgmts://.").ExecQuery(wql)

For Each service in w3svc
  IISrunning = (service.State = "Running")
Next

WScript.Echo IISrunning

EDIT: I try and make an IS script out of this. Don't hit me if there is a syntax error.

function BOOL DetectIIS()
OBJECT wmi, slist, obj;
NUMBER i;
BOOL IISrunning;
begin

  IISrunning = false;
  try
    set wmi = CoGetObject( "winmgmts://.", "" );
    if ( !IsObject(wmi) ) then 
      MessageBox("Failed to connect to WMI.", WARNING);
      return false;
    endif;
    set slist = wmi.ExecQuery("SELECT state FROM Win32_Service WHERE name = 'W3SVC'");
    if ( !IsObject(slist) ) then
      MessageBox("Failed to get query W3SVC service state.", WARNING);
      return false;
    endif;
    for i = 0 to slist.Count-1
      set obj = slist.Item(i);
      IISrunning = (obj.state = "Running");
    endfor;
  catch
    MessageBox(Err.Description, WARNING);
    return false;
  endcatch;

  return IISrunning;
end;

Code borrowed from here and here, because I know zero about the IS scripting language. ;-)

Tomalak
+1  A: 

You should also check to see if the Web Site is started, in addition to the W3svc Service

c:\Inetpub\scripts>adsutil.vbs get W3SVC/1/ServerState
ServerState                     : (INTEGER) 2

Where ServerState =

Value  Meaning  Friendly ID  
1      Starting    MD_SERVER_STATE_STARTING
2      Started     MD_SERVER_STATE_STARTED  <-- What you want
3      Stopping    MD_SERVER_STATE_STOPPING
4      Stopped     MD_SERVER_STATE_STOPPED
5      Pausing     MD_SERVER_STATE_PAUSING
6      Paused      MD_SERVER_STATE_PAUSED
7      Continuing  MD_SERVER_STATE_CONTINUING

So the above answer using Win32_Service will tell you if the service is started or not, this will tell you if the web site is running in addition to telling you if the service is running.

Christopher_G_Lewis