Unable to obtain a list of IIS websites to allow user selection of setup location for use in an Inno Script setup, please help

旧街凉风 提交于 2020-01-15 10:59:28

问题


I am currently attempting to create an Inno script installer which requests a list of "Web sites" from a user's IIS installation so that the user can select the appropriate website from a combo box list and this list can be used to create a virtual directory in the correct Website location.

I need to Generate a list of IIS websites e.g. "Default Web Site" populating a combo box

So far I have only been able to achieve installing the virtual directory to a location based on a hard-coded combobox selection with the following code.

[Run]
Filename: {sys}\iisvdir.vbs; Parameters: "/create ""{code:GetWebSite}"" MyApp ""{app}\Website"""; Flags: skipifdoesntexist waituntilterminated shellexec; StatusMsg: Creating IIS Virtual Directory

[Code]
var
  WebsitePage: TWizardPage;
  ComboBox: TNewComboBox;

procedure InitializeWizard;
begin
  WebsitePage := CreateCustomPage(wpSelectComponents, 'Select which website you wish to install to',
'Which website should I install to?');
  ComboBox := TNewComboBox.Create(WebsitePage);
  ComboBox.Width := WebsitePage.SurfaceWidth;
  ComboBox.Parent := WebsitePage.Surface;
  ComboBox.Style := csDropDownList;
  ComboBox.Items.Add('Default Web Site');
  ComboBox.Items.Add('Website 1');
  ComboBox.ItemIndex := 0;
end;

function GetWebSite(Param: String): String;
begin
  { Return the selected Website }
  Result  := ComboBox.Text;
end;

All I need to do now is dynamically set the items from the available Websites that the user has in IIS...

Thanks for any help!


回答1:


Actually I "solved" this yesterday, still haven't found anything else on this subject so I guess we are pioneers ;). I started a long the lines you did but I could not find any useful documentation and so went down another path. Although my solution works it is very messy.

Basically I run a VB script that outputs the list of web sites to a text file and then read that text file back into Inno setup. Below is my current code, which is very rough, I plan to tidy it up and add appropriate error handling later.

Website.vbs

OPTION EXPLICIT

DIM CRLF, TAB
DIM strServer
DIM objWebService
strServer = "localhost"

CRLF = CHR( 13 ) & CHR( 10 )

' WScript.Echo "Enumerating websites on " & strServer & CRLF
SET objWebService = GetObject( "IIS://" & strServer & "/W3SVC" )
EnumWebsites objWebService

SUB EnumWebsites( objWebService)
    DIM objWebServer, objWebServerRoot, strBindings

    Dim objFSO, objFolder, objShell, objTextFile, objFile
    Dim strDirectory, strFile, strText

    strFile = "website.txt"

    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If objFSO.FileExists(strFile) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFile = objFSO.CreateTextFile(strFile)
       ' Wscript.Echo "Just created " & strDirectory & strFile
    End If 

    set objFile = nothing
    set objFolder = nothing

    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const ForAppending = 8

    Set objTextFile = objFSO.OpenTextFile _
    (strFile, ForAppending, True)

    FOR EACH objWebServer IN objWebService
        IF objWebserver.Class = "IIsWebServer" THEN

            SET objWebServerRoot = GetObject(objWebServer.adspath & "/root")

            ' Writes strText every time you run this VBScript
            objTextFile.WriteLine(objWebServer.ServerComment)

        END IF
    NEXT

    objTextFile.Close
END SUB

Innosetup script

[Code]
var

  WebsitePage: TWizardPage;
  ComboBox: TNewComboBox;
  WebSite: Variant;
  WebServer: Variant;
  WebRoot: Variant; 
  ErrorCode: Integer;
  ResultCode: Integer;
  Sites: AnsiString;

