Thursday, February 14, 2013

PowerShell : Complete script to setup a test web app and apply config changes

This script does the following (variables ommitted)
* Setup environment required
* Delete web application if exists already
* Creates web application with forms and windows authentication configured, with claims enabled
* Sets membership and role provider objects
* Changes default signin page so that custom sign in page is in effect.
* Removes wildcard managed path so that sub sites cannot be created.
* Disables custom errors for the SharePoint application
* Disables custom errors for application pages
* Configures membership and role provider entries as required
* Configures appSetttings section with serviceURL required
* Enables ASP.NET sessions in SharePoint application
* Updates SecurityTokenService with member/role providers
* Reset IIS
* Warmup SharePoint
* Call DeploySolutions to deploy and activate features
* Add PDF Mime-type so that PDF files are opened in browser for document libraries
* Uploads necessary files to a document library


# Call functions
.$CurrentFolder"\Functions.ps1"


# SharePoint cmdlets
cls
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Progress -Activity "Setting up environment" -Status "Loading Modules"
Write-Progress -Activity "Setting up environment" -Status "Setting up Variables ...."
Write-Host -NoNewline "Setting up Variables...." -foregroundcolor "Cyan"
# Call the Function to setup environment and variables
SetupEnvironment
Write-Host "Done." -ForegroundColor Green

#Remove a web application
Write-Progress -Activity "Initial cleanup...." 
Write-Host "Initial cleanup...."$SiteCollectionURL -foregroundcolor "Cyan"

# ValidateURL $SiteCollectionURL
if ($WebApp -ne $null){
    try
    {
        Write-Host -NoNewline "Deleting Web Application at" $SiteCollectionURL"...." -ForegroundColor "Cyan"
        #Web application exists. So need to delete.
        Remove-SPWebApplication -identity $SiteCollectionURL -DeleteIISSite -RemoveContentDatabases -Confirm:$false
        Write-Host "Done."  -ForegroundColor "Green"
    }
    catch 
    {
        Write-Host "Errors while initial cleanup...Please contact Administrator..." -ForegroupColor "Red"
        Write-Host $error
        Break;
    }
}
Write-Host "Initial cleanup....Done." -foregroundcolor "Green"

try
{
    #Create a web application
    Write-Progress -Activity "Web Application" -Status "Creating web application with forms and windows authentication providers...."
    Write-Host -NoNewline "Web Application. Creating web application with forms and windows authentication providers...." -foregroundcolor "Cyan"
    $WebApp = New-SPWebApplication -Name $WebAppName -AuthenticationProvider $winap, $fbaap -Port $WebAppPort -HostHeader $WebAppHostHeader -AllowAnonymousAccess  -URL ("http://xxxxx") -SignInRedirectURL $RedirectLoginPage -ApplicationPool $WebAppAppPool -ApplicationPoolAccount (Get-SPManagedAccount $WebAppAppPoolAccount) -DatabaseName $WebAppDatabaseName -DatabaseServer $WebAppDatabaseServer
    Write-Progress -Activity "Web Application" -Status "Enabling for claims..."
    $WebApp.UseClaimsAuthentication = $true
    $WebApp.Update()
    Write-Host "Done." -foregroundcolor "Green"
}
catch 
{
    Write-Host "Web Application could not be created. Please contact Administrator." -ForegroundColor Red
    Write-Host $error
    Break
}

#Create site collection
Write-Progress -Activity "Site collection" -Status "Creating...."
Write-Host -NoNewline "Site Collection. Creating at" $SiteCollectionURL"...." -foregroundcolor "Cyan"
$template = Get-SPWebTemplate $SiteCollectionTemplate
$SiteCollection = New-SPSite -Url $SiteCollectionURL -Template $template -OwnerAlias $SiteCollectionOwner
Write-Progress -Activity "Site collection" -Status "Done."
Write-Host "Done." -foregroundcolor "Green"

Write-Host ""
Write-Host ""

try
{
    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")  
    $WebApp = Get-SPWebApplication $SiteCollectionURL
    Write-Host -NoNewline "Setting up user policies on SharePoint site...." -ForegroundColor "Cyan"
    $UserName = "c:0(.s|true" #Everyone
    $UserDisplayName = "Everyone"
    $Policy = $WebApp.Policies.Add($UserName, $UserDisplayName)
    $PolicyRole = $WebApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl)
    $Policy.PolicyRoleBindings.Add($PolicyRole)
    $WebApp.Update()
    Write-Host "Done." -ForegroundColor "Green"
}
catch 
{
    Write-Host "Error permissioning SharePoint site. Please contact Administrator." -ForegroundColor Red
    Write-Host $error
    Break
}

