Working with IIS

Create FTP and Web Sites and Application Pool
The following code will create a new FTP Virtual Directory, Website (including Virtual Directory) and Application Pool on the Local Computer.

import clr import System clr.AddReferenceByPartialName("System.DirectoryServices") from System.DirectoryServices import * from System import Array

web_root_path = "D:\\websites\\" domain = "ironpython.info" local_ip = "10.1.1.10" site_id = "100" anon_user = "ip_anon" anon_user_pass = "MyPassword" apppool_user = "ip_apppool" apppool_user_pass = "MyPassword"

print "Create FTP Virtual Directory" ftp_server = System.DirectoryServices.DirectoryEntry("IIS://" + System.Environment.MachineName + "/MSFTPSVC/1/ROOT") new_ftp = ftp_server.Children.Add(domain,"IIsFtpVirtualDir") new_ftp.Properties["AccessFlags"].Value = 3 new_ftp.Properties["DontLog"].Value = 0 new_ftp.Properties["FtpDirBrowseShowLongDate"].Value = 0 new_ftp.Properties["Path"].Value = web_root_path + domain new_ftp.CommitChanges

print "Create web site" web_server = System.DirectoryServices.DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC") new_site = web_server.Children.Add(site_id,"IISWebServer") new_site.Properties["ServerComment"].Value = domain new_site.Properties["ServerBindings"].Value = Array[str]((local_ip+":80:"+domain ,local_ip+":80:www."+domain)) new_site.Properties["ServerState"].Value = 2 new_site.Properties["ServerAutoStart"].Value = 1 new_site.Properties["AnonymousUserName"].Value = anon_user new_site.Properties["AnonymousUserPass"].Value = anon_user_pass new_site.Properties["LogFileDirectory"].Value = web_root_path + domain + "\logs" new_site.Properties["LogFileLocaltimeRollover"].Value = 1 new_site.Properties["LogType"].Value = 1 new_site.CommitChanges

print "Create virtual directory" site_vdir = new_site.Children.Add("Root","IISWebVirtualDir") site_vdir.Properties["Path"].Value = web_root_path + domain + "\www" site_vdir.Properties["AppFriendlyName"].Value = domain site_vdir.Properties["AccessFlags"].Value = 513 # read + script site_vdir.Properties["AuthFlags"].Value = 1 # AuthAnonymous site_vdir.CommitChanges site_vdir.Invoke("AppCreate",1) site_vdir.CommitChanges

print "Create app pool" app_pools = System.DirectoryServices.DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/AppPools") new_pool = app_pools.Children.Add(domain,"IIsApplicationPool") new_pool.CommitChanges new_pool.Properties["WAMUserName"].Value = apppool_user new_pool.Properties["WAMUserPass"].Value = apppool_user_pass new_pool.CommitChanges

print "Assign App pool to site" new_site.Properties["AppPoolID"].Value = domain new_site.CommitChanges

Notes:
 * If you are wanting to make changes to a remote computer replace System.Environment.MachineName.
 * Each website needs a unique ID, this is passed to the web_server.Children.Add command. You will need to make sure your code passes a unique number for each site you create.

List Websites
This code will return a list of all websites configured on the local server. import clr import System clr.AddReferenceByPartialName("System.DirectoryServices") from System.DirectoryServices import *

web_server = System.DirectoryServices.DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC") sites = web_server.Children for site in sites: if site.Name.isdigit: print site.Properties["ServerComment"].Value + ":" + site.Name

Back to Contents.