轉 : http://www.sostan.com/webscript/vbs-aspapppool/
應用程式集區在使用一段時間後需要回收,想用程式來控制,就用vbs實現了以下代碼,在asp上一樣運行。
set apppools = GetObject(“IIS://LocalHost/W3SVC/AppPools”)for each apppool in apppoolsmsgbox apppool.nameapppool.recyclenext
上面的程式在安裝有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%的CPUNewPool.PeriodicRestartTime = 180′記憶體回收時間(分鐘)NewPool.CPUResetInterval = 2′重新整理CPU使用率值(分鐘)NewPool.AppPoolAutoStart = true’自動啟動此池NewPool.SetInfoSet AppPools = nothingset NewPool = nothingif err.number=0 then CreateAppPool=trueend function
2. 設定應用程式集區的屬性
function SetAppPoolSetting(AppPoolName,Values)SetAppPoolSetting=falseSet 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.SetInfoset apps=nothingSetAppPoolSetting=trueend 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,"停止失敗"); } }