How can I list websites on IIS7, from script, without using IIS6 compat pack (WMI veneer)

On IIS6, I can use WMI to list available websites, like this:

    var iis = GetObject("winmgmts://localhost/root/MicrosoftIISv2");
    var query  = "SELECT * FROM IIsWebServerSetting"

    // get the list of virtual servers
    var results = iis.ExecQuery(query);

    for(var e = new Enumerator(results); !e.atEnd(); e.moveNext()) {
        var site = e.item();
        // site.Name                   // W3SVC/1, W3SVC/12378398, etc
        // site.Name.substr(6)         // 1, 12378398, etc
        // site.ServerComment)         // "Default Web Site", "Site2", etc
        // site.ServerBindings(0).Port // 80, 8080, etc
    }

I know I can run this script on IIS7, if I have previously installed the IIS6 Compatibility Pack.

Is it possible to get the list of WebSites without requiring the compatibility pack as a pre-requisite?

I know I can run AppCmd to do this from the command line:

\Windows\system32\inetsrv\appcmd list sites

But... can I run that from a custom action in an MSI? And... if not, how can I do the equivalent thing (list websites on IIS7) from javascript?


EDIT
Here's how I tried running the command from within Javascript.

function GetWebSites_IIS7()
{
    var ParseOneLine = function(oneLine) {
        ...a bunch of regex parsing here....
    };

    LogMessage("GetWebSites_IIS7() ENTER");
    var shell = new ActiveXObject("WScript.Shell");
    var windir = shell.Environment("system")("windir");
    // aka Session.Property("%WINDIR%")

    var appcmd = windir + "\\system32\\inetsrv\\appcmd.exe list sites";
    var oExec = shell.Exec(appcmd);

    var sites = [];
    while (!oExec.StdOut.AtEndOfStream) {
        var oneLine = oExec.StdOut.ReadLine();
        var line = ParseOneLine(oneLine);
        LogMessage("  site: " + line.name);
        sites.push(line);
    }

    return sites;
}

This works, but it briefly pops a visible console window, which then disappears. Doesn't look very polished. I think I can avoid the console window by using shell.Run() instead of shell.Exec(). But shell.Run() doesn't give access to the stdout, so I would have to redirect the output to a temporary file, then read the output. I haven't tried that yet. That may introduce some security issues; I'll have to see.


Related:
Where and how should my CustomAction create and read a temporary file?

Answers:

Answer

Yes, you can run appcmd from the custom action the same way you do any custom action which runs exe. First off, you should author a DirectorySearch/FileSearch elements to find the full path to the executable. Next, add a custom action with ExeCommand attribute. You're probably trying to get feedback from a user, so leave it immediate. Also, think about using QuietExec in order not to show console window to your users.

By the way, if my guess is correct, you're trying to do something like this. Hope this helps.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.