1. 程式人生 > >C++ Windows Server 2016用CMD命令列安裝IIS角色和功能元件

C++ Windows Server 2016用CMD命令列安裝IIS角色和功能元件

Windows Server 2016資料中心純淨版上安裝IIS1.0版本的元件,Windows Server 2016和Windows Server 2012版本,他們的元件是沒有多大區別的,但是安裝的方式有所差異,也就是安裝的順序比較怪異。由於在08和12版本上面安裝都沒有問題,理所當然我就以12版本的IIS安裝方式去安裝16版本的,既然是安裝IIS,那麼最重要的當然是安裝【伺服器角色】-->【Web伺服器(IIS)】下的部分元件。
但是問題來了,因為16版本的IIS10.0的元件,如果安裝常規先安裝:
【功能】-->【.NET Framework 3.5功能】
【伺服器角色】-->【Web伺服器(IIS)】-->【Web伺服器】-->【應用程式開發】-->[.Net Extensibility 3.5]/[ASP.NET 3.5]

那麼[.Net Extensibility 3.5]/[ASP.NET 3.5]是無法安裝的,為什麼?我也不知道!o_X ,但是後來經過我一個上午的安裝解除安裝安裝解除安裝(當然這一切都是在cmd命令列下進行的)最後發現了問題,那就是[.Net Extensibility 3.5]/[ASP.NET 3.5]和[.Net Extensibility 4.6]/[ASP.NET 4.6]的安裝都需要依賴【功能】-->【.NET Framework 4.6功能】-->[ASP.NET 4.6]元件。假設這四個都已經安裝了,如果解除安裝掉【功能】-->【.NET Framework 4.6功能】-->[ASP.NET 4.6]元件,那麼這四個元件也將被解除安裝掉,由此可知他們是依賴[ASP.NET 4.6]元件的。

所以,我整理的安裝順序是這樣的:
(1)【功能】-->【.NET Framework 3.5功能】
     【.NET Framework 3.5功能】-->[.NET Frameword 3.5];
(2)【功能】-->【.NET Framework 4.5功能】
     【.NET Framework 3.5功能】-->[.NET Frameword 4.6]
     【.NET Framework 3.5功能】-->[ASP.NET 4.6];
(3)【伺服器角色】-->【Web伺服器(IIS)】-->【Web伺服器】-->【應用程式開發】-->[.Net Extensibility 3.5]/[ASP.NET 3.5];

(4)【伺服器角色】-->【Web伺服器(IIS)】-->【Web伺服器】-->【應用程式開發】-->[.Net Extensibility 4.6]/[ASP.NET 4.6];

.NET重要元件圖:



程式碼如下:

#include "stdafx.h"  
#include <windows.h>  
#include <tchar.h>  
#include <string>  
#include <iostream>  
#include <Shlwapi.h>  
#include "process.h"  
#pragma comment(lib, "Shlwapi.lib")  
using namespace std;  
  
bool RunSysCmd(const wstring& strcmd);  
wstring getSystemDirectory();  
BOOL IsWow64();  
bool WinServ2016_IISInstall();  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    bool bRet = false;  
  
    bRet = WinServ2016_IISInstall();  
  
    system("pause");  
    return 0;  
}  
  
bool RunSysCmd(const wstring& strcmd)  
{  
    bool suc;  
    wstring stdDirectory = getSystemDirectory() + _T("Windows\\System32\\");  
    if (IsWow64())  
    {  
        PVOID OldValue = NULL;  
        Wow64DisableWow64FsRedirection(&OldValue);  
        SHELLEXECUTEINFO ShExecInfo = { 0 };  
        ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);  
        ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;  
        ShExecInfo.hwnd = NULL;  
        ShExecInfo.lpVerb = _T("runas");  
        ShExecInfo.lpFile = _T("cmd.exe");  
        ShExecInfo.lpParameters = strcmd.c_str();  
        ShExecInfo.lpDirectory = stdDirectory.c_str();  
        ShExecInfo.nShow = SW_HIDE;  
        ShExecInfo.hInstApp = NULL;  
        suc = ShellExecuteEx(&ShExecInfo);  
        WaitForSingleObject(ShExecInfo.hProcess, INFINITE);  
        Wow64RevertWow64FsRedirection(OldValue);  
    }  
    else  
    {  
        SHELLEXECUTEINFO ShExecInfo = { 0 };  
        ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);  
        ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;  
        ShExecInfo.hwnd = NULL;  
        ShExecInfo.lpVerb = _T("open");  
        ShExecInfo.lpFile = _T("cmd.exe");  
        ShExecInfo.lpParameters = strcmd.c_str();  
        ShExecInfo.lpDirectory = stdDirectory.c_str();  
        ShExecInfo.nShow = SW_HIDE;  
        ShExecInfo.hInstApp = NULL;  
        suc = ShellExecuteEx(&ShExecInfo);  
        WaitForSingleObject(ShExecInfo.hProcess, INFINITE);  
    }  
    return suc;  
}  
  