# Defining managed paths to explicit so that sub sites could not be created
# Remove sites default managed path
Remove-SPManagedPath "sites" -WebApplication $WebApp.Url -Confirm:$false
Write-Host "Sub sites managed path removed." -f Green

try
{
    Write-Progress -Activity "Making updates to Web.Config in SharePoint site...." -Status "Disabling custm errors..."
    Write-Host "Making updates to Web.Config in SharePoint site...." -foregroundcolor "Cyan"
    Disable-SPCustomErrors
    Write-Host "Creating membership/roles providers..." -foregroundcolor "Cyan"
    $WebAppZone = $WebApp.AlternateUrls[0].UrlZone
    $IISSettings = $WebApp.IisSettings[$WebAppZone]
    $WebAppConfigPath = $IISSettings.Path.ToString() + "\Web.Config"
    UpdateMemberAndRoleSections($WebAppConfigPath)
    Write-Host "Membership/roles providers created successfully." -foregroundcolor "Green"
    Write-Host "Updating appSettings section...." -NoNewline -ForegroundColor Cyan
    Update-AppSettingsSection
    Write-Host "Done." -ForegroundColor Green
    Write-Host "Enabling ASP.NET sessions...." -NoNewline -ForegroundColor Cyan
    Enable-ASPSessions
    Write-Host "Done." -ForegroundColor Green
    Write-Host "Making updates to Web.Config for SharePoint site completed successfully." -foregroundcolor "Green"
}
catch
{
    Write-Host "Error disabling custom errors in "$WebAppConfigPath -ForegroundColor "Red"
    Write-Host $error
    Break 
}

Write-Host ""
Write-Host ""

try
{
    Write-Host "Making updates to Web.Config in LAYOUTS folder...." -foregroundcolor "Cyan"
    Disable-SPCustomErrors-LAYOUTS-Folder
    Write-Host ""
    Write-Host ""
}
catch
{
    Write-Host "Error disabling custom errors in LAYOUTS folder."
}

Write-Host "Making updates to Web.Config in SecurityToken folder...." -foregroundcolor "Cyan"
UpdateSecurityTokenServiceWebConfig
Write-Host "Updates to Web.Config in SecurityToken folder. Membership/Roles created successfully." -foregroundcolor "Green"
Write-Progress -Activity "Updates to Web.Config" -Status "Membership/Roles created successfully."

# Reset IIS and then fire up warm up script
Write-Host "Resetting IIS...." -NoNewline -ForegroundColor Cyan
iisreset
Write-Host "Done." -ForegroundColor Green
Write-Host "Warming up SharePoint...." -NoNewline -ForegroundColor Cyan
.$CurrentFolder"\WarmupScript.ps1"
Write-Host "Done." -NoNewline -ForegroundColor Green

# Deploy solutions
.$CurrentFolder"\DeploySolutions.ps1"

Write-Host "Checking for PDF mime-type...." -ForegroundColor Cyan -NoNewline
If ($WebApp.AllowedInlineDownloadedMimeTypes -notcontains "application/pdf")
{
    Write-Host -ForegroundColor White "Adding PDF MIME Type..."
    $WebApp.AllowedInlineDownloadedMimeTypes.Add("application/pdf")
    $WebApp.Update()
    Write-Host -ForegroundColor White "Added and saved."
} 
Write-Host "Done." -ForegroundColor Green

# Upload files necessary into document libraries
Write-Host "Uploading necessary documents...." -NoNewline -ForegroundColor Cyan
# Upload required documents to document libraries
$Web = Get-SPWeb $SiteCollectionURL
$List = $Web.GetFolder($DocumentLibraryName)

if ($List -ne $null)
{
    $FullLocalFileName = $DocumentsLocalPath + $DocumentLibraryFile1 

    # Read the file
    $File = Get-ChildItem $FullLocalFileName 

    # Add file to the collection
    $UploadedFile = $Files.Add($DocumentLibraryName + "/" + $DocumentLibraryFile1, $File.OpenRead(),$true)
}
Write-Host "Done." -ForegroundColor Green


No comments:

Post a Comment