Automatic Web site deployment using powershell scripts

Source: Internet
Author: User

You need to merge multiple original files into a PS1 file to achieve automatic web site deployment. Two problems are encountered during the merge process:

1. powershell is case-insensitive, so be sure to name the variable;

2. This exception occurs during merge:

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

View the script according to the prompts. After the site settings are submitted and changed, you must obtain a new reference to obtain the write permission. For details, see the script below.

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 () # shocould 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.