wstring getSystemDirectory()  
{  
    wstring wstr = _T("");  
    int size = ::GetSystemDirectory(NULL, 0);  
    wchar_t *path = new wchar_t[size];  
    if (::GetSystemDirectory(path, size) != 0)  
    {  
        wstr = path;  
    }  
    delete[] path;  
    return wstr;  
}  
  
BOOL IsWow64()  
{  
    typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);  
    LPFN_ISWOW64PROCESS fnIsWow64Process;  
    BOOL bIsWow64 = FALSE;  
    fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(_T("kernel32")), "IsWow64Process");  
    if (NULL != fnIsWow64Process){  
        fnIsWow64Process(GetCurrentProcess(), &bIsWow64);  
    }  
    return bIsWow64;  
}  
 
bool WinServ2016_IISInstall()
{
	std::cout << "start install IIS comm";
	bool bRet = false;

	std::wstring strRunCmd = _T("/c start /w pkgmgr /iu:IIS-WebServerRole;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-WebServer;");
	bRet = RunSysCmd(strRunCmd);
	//常見HTTP功能
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-CommonHttpFeatures;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-StaticContent;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-DefaultDocument;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-DirectoryBrowsing;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-HttpErrors;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-HttpRedirect;");
	bRet = RunSysCmd(strRunCmd);
	//應用程式開發
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ApplicationDevelopment;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:NetFx3ServerFeatures;");//這個安裝特別慢!!!最好衝杯咖啡核一下等待
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:NetFx3;");//注意:只有父功能開啟了,子功能才能安裝,否則,無法安裝
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:NetFx4ServerFeatures;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:NetFx4;");//注意:只有父功能開啟了,子功能才能安裝,否則,無法安裝
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:NetFx4Extended-ASPNET45;");//注意:必須先安裝此元件,下面的3、4元件才能安裝
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-NetFxExtensibility;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ASPNET;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-NetFxExtensibility45;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ASPNET45;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-CGI;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ASP;IIS-ISAPIExtensions;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ISAPIFilter;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ServerSideIncludes;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-WebSockets;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ApplicationInit;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ASPNET;");//再次安裝,保障一點
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ASPNET45;");//再次安裝,保障一點
	bRet = RunSysCmd(strRunCmd);
	//執行狀態(健康)和診斷
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-HealthAndDiagnostics;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-HttpLogging;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-LoggingLibraries;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-RequestMonitor;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-HttpTracing;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-CustomLogging;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ODBCLogging;");
	bRet = RunSysCmd(strRunCmd);
	//安全性
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-Security;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-BasicAuthentication;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-CertProvider;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-WindowsAuthentication;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-DigestAuthentication;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ClientCertificateMappingAuthentication;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-IISCertificateMappingAuthentication;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-URLAuthorization;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-RequestFiltering;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-IPSecurity;");
	bRet = RunSysCmd(strRunCmd);
	//效能
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-Performance;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-HttpCompressionStatic;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-HttpCompressionDynamic;");
	bRet = RunSysCmd(strRunCmd);
	//管理工具
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-WebServerManagementTools;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ManagementConsole;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ManagementScriptingTools;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-ManagementService;");
	bRet = RunSysCmd(strRunCmd);
	//FTP伺服器
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-FTPServer;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-FTPSvc;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:IIS-FTPExtensibility;");
	bRet = RunSysCmd(strRunCmd);
	//Windows process Activation Service
	strRunCmd = _T("/c start /w pkgmgr /iu:WAS-WindowsActivationService;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:WAS-ProcessModel;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:WAS-NetFxEnvironment;");
	bRet = RunSysCmd(strRunCmd);
	strRunCmd = _T("/c start /w pkgmgr /iu:WAS-ConfigurationAPI;");
	bRet = RunSysCmd(strRunCmd);

	return bRet;
}
//IIS的元件結構程式碼:  
WebServerRole                   web服務角色                   
    IIS-WebServer                   web伺服器  
        IIS-CommonHttpFeatures              常見HTTP功能  
            IIS-StaticContent               靜態內容  
            IIS-DefaultDocument             預設文件  
            IIS-DirectoryBrowsing               目錄瀏覽  
            IIS-HttpErrors                  HTTP錯誤  
            IIS-HttpRedirect                HTTP重定向  
        IIS-ApplicationDevelopment          應用程式  
            IIS-ASP                     ASP  
            IIS-CGI                     CGI  
            IIS-ASPNET                  ASP.NET3.5  
            IIS-NetFxExtensibility              .NET Extensibility3.5  
            IIS-ASPNET45                    ASP.NET4.5  
            IIS-NetFxExtensibility45            .NET Extensibility4.5  
            NetFx4Extended-ASPNET45  
            IIS-ISAPIFilter                 ISAPI篩選器  
            IIS-ISAPIExtensions             ISAPI擴充套件  
        IIS-HealthAndDiagnostics            執行狀態和診斷  
            IIS-HttpLogging                 HTTP日誌記錄  
            IIS-LoggingLibraries                日誌記錄工具  
            IIS-RequestMonitor              請求監控器  
            IIS-HttpTracing                 跟蹤  
            IIS-CustomLogging               自定義日誌  
            IIS-ODBCLogging                 ODBC日誌記錄  
        IIS-Security                    安全性  
            IIS-BasicAuthentication             基本身份驗證  
            IIS-WindowsAuthentication           Windows身份驗證  
            IIS-DigestAuthentication            摘要式身份驗證  
            IIS-ClientCertificateMappingAuthentication  客戶端證書對映身份驗證  
            IIS-IISCertificateMappingAuthentication     IIS證書對映身份驗證  
            IIS-URLAuthorization                URL授權  
            IIS-RequestFiltering                請求篩選  
            IIS-IPSecurity                  IP和域限制  
        IIS-Performance                 效能  
            IIS-HttpCompressionStatic           靜態內容壓縮  
            IIS-HttpCompressionDynamic          動態內容壓縮  
    IIS-WebServerManagementTools            管理工具      
        IIS-ManagementConsole               IIS管理控制檯  
        IIS-ManagementScriptingTools            IIS 管理指令碼和工具  
        IIS-ManagementService               管理服務  
    IIS-FTPServer                   FTP伺服器  
        IIS-FTPSvc              FTP服務  
        IIS-FTPExtensibility            FTP擴充套件  
  