procedure InitializeWizard;
begin

  ExtractTemporaryFile('Website.vbs');
  if not ShellExec('', ExpandConstant('{tmp}\Website.vbs'),     '', '', SW_SHOW, ewWaitUntilTerminated, ErrorCode) then
    begin
      MsgBox('Oh no!:' #13#13 'The file could not be executed. ' + SysErrorMessage(ResultCode) + '.', mbError, MB_OK);
    end;

  if LoadStringFromFile(ExpandConstant('{tmp}\website.txt'), Sites) then
  begin
    //MsgBox(Sites, mbInformation, MB_OK);
  end else begin
    Exit; 
  end;

WebsitePage := CreateCustomPage(DataDirPage.ID, 'Select which website you wish to install to',
'Which website should the application be install to?');
  ComboBox := TNewComboBox.Create(WebsitePage);
  ComboBox.Width := WebsitePage.SurfaceWidth;
  ComboBox.Parent := WebsitePage.Surface;
  ComboBox.Style := csDropDownList;
  ComboBox.Items.Text := Sites;
  ComboBox.ItemIndex := 0;
end;



回答2:


Good news!! I found the hidden thing we've both been looking for, and you don't need a separate vb project to fix it.

Heres my code again:

[Code]
var
  WebsitePage: TWizardPage;
  ComboBox: TNewComboBox;
  IIS, WebSite, WebServer: Variant;
  IISServerIndex: Integer;

procedure InitializeWizard;
begin
  WebsitePage := CreateCustomPage(wpSelectComponents, 'Select which website you wish to install to',
'Which website should I install to?');
  ComboBox.Width := WebsitePage.SurfaceWidth;
  ComboBox.Parent := WebsitePage.Surface;
  ComboBox.Style := csDropDownList;

// ------------------------------------------------------------------------------

    IIS := CreateOleObject('IISNamespace');
WebServer := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');

IISServerIndex := 1;
try
    while True do
    begin
        WebSite := WebServer.GetObject('IIsWebServer', IISServerIndex);
        ComboBox.Items.Add(WebSite.ServerComment);
        IISServerIndex := IISServerIndex + 1;
    end;
except
end;

The answer was changing the ComboBox.Items.Add line to .ServerComment, not .Name.

Enjoy :)

Stu




回答3:


I'm a bit further on with your problem, but I still haven't cracked it either! I can get the number it appears in the directory, but not the name itself.

Heres what I have so far. If you manage to take it further, please let me know what you did :)

[Code]
var
  WebsitePage: TWizardPage;
  ComboBox: TNewComboBox;
  IIS, WebSite, WebServer: Variant;
  IISServerIndex: Integer;

procedure InitializeWizard;
begin
  WebsitePage := CreateCustomPage(wpSelectComponents, 'Select which website you wish to install to',
'Which website should I install to?');
  ComboBox.Width := WebsitePage.SurfaceWidth;
  ComboBox.Parent := WebsitePage.Surface;
  ComboBox.Style := csDropDownList;

// ------------------------------------------------------------------------------

    IIS := CreateOleObject('IISNamespace');
    WebSite := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');
    WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');

    IISServerIndex := 1;
    try
        while True do
        begin
            WebServer := WebSite.GetObject('IIsWebServer', IISServerIndex);
            OneClickSiteComboBox.Items.Add(WebServer.Name);
            IISServerIndex := IISServerIndex + 1;
        end;
    except
    end;

Sorry if a combobox or two is named wrong. I copied it from my source and tried to match it to your names. It basically connects to IIS and lists the webserver names. For some reason they come out as ID's though :(

I also didn't end up needing a second function (your getwebsite)

MSDN mentions Name can be overridden to display keys but doesn't elaborate and I'm going crazy trying to find out where it could be getting overridden if thats is the error. http://msdn.microsoft.com/en-us/library/ms525545%28VS.90%29.aspx



来源:https://stackoverflow.com/questions/5248834/unable-to-obtain-a-list-of-iis-websites-to-allow-user-selection-of-setup-locatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!