Monday, December 31, 2012

How To Deploy applications to IIS using Powershell

How To Deploy applications to IIS

#Additional information on deploying to IIS here http://www.iis.net/learn/manage/powershell/powershell-snap-in-creating-web-sites-web-applications-virtual-directories-and-application-pools
$from = "c:\temp\WebApp\*"
$name = "WebApp"
$to = "C:\inetpub\wwwroot\"+$name+"\"
$physicalPath = $to
#Move the Web Application to the IIS folder
Copy-Item $from $to -recurse -Force

###### Create an application pool and ensure it is using .NET 4   #######
    New-WebAppPool -Name $name -Force
    #To Get The Web Application Object again use the following statement ####$webPool = Get-Item IIS:\AppPools\$name
    $webPool = Get-Item IIS:\AppPools\$name
    $webPool.managedRuntimeVersion = "v4.0"
    $webPool.managedPipelineMode = "Classic"
    $webPool.processModel.loadUserProfile =$true
    $webPool.processModel.logonType = "LogonBatch"
    #If Identity type is ApplicationPoolIdenetity/NetworkService/LocalService/LocalSystem uncomment
    #the Line bellow and comment out the other identityType="SpecificUser,UserName and Password lines
    #$webPool.processModel.identityType = "ApplicationPoolIdentity"
    $webPool.processModel.identityType = "SpecificUser"
    $webPool.processModel.userName = "Gtank1"
    $webPool.processModel.password = "Password1"
    $webPool | Set-Item

    # Create Website
    New-WebSite -Name $name -Port 80 -HostHeader $name -PhysicalPath $physicalPath -Force
    #To Get The Web Site Object again use the following statement #### $WebSite = Get-Item IIS:\Sites\$name
    # Set the Application Pool
    Set-ItemProperty IIS:\Sites\$name ApplicationPool $name
    #Turn on Directory Browsing
    Set-WebConfigurationProperty -filter /system.webServer/directoryBrowse -name enabled -Value $true -PSPath IIS:\Sites\$name
    #Only add the default document if it already hasn't been added
    $DefaultDocument = "Accounts/Default.aspx"
    $ExistingDefaultDocuments = get-webconfigurationproperty -filter /system.webServer/defaultDocument -name files -Location $name #Retrieve all the default documents
    $DefDocFound = $ExistingDefaultDocuments.Collection | where {$_.value -eq $DefaultDocument} #look for the default document that we want to add
    if(!$DefDocFound) {
            #To add the default document globally remove the -Location parameter
            Add-webconfigurationproperty -filter /system.webServer/defaultDocument -name files -value @{value=$DefaultDocument} -Location $name
            #remove-webconfigurationproperty /system.webServer/defaultDocument -name files -atElement @{value=$DefaultDocument} -Location $name
    }
    #Add-WebConfiguration -filter /system.webServer/defaultDocument/files -name enabled -location $name -value @{value="default.aspx"}
    # Update Authentication
    Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/AnonymousAuthentication -name enabled -value true -location $name
    Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/windowsAuthentication -name enabled -value false -location $name
    Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/basicAuthentication -name enabled -value false -location $name
#The code beolow will actually delete the Application pool and Web site that has been created above.
#NOTE:  You must delete the site and application pools that are retrieved from the Servermanager object
#       instead of using get-item otherwise you will get the following error "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."
#
<#
   [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
   $iis = New-Object Microsoft.Web.Administration.ServerManager
   Remove-Item $to -recurse -Force
   $WebSite = $iis.Sites["WebApp"]
   $webPool = $iis.ApplicationPools["WebApp"]
   $WebSite.Delete()
   $webPool.Delete()
#>

No comments:

Post a Comment