用powershell指令碼實現web網站自動部署

來源:互聯網
上載者:User

使用者要求將原來的多個檔案合并成一個ps1檔案實現web網站的自動部署,在合并過程中遇到兩個問題:

1. powershell 不區分大小寫,所以在變數命名時一定要注意;

2. 在合并中遇到這個異常:

Exception setting "Protocol": "The configuration object is read only, because it has been committed by a call to ServerManager.CommitChanges(). If write access is required, use ServerManager to get a new reference."
At C:\v-zhdu\code\WebsiteDeployment\Website_Deployment.ps1:76 char:11
+                 $bind. <<<< Protocol = $Binding.Protocol
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

根據提示,查看指令碼,發現對網站的設定提交變化後,需要重新擷取新的引用才可以獲得寫的許可權,具體參見下面的script。

Website_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

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$iis = New-Object Microsoft.Web.Administration.ServerManager

#Load the config file
[xml]$Config = get-content ($scriptDir + "\Config.xml")
$AppPools = $Config.Script

##############################RemoveSitePool-begin
foreach ($AppPool in $AppPools.ApplicationPool)
{ 
 foreach($Site in $AppPool.Site)
 {
         
      #RemoveSite $Site.Name
  $website = $iis.Sites[$Site.Name]
  if($website -ne $null)
  {
   $website.Delete()
   $iis.CommitChanges()
       Write-Host "Site Removed" -foregroundColor Green
  }          
        }
 
 #RemoveAppPool
 $pool = $iis.ApplicationPools[$AppPool.Name]
 if($pool -ne $null)
 {
  $pool.Delete()
  $iis.CommitChanges()
  Write-Host "App Pool Removed" -foregroundColor Green
 } 
}
##############################RemoveSitePool-end

##############################CreateSitePool-begin
foreach ($AppPool in $AppPools.ApplicationPool)
{
 #CreateAppPool
 $iis.ApplicationPools.Add($AppPool.Name)
 $pool = $iis.ApplicationPools[$AppPool.Name]
 $pool.ProcessModel.UserName = $AppPool.UserName
 $pool.ProcessModel.Password = $AppPool.Password
 $iis.CommitChanges()
 Write-Host "App Pool Created" -foregroundColor Green
 
 foreach($Site in $AppPool.Site)
 {
         
      #CreateSite
 
  $website = $iis.Sites.CreateElement()
  $website.ID = $Site.ID
  $website.Name = $Site.Name
  $website.Applications.Add("/", $Site.PhysicalPath)
  $website.Applications["/"].ApplicationPoolName = $AppPool.Name
  $iis.Sites.Add($website)
  #$iis.CommitChanges()         #should be comment out, or will throw exception like 2#
      Write-Host "Site Created" -foregroundColor Green   
   
      foreach($Binding in $Site.Binding)
      {                 
             #CreateBindingOnSite
    $bind = $website.Bindings.CreateElement()
    $bind.Protocol = $Binding.Protocol
    $bind.BindingInformation = "*:" + $Binding.Port + ":" + $Binding.HostName
    $website.Bindings.Add($bind)
    $iis.CommitChanges()
             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         
 
}

}

##############################CreateSitePool-end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.