NetFx3ServerFeatures                .NET Framework 3.5功能  
    NetFx3                      .NET Frameword 3.5  
  
NetFx4ServerFeatures                .NET Frameword 4.6功能  
    NetFx4                      .NET Frameword 4.6  
    NetFx4Extended-ASPNET45             ASP.NET 4.6  
  
WAS-WindowsActivationService            Windows Process Activation Service    
    WAS-ProcessMode                 程序模型  
    WAS-NetFxEnvironment                .NET Environment 3.5  
    WAS-ConfigurationAPI                配置 API

使用的是 pkgmgr的安裝方式,其實用dism也行。在此提供一個方法獲取這些角色和功能的命令程式碼, 在cmd.exe下或者Windows PowerShell.exe下,檢視元件是否安裝及元件角色和功能程式碼:

(1)進入 Windows PowerShell.exe 輸入:Import-Module ServerManager 回車執行;Get-WindowsFeature 回車執行;

(2)進入 Windows PowerShell.exe 輸入:dism /Online /Get-Features 回車執行;

(3)進入 cmd.exe 輸入:dism /Online /Get-Features 回車執行。

這些安裝都是在cmd下進行的,完整的安裝就如程式碼上的,有圖的部分知識要求注意的安裝順序,請注意最終以程式碼為主。好了Windows Server 2016的IIS10.0元件安裝就是這些了(我個人需要就這個麼多 o_X)。我有個疑問就是總覺得有點不合理,在此供大家學習,不足之處還望大家提出。