@manhng

Welcome to my blog!

Using AppCmd.exe to Create Site in IIS

September 3, 2020 21:39

Using AppCmd.exe to Create Site in IIS (edit)

https://www.codeproject.com/Articles/99634/Use-C-to-manage-IIS

https://stackoverflow.com/questions/39201375/change-iis-preloadenable-to-true-globally

https://forums.iis.net/t/1196037.aspx?Using+Appcmd+to+set+preloadEnabled+true

https://documentation.red-gate.com/sm7/getting-started/installing/choosing-the-web-server/enabling-application-initialization-in-iis

https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/bindings/binding

cd %windir%\system32\inetsrv

%windir%\system32\inetsrv\appcmd.exe

AppCmd.exe /?

ADD SITE | SET SITE

AppCmd.exe ADD SITE /name:WebApp01 /physicalPath:"C:\Inetpub\wwwroot\WebApp01" /bindings:https/*:44301:WebApp01
AppCmd.exe SET SITE /site.name:WebApp01 /[path='/'].applicationPool:DefaultAppPool

AppCmd.exe ADD SITE /name:WebApp02 /physicalPath:"C:\Inetpub\wwwroot\WebApp02" /bindings:https/*:44302:
AppCmd.exe SET SITE /site.name:WebApp02 /[path='/'].applicationPool:DefaultAppPool

AppCmd.exe ADD SITE /name:WebApp03 /physicalPath:"C:\Inetpub\wwwroot\WebApp03" /bindings:https/*:44303:
AppCmd.exe SET SITE /site.name:WebApp03 /[path='/'].applicationPool:DefaultAppPool

ADD & SET in a single line

mkdir C:\inetpub\wwwroot\WebApp04

AppCmd.exe add site /name:WebApp04 /physicalpath:"C:\inetpub\wwwroot\WebApp04" & AppCmd.exe set config -section:system.applicationHost/sites /+"[name='WebApp04'].bindings.[protocol='https',bindingInformation='192.168.1.61:44304:']" /commit:apphost

ADD & SITE Program

https://drive.google.com/file/d/10IJrgUf3dZ5mso66LwkOVRnnR-ohh1Zf/view?usp=sharing (manhng83@gmail.com)

using System;
using System.Diagnostics;
using System.Windows.Forms;
 
namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnCreateSite_Click(object senderEventArgs e)
        {
            string strSite = txtSite.Text.Trim();
            string strPhysicalPath = txtPhysicalPath.Text.Trim();
            string strIpAddress = txtIpAddress.Text.Trim();
            string strPort = txtPort.Text.Trim();
            string strHost = txtHost.Text.Trim();
 
            if (!System.IO.Directory.Exists(strPhysicalPath))
            {
                System.IO.Directory.CreateDirectory(strPhysicalPath);
            }
 
            string strAppCmdLocation = Environment.ExpandEnvironmentVariables("%windir%\\system32\\inetsrv\\appcmd.exe");
            if (System.IO.File.Exists(strAppCmdLocation))
            {
                string strCmdParams = strAppCmdLocation + string.Format(" add site /name:{0} /physicalpath:\"{1}\" & {2} set config -section:system.applicationHost/sites /+\"[name = '{0}'].bindings.[protocol = 'https', bindingInformation = '{3}:{4}:{5}']\" /commit:apphost"strSitestrPhysicalPathstrAppCmdLocationstrIpAddressstrPortstrHost);
 
                System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd""/c " + strCmdParams);
 
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                procStartInfo.CreateNoWindow = true;
                procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
 
                string result = proc.StandardOutput.ReadToEnd();
 
                Console.WriteLine(result);
                MessageBox.Show(result);
                Debug.WriteLine(result);
            }
        }
 
        private void Form1_Load(object senderEventArgs e)
        {
            txtSite.Text = "WebApp9";
            txtPhysicalPath.Text = @"C:\inetpub\wwwroot\WebApp9";
            txtIpAddress.Text = "192.168.1.61";
            txtPort.Text = "443";
            txtHost.Text = "";
        }
    }
}

LIST SITE to the XML file

AppCmd.exe list site "MySite" /config /xml > "C:\Users\Administrator\Desktop\MySite.xml"

Categories

Recent posts