IIS cmd管理命令

命令控制台所在位置 C:\Windows\System32\inetsrv\appcmd.exe

创建iis站点:
appcmd add site /name:"MyTestSite1" /bindings:http/*:8990: /physicalPath:"E:TestSiteSite1"
解释:

/name	(必需) 站点名称
/bindings	绑定列表
/physicalPath	站点的物理路径

创建应用程序池:

appcmd add apppool /name:MyTestSite1 /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated /enable32BitAppOnWin64:true /processModel.identityType:LocalSystem

解释:

/managedRuntimeVersion	是.net Framework的版本 如:v1.0 v1.1或v2.0等
/managedPielineMode	托管管道模式:Classic经典模式,Integrated集成模式
/enable32BitAppOnWin64	是否启用32位应用程序
/processModel.identityType	进程模型--标识

设置站点的应用程序池:

appcmd set site /site.name:MyTestSite1 /[path=‘/‘].applicationPool:MyTestSite1

删除站点及应用程序池:

appcmd delete site /site.name:MyTestSite1 
appcmd delete apppool /apppool.name:MyTestSite1

停止、启动 站点、应用程序池:

appcmd stop site "MyTestSite1" 
appcmd start site "MyTestSite1"      
appcmd stop apppool "MyTestSite1" 
appcmd start apppool "MyTestSite1"

列出所有的站点、应用程序池:

appcmd list sites 
appcmd list apppools

列出指定站点的状态

C:\Windows\System32\inetsrv>appcmd.exe  list site "XXX_8070"
SITE "XXX_8070" (id:10,bindings:http/*:8070:,state:Started)

封装的服务端命令代码

public ActionResult GetSiteStatus(string siteName)
        {
            var result = ExecuteIISCommand($@"list site ""{siteName}""");
            return View();
        }

        /// <summary>
        /// 执行IIS命令
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        private string ExecuteIISCommand(string command)
        {
            return ExecuteCommand($@"C:\Windows\System32\inetsrv\appcmd.exe {command}");
        }


        /// <summary>
        /// 执行cmd命令
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        private string ExecuteCommand(string commandText)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            string strOutput = null;
            try
            {
                p.Start();
                p.StandardInput.WriteLine(commandText);
                p.StandardInput.WriteLine("exit");
                strOutput = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();

                //截取输出结果
                var lines = strOutput.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
                lines = lines.Skip(4).Take(lines.Count - 4 - 3).ToList();
                return string.Join("\r\n", lines);
            }
            catch (Exception e)
            {
                strOutput = e.Message;
            }
            return strOutput;
        }
posted @ 2022-07-08 15:07  sands  阅读(391)  评论(0编辑  收藏  举报