2012-08-06 185 views
8

我一直在试图弄清楚如何使用Inno安装脚本安装和注册IIS,但我迄今并未成功。Inno设置IIS安装和配置

我需要创建一个应用程序,具有.Net版本4和虚拟目录的应用程序池,获取机器的IP并继续使用此IP编辑网站的绑定。

到目前为止,我安装的所有工作都在检查IIS是否安装。

如果有人曾经做过这样的事情,我会非常感谢,如果你能分享你的脚本。

谢谢。

+0

如果你看看与INNO安装样品,codeautomation.iss与IIS – 2012-08-06 14:33:28

+0

交互的一些例子你知道如何(programitcally)的Inno之外?作为@AlexK。说,Inno有一个配置IIS的例子,但是你需要扩展它来完成剩下的工作。 – Deanna 2012-08-06 15:29:11

+0

@Deanna,我不知道如何在Inno安装程序之外以编程方式执行此过程。你有这方面的经验吗? – Feytality 2012-08-06 15:39:34

回答

3

那么,到底是什么我所做的就是创建一个单独的可执行文件在C#编码,将通过命令行安装之前安装IIS,然后将注册IIS与适当的应用程序池等安装后。然后我通过PrepareToInstall中的Inno Setup调用可执行文件来安装IIS,并在CurStepChanged中注册IIS。

0

我们也在使用IIS并考虑自动安装IIS。据我所知可以通过命令行工具来完成所有的工作。因此,您可以创建自己的Inno自定义页面以从用户检索必要的数据。 (您可以使用Inno Setup Form Designer进行此操作)。一旦你收集了所有的东西,你可以创建并填充你准备好的模板批处理文件,并且在安装后将会执行批处理。

我会尝试这样的:

希望这有助于

+0

这是非常有用的,因为它证实了我最近刚刚尝试,所以非常感谢。当你说接受用户的一些数据时,我只是有点困惑。我需要获得什么样的信息? – Feytality 2012-08-07 14:41:40

+0

现在我正在使用这个问题的答案中找到的脚本http://stackoverflow.com/questions/3245199/install-iis-from-c-sharp-code – Feytality 2012-08-07 14:50:43

5

这是我用Inno Setup处理IIS(Internet信息服务)的完整代码。 它包含:

.NET安装的
  • 框架4
  • 安装IIS的/卸载+ ASP.NET 4
  • 注册和删除网络站点的/应用
  • 助手来获得用户帐户一个应用程序池是在
  • 助手运行去一个网站

它已与下列Windows editio测试物理路径ns: XP Pro SP3 x86,2003 R2 x86,2003 R2 x64,Vista Ultimate x64,7家庭高级版x64,2008 R2 x64,2011 x64,8.1 Pro x86,10 Pro x64

所有工作都尽可能被动不会破坏任何现有的网站。

重要

这可能需要的Inno-设置的Unicode的版本(我使用的是5.5.6(U))。 它也需要强制在x64机器上的64位模式(否则注册表和文件访问重定向到32位的东西):

[Setup] 
ArchitecturesInstallIn64BitMode=x64 

要求助手

#ifdef UNICODE 
#define AW "W" 
#else 
#define AW "A" 
#endif 

const 
    // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx 
    ERROR_SUCCESS = 0; 
    ERROR_INVALID_FUNCTION = 1; 
    ERROR_NOT_SUPPORTED = 50; 
    ERROR_NOT_FOUND = 1168; 
    ERROR_SUCCESS_REBOOT_REQUIRED = 3010; 

function ExpandEnvironmentStrings(lpSrc: String; lpDst: String; nSize: DWORD): DWORD; 
    external 'ExpandEnvironmentStrings{#AW}@kernel32.dll stdcall'; 
function ExpandEnvVars(const Input: String): String; 
var 
    Buf: String; 
    BufSize: DWORD; 
