[转]利用VBS或ASP脚本回收操作应用程序池

转 : http://www.sostan.com/webscript/vbs-aspapppool/

应用程序池在使用一段时间后需要回收,想用程序来控制,就用vbs实现了以下代码,在asp上一样运行。

set apppools = GetObject(“IIS://LocalHost/W3SVC/AppPools”)
for each apppool in apppools
msgbox apppool.name
apppool.recycle
next

上面的程序在安装有iis的服务器上面运行,即可弹出所有的应用程序池,想回收指定的应用程序池,只需要判断相应的apppool.name即可。

下面提供两个操作应用程序池的方法:
1. 创建一个池并设置属性

function CreateAppPool(NewAppPoolName)
Set AppPools = GetObject(“IIS://localhost/W3SVC/AppPools”)
set NewPool = AppPools.Create(“IIsApplicationPool”, NewAppPoolName)
NewPool.AppPoolIdentityType = 2′预定义账户0本地系统1本地服务2网络服务
NewPool.PeriodicRestartMemory = 512 * 1000 ‘最大虚拟内存使用值
NewPool.PeriodicRestartPrivateMemory = 500 * 1000 ’500物理内存限制’
NewPool.CPUAction = 0′超过CPU不操作,1就是超过cpu就关闭。
NewPool.CPULimit = “80000″‘最大80%的CPU
NewPool.PeriodicRestartTime = 180′内存回收时间(分钟)
NewPool.CPUResetInterval = 2′刷新CPU使用率值(分钟)
NewPool.AppPoolAutoStart = true’自动启动此池
NewPool.SetInfo
Set AppPools = nothing
set NewPool = nothing
if err.number=0 then CreateAppPool=true
end function

2. 设置应用程序池的属性

function SetAppPoolSetting(AppPoolName,Values)
SetAppPoolSetting=false
Set apps=GetObject(“IIS://localhost/w3svc/AppPools/”&AppPoolName)
SetValue=split(Values,”|”)
apps.CpuLimit=int(SetValue(1))*1000′最大CPU百分比
apps.CPUAction=SetValue(2)’超过处理方式0忽略1关闭
apps.PeriodicRestartMemory=int(SetValue(3))*1024′虚拟内存
apps.PeriodicRestartPrivateMemory=int(SetValue(4))*1024′物理内存
apps.PeriodicRestartTime=SetValue(5)’回收时间
apps.SetInfo
set apps=nothing
SetAppPoolSetting=true
end function

ASP.NET操作

http://topic.csdn.net/u/20090525/13/f1d14958-c2cc-4326-be1c-77112e1bb648.html

//添加应用程序池空间引用
using System.DirectoryServices;
using System.Text; 
using System.Text.RegularExpressions; 
using System.Diagnostics; 
using System.Management;

private void button6_Click(object sender, System.EventArgs e)
  {
   //如果应用程序池不存在,则会报错系统找不到指定路径
   string AppPoolName=this.textBox1.Text.Trim();
   string method="Start";

   try
   {    
      DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
      DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool");
      findPool.Invoke(method,null);
      appPool.CommitChanges();
      appPool.Close();
    MessageBox.Show("应用程序池名称启动成功","启动成功"); 
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,"启动失败");      
   }

  }

  private void button7_Click(object sender, System.EventArgs e)
  {
   //如果应用程序池当前状态为停止,则会发生异常报错
   string AppPoolName=this.textBox1.Text.Trim();
   string method="Recycle";

   try
   {    
      DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
      DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool");
      findPool.Invoke(method,null);
      appPool.CommitChanges();
      appPool.Close();
    MessageBox.Show("应用程序池名称回收成功","回收成功"); 
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,"回收失败");      
   }  
  }

  private void button8_Click(object sender, System.EventArgs e)
  {
   string AppPoolName=this.textBox1.Text.Trim();
   string method="Stop";

   try
   {    
      DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
      DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool");
      findPool.Invoke(method,null);
      appPool.CommitChanges();
      appPool.Close();
    MessageBox.Show("应用程序池名称停止成功","停止成功"); 
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,"停止失败");      
   }  
  } 

  

原文地址:https://www.cnblogs.com/Athrun/p/2717918.html