'****************************************************
'		Script Witten by Larry Heintz
'		April 2006 www.windowsadminscripts.com
' This script will create an Application Pool on a
' local or remote computer. The Application Pool will
' inherit all default settings.
'
' To add custom Maximum Virtual Memory and
' Maximum Used Memory amounts please edit lines 22
' and 24. If the amounts are left at 0 the options
' will not be added and the default values will be used.
'
' Script Usage: cscript apadd.vbs /computer:[computername] /apname:"[App Pool Name]"
'****************************************************
Dim args,computername,apname
Dim mvm,mum
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
apname = lcase(args.Item("apname"))
'**** Optional Settings ****'
'Setting for Maximum Virtual Memory
mvm = 0
'Setting for Maximum Used Memory
mum = 0
'**** Optional Settings ****'

if wscript.arguments.count = 0 then
	wscript.echo "Usage: cscript apadd.vbs /computer:[computername] /apname:""[App Pool Name]"""
else
	if computername = "" then
		computername = getComputer()
	else
		computername = computername
	end if
	if apname = "" then
		wscript.echo "The script has ended due to no Application Pool name given (apname)"
		wscript.quit
	else
		apname = trim(apname)
	end if
	Call addAP(computername,apname)
end if

Function addAP(computername,apname)
On Error Resume Next
dim obj,objpool
set obj = GetObject("IIS://" & computername & "/W3SVC/apppools")
if not (errorChecking (computername)) then
	set objpool = obj.create("IIsApplicationPool",apname)
		if mvm <> 0 and mum <> 0 then
			objpool.PeriodicRestartMemory = (mvm * 1024)
			objpool.PeriodicRestartPrivateMemory = (mum * 1024)
		end if
		objpool.setinfo
	set objpool = nothing
wscript.echo "Application Pool " & ucase(apname) & " has been created"
end if	
set obj = nothing
End Function

Function getComputer()
	Dim objNet
	Set objNet = WScript.CreateObject("WScript.Network") 
	getComputer = objNet.ComputerName 
	Set objNet = Nothing 
End Function

Function errorChecking(computername) 
errorChecking = False 
if err.number <> 0 then 
	wscript.echo "Unable to connect to " & ucase(computername) & " or " & ucase(computername) & " is not running IIS 6.0"
	err.Clear () 
	errorChecking = True
end if 
end Function
