Advertisement
Guest User

MALIKE

a guest
Oct 26th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## FYI: These scripts are now maintained in a proper repository:
  2. ##      https://github.com/alastairtree/deploy-websites-with-powershell
  3.  
  4. ## Intro: Simple powershell script to install (or replace) a local website and app pool
  5. ## Usage: CreateSite.ps1 [WebsiteName] [AppPoolName] [Port] [Path] ([domain\user] [password])
  6. ## Note : These scripts require local admin priviliges!
  7.  
  8. # Load IIS tools
  9. Import-Module WebAdministration
  10. sleep 2 #see http://stackoverflow.com/questions/14862854/powershell-command-get-childitem-iis-sites-causes-an-error
  11.  
  12. # Get SiteName and AppPool from script args
  13. $siteName    = $args[0]  # "default web site"
  14. $appPoolName = $args[1]  # "DefaultAppPool"
  15. $port        = $args[2]  # "80"
  16. $path        = $args[3]  # "c:\sites\test"
  17. $header      = $args[4]  # "www.webstekXX.net
  18. $user        = $args[5]  # "domain\username"
  19. $password    = $args[6]  # "password1"
  20.  
  21. if($siteName -eq $null)    { throw "Empty site name, Argument one is missing" }
  22. if($appPoolName -eq $null) { throw "Empty AppPool name, Argument two is missing" }
  23. if($port -eq $null)        { throw "Empty port, Argument three is missing" }
  24. if($path -eq $null)        { throw "Empty path, Argument four is missing" }
  25.  
  26. $backupName = "$(Get-date -format "yyyyMMdd-HHmmss")-$siteName"
  27. "Backing up IIS config to backup named $backupName"
  28. $backup = Backup-WebConfiguration $backupName
  29.  
  30. try {
  31.     # delete the website & app pool if needed
  32.     if (Test-Path "IIS:\Sites\$siteName") {
  33.         "Removing existing website $siteName"
  34.         Remove-Website -Name $siteName
  35.     }
  36.  
  37.     if (Test-Path "IIS:\AppPools\$appPoolName") {
  38.         "Removing existing AppPool $appPoolName"
  39.         Remove-WebAppPool -Name $appPoolName
  40.     }
  41.  
  42.  
  43.     "Create an appPool named $appPoolName under v4.0 runtime, default (Integrated) pipeline"
  44.     $pool = New-WebAppPool $appPoolName
  45.     $pool.managedRuntimeVersion = "v4.0"
  46.     $pool.processModel.identityType = 2 #NetworkService
  47.    
  48.     if ($user -ne $null -AND $password -ne $null) {
  49.         "Setting AppPool to run as $user"
  50.         $pool.processmodel.identityType = 3
  51.         $pool.processmodel.username = $user
  52.         $pool.processmodel.password = $password
  53.     }
  54.    
  55.     $pool | Set-Item
  56.  
  57.     if ((Get-WebAppPoolState -Name $appPoolName).Value -ne "Started") {
  58.         throw "App pool $appPoolName was created but did not start automatically. Probably something is broken!"
  59.     }
  60.  
  61.     "Create a website $siteName from directory $path on port $port"
  62.     $website = New-Website -Name $siteName -PhysicalPath $path -ApplicationPool $appPoolName -Port $port -HostHeader $header
  63.  
  64.     if ((Get-WebsiteState -Name $siteName).Value -ne "Started") {
  65.         throw "Website $siteName was created but did not start automatically. Probably something is broken!"
  66.     }
  67.  
  68.     "Website and AppPool created and started sucessfully"
  69. } catch {
  70.     "Error detected, running command 'Restore-WebConfiguration $backupName' to restore the web server to its initial state. Please wait..."
  71.     sleep 3 #allow backup to unlock files
  72.     Restore-WebConfiguration $backupName
  73.     "IIS Restore complete. Throwing original error."
  74.     throw
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement