写好的website或者webservice需要自动化部署到IIS中,目前考虑用powershell实现,相关的代码如下:
1. AppPool_Site_Deployment.ps1
#Get the path where the script is running
$scriptDir = Split-Path (Resolve-Path $myInvocation.MyCommand.Path)
write-host "Script Location: " $scriptDir -foregroundColor Green
#Set up aliases
Set-Alias RemoveSitePool $scriptDir\AppPool_Site_Delete.ps1
Set-Alias CreateSitePool $scriptDir\AppPool_Site_Creation.ps1
RemoveSitePool
CreateSitePool
2. AppPool_Site_Delete.ps1
#Get the path where the script is running
$scriptDir = Split-Path (Resolve-Path $myInvocation.MyCommand.Path)
write-host "Delete Script Location: " $scriptDir -foregroundColor Green
#Set up aliases
Set-Alias RemoveAppPool $scriptDir\RemoveAppPool.ps1
Set-Alias RemoveSite $scriptDir\RemoveSite.ps1
#Load the config file
[xml]$Config = get-content ($scriptDir + "\Config.xml")
$AppPools = $Config.Script
foreach ($AppPool in $AppPools.ApplicationPool)
{
foreach($Site in $AppPool.Site)
{
if ($Site -ne $null)
{
RemoveSite $Site.Name
Write-Host "Site Removed" -foregroundColor Green
}
}
RemoveAppPool $AppPool.Name
Write-Host "App Pool Removed" -foregroundColor Green
}
2.1 RemoveSite.ps1
Param
(
[String]$Name #The Name of the site
)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$iis = new-object Microsoft.Web.Administration.ServerManager
$Site = $iis.Sites[$Name]
if($site -ne $null)
{
$site.Delete()
}
$iis.CommitChanges()
2.2 RemoveAppPool.ps1
Param
(
[String]$PoolName = "TestAppPool" #The Name of the App Pool
)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$iis = New-Object Microsoft.Web.Administration.ServerManager
$appPool = $iis.ApplicationPools[$PoolName]
if($appPool -ne $null)
{
$appPool.Delete()
}
$iis.CommitChanges()
3. AppPool_Site_Creation.ps1
#Get the path where the script is running
$scriptDir = Split-Path (Resolve-Path $myInvocation.MyCommand.Path)
write-host "Create Script Location: " $scriptDir -foregroundColor Green
#Set up aliases
Set-Alias CreateAppPool $scriptDir\CreateAppPool.ps1
Set-Alias CreateSite $scriptDir\CreateSite.ps1
Set-Alias CreateBindingOnSite $scriptDir\CreateBindingOnSite.ps1
Set-Alias RemoveSitePool $scriptDir\AppPool_Site_Delete.ps1
#Load the config file
[xml]$Config = get-content ($scriptDir + "\Config.xml")
$AppPools = $Config.Script
foreach ($AppPool in $AppPools.ApplicationPool)
{
CreateAppPool $AppPool.Name $AppPool.UserName $AppPool.Password
Write-Host "App Pool Created" -foregroundColor Green
foreach($Site in $AppPool.Site)
{
if ($Site -ne $null)
{
CreateSite $Site.Name $Site.PhysicalPath $AppPool.Name $Site.ID
Write-Host "Site Created" -foregroundColor Green
foreach($Binding in $Site.Binding)
{
if ($Binding -ne $null)
{
CreateBindingOnSite $Site.Name $Binding.Port $Binding.HostName
$Binding.Protocol
Write-Host "Binding Created" -foregroundColor Green
}
}
foreach($Folder in $Site.Folder)
{
if ($Folder -ne $null)
{
#Create the folder if it does not exist
$objFSO = New-Object -ComObject Scripting.FileSystemObject
if($objFSO.FileExists($Folder -eq $FALSE))
{
Mkdir ($Folder.Name)
}
foreach($Permission in $Folder.Permission)
{
if ($Permission -ne $null)
{
#Set the ACL permission on the folder
Cacls $folder.Name “/E” “/G” “$($permission.User):$($Permission.Capability)?
}
}
Write-Host "Completed Permissions Configuration for " $Folder.name -foregroundColor Green
}
}
Write-Host "Completed Folder Configuration for all folders" -foregroundColor Green
}
}
}
3.1 CreateAppPool.ps1
Param
(
[String]$PoolName = "TestAppPool", #The Name of the App Pool
[String]$UserName = "redmond\v-zhdu", #The AppPool UserName
[String]$Password = "4rfv$RFV" #The AppPool Password
)
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" )
$iis = New-Object Microsoft.Web.Administration.ServerManager
$iis.ApplicationPools.Add($PoolName)
$appPool = $iis.ApplicationPools[$PoolName]
$appPool.ProcessModel.UserName = $UserName
$appPool.ProcessModel.Password = $Password
$iis.CommitChanges()
3.2 CreateSite.ps1
Param
(
[String]$Name, #The Name of the site
[String]$Path, #The Physical Path of the site
[String]$AppPool, #The name of the appPool the default app is part of
[Int]$SiteID
)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$iis = new-object Microsoft.Web.Administration.ServerManager
$Site = $iis.Sites.CreateElement()
$Site.ID = $SiteID
$Site.Name = $Name
$Site.Applications.Add("/", $Path)
$Site.Applications["/"].ApplicationPoolName = $AppPool
$iis.Sites.Add($Site)
$iis.CommitChanges()
3.3 CreateBindingOnSite.ps1
Param
(
[String]$SiteName, #The Name of the site
[String]$Port, #Typically 80
[String]$BindingInfo, #should look like www.xxx.com
[String]$Protocol = "http"
)
#Reference the Microsoft.Web.Administration namespace
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$iis = new-object Microsoft.Web.Administration.ServerManager
#Add the Application to the site
$site = $iis.Sites[$SiteName]
$binding = $site.Bindings.CreateElement()
$binding.Protocol = $Protocol
$binding.BindingInformation = "*:" + $PORT + ":" + $BindingInfo
$site.Bindings.Add($binding)
$iis.CommitChanges()
Reference:
http://geekswithblogs.net/silverbax/archive/2008/08/06/124268.aspx
http://blogs.msdn.com/b/powershell/archive/2007/06/19/get-scriptdirectory.aspx
http://www.techtipsgeek.com/secure-private-folders-making-inaccessible-undeletable/104/
and also made a research on a zip file ProvisioningScripts 4-7-2008.zip which downloaded from website.