Deploy an website automatically using powershell

Source: Internet
Author: User

The website or WebService must be automatically deployed to IIS. Currently, powershell is used for implementation. The related code is as follows:

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
[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, # shocould 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.

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.