begin 
    BufSize := ExpandEnvironmentStrings(Input, #0, 0); 
    if BufSize > 0 then 
    begin 
    SetLength(Buf, BufSize); 
    if ExpandEnvironmentStrings(Input, Buf, BufSize) = 0 then 
     RaiseException(Format('Expanding env. strings failed. %s', [SysErrorMessage(DLLGetLastError)])); 
#if AW == "A" 
    Result := Copy(Buf, 1, BufSize - 2); 
#else 
    Result := Copy(Buf, 1, BufSize - 1); 
#endif 
    end 
    else 
    RaiseException(Format('Expanding env. strings failed. %s', [SysErrorMessage(DLLGetLastError)])); 
end; 

// Exec with output stored in result. ResultString will only be altered if True is returned. 
function ExecWithResult(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer; var ResultString: String): Boolean; 
var 
    TempFilename: String; 
    TempAnsiStr: AnsiString; 
    Command: String; 
begin 
    TempFilename := ExpandConstant('{tmp}\~execwithresult.txt'); 
    // Exec via cmd and redirect output to file. Must use special string-behavior to work. 
    Command := Format('"%s" /S /C ""%s" %s > "%s""', [ExpandConstant('{cmd}'), Filename, Params, TempFilename]); 
    Result := Exec(ExpandConstant('{cmd}'), Command, WorkingDir, ShowCmd, Wait, ResultCode); 
    if not Result then 
    Exit; 
    LoadStringFromFile(TempFilename, TempAnsiStr); // Cannot fail 
    ResultString := String(TempAnsiStr); 
    DeleteFile(TempFilename); 
    // Remove new-line at the end 
    if (Length(ResultString) >= 2) and (ResultString[Length(ResultString) - 1] = #13) and (ResultString[Length(ResultString)] = #10) then 
    Delete(ResultString, Length(ResultString) - 1, 2); 
end; 

function IIs7ExecAppCmd(Params: String; var ResultString: String; var ResultCode: Integer): Boolean; 
var 
    AppCmdFilePath: String; 
    Command: String; 
begin 
    AppCmdFilePath := ExpandConstant('{sys}\inetsrv\appcmd.exe'); 
    Result := ExecWithResult(AppCmdFilePath, Params, '', SW_HIDE, ewWaitUntilTerminated, ResultCode, ResultString); 
end; 

.NET 4的安装所需的IIS安装

将安装.NET 4.0为Win XP/2003。 .NET 4.6 for Vista/2008及更高版本。 如果.NET 4已包含在系统中,则不会执行任何操作(Win 8/2012/10)。 下列文件是必需的,必须被包括在该设置:

-

[Files] 
Source: "dotNetFx40_Full_setup.exe"; DestDir: "{tmp}"; Flags: ignoreversion dontcopy 
Source: "NDP46-KB3045560-Web.exe"; DestDir: "{tmp}"; Flags: ignoreversion dontcopy 
Source: "wic_x64_enu.exe"; DestDir: "{tmp}"; Flags: ignoreversion dontcopy 
Source: "wic_x86_enu.exe"; DestDir: "{tmp}"; Flags: ignoreversion dontcopy 

由于我使用Web安装程序,因此在安装过程中可能需要连接到Internet。

// Checks if .NET 4 "Full" is installed 
function IsDotNet4FullInstalled(): Boolean; 
var 
    Success: Boolean; 
    Install: Cardinal; 
begin 
    try 
    ExpandConstant('{dotnet40}'); // This will throw an exception if .NET 4 is not installed at all 
    Success := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full', 'Install', Install); // Check for "Full" version 
    Result := Success and (Install = 1); 
    except 
    Result := False; 
    end; 
end; 

// Returns True if a restart is required. 
function InstallDotNet4(): Boolean; 
var 
    Version: TWindowsVersion; 
    ResultCode: Integer; 
    Success: Boolean; 
begin 
    GetWindowsVersionEx(Version); 
    if (Version.Major <= 5) then // 4.0 for XP, 2003 
    ExtractTemporaryFile('dotNetFx40_Full_setup.exe') 
    else // 4.6 
    ExtractTemporaryFile('NDP46-KB3045560-Web.exe'); 
    if (Version.Major <= 5) then // XP, 2003: Install .NET 4.0 
    begin 
    Success := Exec(ExpandConstant('{tmp}\dotNetFx40_Full_setup.exe'), '/passive', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
    if Success and (ResultCode = 5100) then // Indicates that "Windows Imaging Component" is missing (probably 2003) 
    begin 
     if IsWin64 then 
     begin 
     ExtractTemporaryFile('wic_x64_enu.exe'); 
     if not Exec(ExpandConstant('{tmp}\wic_x64_enu.exe'), '/passive', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) or (ResultCode <> ERROR_SUCCESS) then 
      RaiseException('Failed to install "Windows Imaging Component": ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
     // Retry .NET 
     Success := Exec(ExpandConstant('{tmp}\dotNetFx40_Full_setup.exe'), '/passive', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
     end 
     else 
     begin 
     ExtractTemporaryFile('wic_x86_enu.exe'); 
     if not Exec(ExpandConstant('{tmp}\wic_x86_enu.exe'), '/passive', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) or (ResultCode <> ERROR_SUCCESS) then 
      RaiseException('Failed to install "Windows Imaging Component": ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
     // Retry .NET 
     Success := Exec(ExpandConstant('{tmp}\dotNetFx40_Full_setup.exe'), '/passive', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
     end; 
    end 
    end 
    else // Vista/2008 or later: Install .NET 4.6 
    Success := Exec(ExpandConstant('{tmp}\NDP46-KB3045560-Web.exe'), '/passive', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
    // Check for errors 
    if not Success or ((ResultCode <> ERROR_SUCCESS) and (ResultCode <> ERROR_SUCCESS_REBOOT_REQUIRED)) then 
    RaiseException('Failed to install .NET: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    Result := ResultCode = ERROR_SUCCESS_REBOOT_REQUIRED; 
end; 

IIS安装/卸载

与ASP.NET 4已安装IIS将激活相同的(默认 - )特性,如果它是通过GUI激活。 安装完成后,ASP.NET将(重新)向IIS注册。

注意:对于Win XP/2003,需要“Windows安装CD”。

// Returns True if a restart is required. Throws exceptions. 
function InstallIIs(): Boolean; 
var 
    Version: TWindowsVersion; 
    Success: Boolean; 
    ResultCode: Integer; 
    IIS56IniFile: String; 
begin 
    GetWindowsVersionEx(Version); 
    if (Version.Major <= 5) then // XP/2003: Use "sysocmgr" 
    begin 
    IIS56IniFile := ExpandConstant('{tmp}\~iis56.ini'); 
    SaveStringToFile(IIS56IniFile, '[Components]' + #13#10 + 'iis_common = ON' + #13#10 + 'iis_www = ON' + #13#10 + 'iis_inetmgr = ON', False); 
    Success := Exec('sysocmgr', ExpandConstant('/i:"{win}\inf\sysoc.inf" /u:"' + IIS56IniFile + '"'), '', SW_SHOW, ewWaitUntilTerminated, ResultCode); 
    DeleteFile(IIS56IniFile); 
    end 
    else if (Version.Major = 6) and (Version.Minor = 0) then // Vista/2008: Use "pkgmgr" 
    begin 
    Success := Exec('pkgmgr', '/iu:' + 
     // Enables everything a fresh install would also (implicitly) enable. 
     // This is important in case IIS was already installed and some features manually disabled. 
     'WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI' + 
     ';IIS-WebServerRole' + 
     ';IIS-WebServerManagementTools;IIS-ManagementConsole' + 
     ';IIS-WebServer' + 
     ';IIS-ApplicationDevelopment;IIS-NetFxExtensibility;IIS-ASPNET;IIS-ISAPIExtensions;IIS-ISAPIFilter' + 
     ';IIS-CommonHttpFeatures;IIS-HttpErrors;IIS-DefaultDocument;IIS-StaticContent;IIS-DirectoryBrowsing' + 
     ';IIS-Performance;IIS-HttpCompressionStatic' + 
     ';IIS-Security;IIS-RequestFiltering' + 
     ';IIS-HealthAndDiagnostics;IIS-RequestMonitor;IIS-HttpLogging', 
     '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
    end 
    else if (Version.Major = 6) and (Version.Minor = 1) then // 7/2008 R2/2011: Use "Dism" 
    begin 
    Success := Exec('Dism', '/Online /Enable-Feature' + 
     // Enables everything a fresh install would also (implicitly) enable. 
     // This is important in case IIS was already installed and some features manually disabled. 
     // "Parent fetaures" are NOT automatically enabled by "Dism". 
     ' /FeatureName:WAS-WindowsActivationService /FeatureName:WAS-ProcessModel /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ConfigurationAPI' + 
     ' /FeatureName:IIS-WebServerRole' + 
     ' /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-ManagementConsole' + 
     ' /FeatureName:IIS-WebServer' + 
     ' /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ASPNET /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter' + 
     ' /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-HttpErrors /FeatureName:IIS-DefaultDocument /FeatureName:IIS-StaticContent /FeatureName:IIS-DirectoryBrowsing' + 
     ' /FeatureName:IIS-Performance /FeatureName:IIS-HttpCompressionStatic' + 
     ' /FeatureName:IIS-Security /FeatureName:IIS-RequestFiltering' + 
     ' /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-RequestMonitor /FeatureName:IIS-HttpLogging', 
     '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
    end 
    else // 8/2012 and later: Use "Dism" 
    begin 
    Success := Exec('Dism', '/Online /Enable-Feature' + 
     // Enables everything a fresh install would also (implicitly) enable. 
     // This is important in case IIS was already installed and some features manually disabled. 
     ' /FeatureName:WAS-WindowsActivationService /FeatureName:WAS-ProcessModel /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ConfigurationAPI' + 
     ' /FeatureName:IIS-ManagementConsole' + 
     ' /FeatureName:IIS-HttpErrors /FeatureName:IIS-DefaultDocument /FeatureName:IIS-StaticContent /FeatureName:IIS-DirectoryBrowsing' + 
     ' /FeatureName:IIS-ASPNET45' + 
     ' /FeatureName:IIS-HttpCompressionStatic' + 
     ' /FeatureName:IIS-RequestFiltering' + 
     ' /FeatureName:IIS-HttpLogging' + 
     ' /All', // Implicitly enables dependent features 
     '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
    end; 
    if not Success or ((ResultCode <> ERROR_SUCCESS) and (ResultCode <> ERROR_SUCCESS_REBOOT_REQUIRED)) then 
    RaiseException('Cannot install IIS: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    Result := ResultCode = ERROR_SUCCESS_REBOOT_REQUIRED; 
    // Register ASP.NET 4 with IIS. This is required since .NET 4 was probably installed before IIS. 
    // This will NOT change existing web-sites (which might be using other ASP.NET versions already) 
    if not Exec(ExpandConstant('{dotnet40}') + '\aspnet_regiis.exe', '-iru -enable', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) or 
    (ResultCode <> ERROR_SUCCESS) then 
    RaiseException('Cannot register ASP.NET: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
end; 

// Returns True if a restart is required. Throws exceptions. 
function UninstallIIs(): Boolean; 
var 
    Version: TWindowsVersion; 
    Success: Boolean; 
    ResultCode: Integer; 
    IIS56IniFile: String; 
begin 
    GetWindowsVersionEx(Version); 
    if (Version.Major <= 5) then // XP/2003: Use "sysocmgr" 
    begin 
    IIS56IniFile := ExpandConstant('{tmp}\~iis56.ini'); 
    SaveStringToFile(IIS56IniFile, '[Components]' + #13#10 + 'iis_common = OFF' + #13#10 + 'iis_www = OFF' + #13#10 + 'iis_inetmgr = OFF', False); 
    Success := Exec('sysocmgr', ExpandConstant('/i:"{win}\inf\sysoc.inf" /u:"' + IIS56IniFile + '"'), '', SW_SHOW, ewWaitUntilTerminated, ResultCode); 
    DeleteFile(IIS56IniFile); 
    end 
    else if (Version.Major = 6) and (Version.Minor = 0) then // Vista/2008: Use "pkgmgr" 
    Success := Exec('pkgmgr', '/norestart /uu:IIS-WebServerRole', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) 
    else // 7/2008 R2 and later: Use "Dism" 
    Success := Exec('Dism', '/NoRestart /Online /Disable-Feature /FeatureName:IIS-WebServerRole', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 
    if not Success or ((ResultCode <> ERROR_SUCCESS) and (ResultCode <> ERROR_SUCCESS_REBOOT_REQUIRED)) then 
    RaiseException('Cannot uninstall IIS: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    Result := ResultCode = ERROR_SUCCESS_REBOOT_REQUIRED; 
end; 

的网站/应用注册和删除

创建一个虚拟目录和ASP.NET 4应用程序池。

注意:IIsServerNumber是通常默认为1(=>“默认Web站点”)的实际Web站点。

用法:

RegisterAppAtIIs('MyAppName', 1, 'MyAppAppPoolName'); 
DeleteAppFromIIs('MyAppName', 1, 'MyAppAppPoolName'); 
// Throws exceptions. 
procedure RegisterAppAtIIs(const IIsAppName: String; const IIsServerNumber: Integer; const IIsApplicationPoolName: String); 
var 
    Version: TWindowsVersion; 
    // IIS 5.1 - 6.0 
    IIS, WebService, WebSite, WebRoot, vDir, AppPools, AppPool: Variant; 
    // IIS 7 and later 
    ResultCode: Integer; 
    ExecResult: String; 
    WebSiteName: String; 
begin 
    GetWindowsVersionEx(Version); 
    if (Version.Major = 5) and (Version.Minor <= 2) then // XP, 2003: IIS 5.1 - 6.0 
    begin 
    try 
     // Create the main IIS COM Automation object 
     IIS := CreateOleObject('IISNamespace'); 
     WebService := IIS.GetObject('IIsWebService', 'localhost/W3SVC'); 
     // Get web site 
     WebSite := WebService.GetObject('IIsWebServer', IntToStr(IIsServerNumber)); 
     WebRoot := WebSite.GetObject('IIsWebVirtualDir', 'Root'); 
    except 
     RaiseException(Format('Web-site #%d not found: ', [IIsServerNumber]) + GetExceptionMessage()); 
    end; 
    // Delete the virtual dir if it already exists 
    try 
     WebRoot.Delete('IIsWebVirtualDir', IIsAppName); 
     WebRoot.SetInfo(); 
    except 
    end; 
    if (Version.Minor = 1) then // XP: IIS 5.1 
    begin 
     // Create the virtual directory 
     try 
     vDir := WebRoot.Create('IIsWebVirtualDir', IIsAppName); 
     vDir.AccessRead := True; 
     vDir.AccessScript := True; 
     vDir.AppFriendlyName := IIsAppName; 
     vDir.Path := ExpandConstant('{app}'); 
     vDir.AppCreate(True); // Create in "InProc" mode (don't really know why though) 
     vDir.SetInfo(); 
     except 
     RaiseException('Cannot create virtual directory: ' + GetExceptionMessage()); 
     end; 
    end 
    else if (Version.Minor = 2) then // 2003: IIS 6.0 
    begin 
     // Application pool stuff 
     AppPools := WebService.GetObject('IIsApplicationPools', 'AppPools'); 
     try 
     // Check if the application pool already exists 
     AppPool := AppPools.GetObject('IIsApplicationPool', IIsApplicationPoolName); 
     except 
     AppPool := Null; 
     end; 
     if VarIsNull(AppPool) then 
     begin 
     // Create the application pool 
     try 
      AppPool := AppPools.Create('IIsApplicationPool', IIsApplicationPoolName); 
      AppPool.SetInfo(); 
     except 
      RaiseException('Cannot add application pool: ' + GetExceptionMessage()); 
     end; 
     end; 
     // Create the virtual directory 
     try 
     vDir := WebRoot.Create('IIsWebVirtualDir', IIsAppName); 
     vDir.AccessRead := True; 
     vDir.AccessScript := True; 
     vDir.AppFriendlyName := IIsAppName; 
     vDir.Path := ExpandConstant('{app}'); 
     vDir.AppCreate(True); // Create in "InProc" mode 
     vDir.AppPoolId := IIsApplicationPoolName; 
     vDir.SetInfo(); 
     except 
     RaiseException('Cannot create virtual directory: ' + GetExceptionMessage()); 
     end; 
    end; 
    // Register handlers for ASP.NET 4 (important if other ASP.NET versions are present) 
    if not ExecWithResult(ExpandConstant('{dotnet40}') + '\aspnet_regiis.exe',Format('-s "W3SVC/%d/ROOT/%s"', [IIsServerNumber, IIsAppName]),'',SW_HIDE,ewWaitUntilTerminated, ResultCode, ExecResult) or (ResultCode <> ERROR_SUCCESS) then 
     RaiseException('Cannot set ASP.NET version: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    end 
    else if (Version.Major >= 6) then // Vista/2008 or later : IIS 7 or later 
    begin 
    // Get name of web-site 
    if not IIs7ExecAppCmd(Format('list site /id:%d /text:name', [IIsServerNumber]), ExecResult, ResultCode) then 
     RaiseException(Format('Cannot get name of web-site #%d: ', [IIsServerNumber]) + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    if (Length(Trim(ExecResult)) = 0) then 
     RaiseException(Format('Web-site #%d not found.', [IIsServerNumber])); 
    WebSiteName := ExecResult; 
    // Delete the application if it already exists 
    if not IIs7ExecAppCmd(Format('delete app "%s/%s"', [WebSiteName, IIsAppName]), ExecResult, ResultCode) or 
     ((ResultCode <> ERROR_SUCCESS) and (ResultCode <> ERROR_NOT_FOUND) and (ResultCode <> ERROR_NOT_SUPPORTED)) then 
     RaiseException('Cannot delete application: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    // Check if the application pool already exists (we don't delete it, since another GVS instance might be using it too) 
    if not IIs7ExecAppCmd(Format('list apppool "%s" /text:name', [IIsApplicationPoolName]), ExecResult, ResultCode) or 
     ((ResultCode <> ERROR_SUCCESS) and (ResultCode <> ERROR_INVALID_FUNCTION)) then 
     RaiseException('Cannot list application pools: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    // Create the application pool 
    if (ExecResult <> IIsApplicationPoolName) then 
    begin 
     if not IIs7ExecAppCmd(Format('add apppool /name:"%s" /managedRuntimeVersion:v4.0', [IIsApplicationPoolName]), ExecResult, ResultCode) or (ResultCode <> ERROR_SUCCESS) then 
     RaiseException('Cannot add application pool: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    end; 
    // Create the application 
    if not IIs7ExecAppCmd(Format('add app /site.name:"%s" /path:"/%s" /physicalPath:"%s" /applicationPool:"%s"', [WebSiteName, IIsAppName, ExpandConstant('{app}'), IIsApplicationPoolName]), ExecResult, ResultCode) or 
     (ResultCode <> ERROR_SUCCESS) then 
     RaiseException('Cannot add application: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    end 
    else 
    RaiseException('Cannot register web-site: Unknown OS version.'); 
end; 

// Throws exceptions. 
procedure DeleteAppFromIIs(const IIsAppName: String; const IIsServerNumber: Integer; const IIsApplicationPoolName: String); 
var 
    Version: TWindowsVersion; 
    // IIS 5.1 - 6.0 
    IIS, WebService, WebSite, WebRoot, AppPools: Variant; 
    // IIS 7 and later 
    ResultCode: Integer; 
    ExecResult: String; 
    WebSiteName: String; 
begin 
    GetWindowsVersionEx(Version); 
    if (Version.Major = 5) and (Version.Minor <= 2) then // XP, 2003: IIS 5.1 - 6.0 
    begin 
    try 
     // Create the main IIS COM Automation object 
     IIS := CreateOleObject('IISNamespace'); 
     WebService := IIS.GetObject('IIsWebService', 'localhost/W3SVC'); 
     // Get web site 
     WebSite := WebService.GetObject('IIsWebServer', IntToStr(IIsServerNumber)); 
     WebRoot := WebSite.GetObject('IIsWebVirtualDir', 'Root'); 
    except 
     RaiseException(Format('Web-site #%d not found: ', [IIsServerNumber]) + GetExceptionMessage()); 
    end; 
    // Delete the virtual dir 
    try 
     WebRoot.Delete('IIsWebVirtualDir', IIsAppName); 
     WebRoot.SetInfo(); 
    except 
    end; 
    if (Version.Minor = 2) then // 2003: IIS 6.0 
    begin 
     // Application pool stuff 
     AppPools := WebService.GetObject('IIsApplicationPools', 'AppPools'); 
     try 
     // Delete the application pool 
     AppPools.Delete('IIsApplicationPool', IIsApplicationPoolName); 
     except 
     end; 
    end; 
    end 
    else if (Version.Major >= 6) then // Vista/2008 or later : IIS 7 or later 
    begin 
    // Get name of web-site 
    if not IIs7ExecAppCmd(Format('list site /id:%d /text:name', [IIsServerNumber]), ExecResult, ResultCode) then 
     RaiseException(Format('Cannot get name of web-site #%d: ', [IIsServerNumber]) + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    if (Length(Trim(ExecResult)) = 0) then 
     RaiseException(Format('Web-site #%d not found.', [IIsServerNumber])); 
    WebSiteName := ExecResult; 
    // Delete the application 
    if not IIs7ExecAppCmd(Format('delete app "%s/%s"', [WebSiteName, IIsAppName]), ExecResult, ResultCode) or 
     ((ResultCode <> ERROR_SUCCESS) and (ResultCode <> ERROR_NOT_FOUND) and (ResultCode <> ERROR_NOT_SUPPORTED)) then 
     RaiseException('Cannot delete application: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    // Delete the application pool 
    if not IIs7ExecAppCmd(Format('delete apppool "%s"', [IIsApplicationPoolName]), ExecResult, ResultCode) or 
     ((ResultCode <> ERROR_SUCCESS) and (ResultCode <> ERROR_NOT_FOUND)) then 
     RaiseException('Cannot delete application pool: ' + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    end 
    else 
    RaiseException('Cannot delete web-site: Unknown OS version.'); 
end; 

有用的工具

查找使用ASP.NET虚拟目录的读/写文件的Windows用户帐户。用于设置“App_Data”的权限。

// Throws exceptions. 
function GetIIsAppPoolIdentity(const IIsApplicationPoolName: String): String; 
var 
    Version: TWindowsVersion; 
    ResultCode: Integer; 
    ExecResult: String; 
    IIS, AppPool: Variant; 
begin 
    GetWindowsVersionEx(Version); 
    if (Version.Major = 5) and (Version.Minor = 1) then // XP: IIS 5.1 
    begin 
    // TODO: Should be read from "[.NET 4]\Config\machine.config" -> system.web.processModel.userName 
    // - "System" 
    // - "Machine" -> "ASPNET" (default) 
    // - A custom user account 
    // See https://msdn.microsoft.com/en-us/library/dwc1xthy.aspx 
    Result := 'ASPNET'; 
    end 
    else if (Version.Major = 5) and (Version.Minor = 2) then // 2003: IIS 6.0 
    begin 
    try 
     IIS := CreateOleObject('IISNamespace'); 
     AppPool := IIS.GetObject('IIsApplicationPool', 'localhost/W3SVC/AppPools/' + IIsApplicationPoolName); 
     if (AppPool.AppPoolIdentityType = 0) then 
     Result := 'NT AUTHORITY\SYSTEM' 
     else if (AppPool.AppPoolIdentityType = 1) then 
     Result := 'NT AUTHORITY\LOCAL SERVICE' 
     else if (AppPool.AppPoolIdentityType = 2) then 
     Result := 'NT AUTHORITY\NETWORKSERVICE' 
     else if (AppPool.AppPoolIdentityType = 3) then 
     Result := AppPool.WAMUserName 
     else 
     RaiseException('Cannot get application pool identity: Unknown identity type (' + IntToStr(AppPool.AppPoolIdentityType) + ')'); 
    except 
     RaiseException('Cannot get application pool identity: Unable to get object'); 
    end; 
    end 
    else if (Version.Major >= 6) then // Vista/2008 or later 
    begin 
    if not IIs7ExecAppCmd(Format('list apppool "%s" /text:processModel.identityType', [IIsApplicationPoolName]), ExecResult, ResultCode) or 
     (ResultCode <> ERROR_SUCCESS) then 
     RaiseException('Cannot get application pool identity.'); 
    if (ExecResult = 'LocalSystem') then 
     Result := 'NT AUTHORITY\SYSTEM' 
    else if (ExecResult = 'LocalService') then 
     Result := 'NT AUTHORITY\LOCAL SERVICE' 
    else if (ExecResult = 'NetworkService') then 
     Result := 'NT AUTHORITY\NETWORKSERVICE' 
    else if (ExecResult = 'ApplicationPoolIdentity') then 
     Result := 'IIS AppPool\' + IIsApplicationPoolName 
    else 
     RaiseException('Cannot get application pool identity: Unknown identity type "' + ExecResult + '"'); 
    end 
    else 
    RaiseException('Cannot get application pool identity: Unknown OS version: ' + IntToStr(Version.Major) + '.' + IntToStr(Version.Minor)); 
end; 

返回到网站的物理路径。有用的是找到安装文件的正确位置。

// Throws exceptions. 
function IIsGetPhysicalPath(const IIsServerNumber: Integer): String; 
var 
    Version: TWindowsVersion; 
    IIS, WebService, WebSite, WebRoot: Variant; 
    ResultCode: Integer; 
    ExecResult: String; 
    WebSiteName: String; 
    PhysPath: String; 
begin 
    GetWindowsVersionEx(Version); 
    if (Version.Major = 5) then 
    begin 
    try 
     // Create the main IIS COM Automation object 
     IIS := CreateOleObject('IISNamespace'); 
     WebService := IIS.GetObject('IIsWebService', 'localhost/W3SVC'); 
     // Get web site 
     WebSite := WebService.GetObject('IIsWebServer', IntToStr(IIsServerNumber)); 
     WebRoot := WebSite.GetObject('IIsWebVirtualDir', 'Root'); 
    except 
     RaiseException(Format('Web-site #%d not found: ', [IIsServerNumber]) + GetExceptionMessage()); 
    end; 
    PhysPath := WebRoot.Path; 
    end 
    else if (Version.Major >= 6) then // Vista/2008 or later 
    begin 
    // Get name of web-site 
    if not IIs7ExecAppCmd(Format('list site /id:%d /text:name', [IIsServerNumber]), ExecResult, ResultCode) then 
     RaiseException(Format('Cannot get name of web-site #%d: ', [IIsServerNumber]) + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    if (Length(Trim(ExecResult)) = 0) then 
     RaiseException(Format('Web-site #%d not found.', [IIsServerNumber])); 
    WebSiteName := ExecResult; 
    // Get physical path 
    if not IIs7ExecAppCmd(Format('list vdir /app.name:"%s"/ /text:physicalPath', [WebSiteName]), ExecResult, ResultCode) or 
     (ResultCode <> ERROR_SUCCESS) then 
     RaiseException(Format('Cannot get physical path to web-site "%s": ', [WebSiteName]) + SysErrorMessage(ResultCode) + ' (' + IntToStr(ResultCode) + ')'); 
    PhysPath := ExecResult; 
    end 
    else 
    RaiseException('Cannot get physical path to web-site root: Unknown OS version: ' + IntToStr(Version.Major) + '.' + IntToStr(Version.Minor)); 
    // Expand environment variables in path (e.g. "%SystemDrive%") 
    Result := ExpandEnvVars(PhysPath); 
end;