Thursday 22 May 2014

Powershell script to create a webapplication

$ErrorActionPreference = "Stop"

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0
$webAppUrl = read-host "Enter WebApplication URL"
$appPoolUserName = read-host " Enter application pool user name"
$contentDatabaseName = read-host "Enter Content database name based on env specific(ex:UrlDev)"

function Main()
{

    If ($webAppUrl -eq $null)
    {
        $webAppUrl = "https://www.testLink.com"
    }

    Write-Host "Creating Web application ($webAppUrl)..."

    $hostHeader = $webAppUrl.Substring("https://".Length)

    $webAppName = "SharePoint - " + $hostHeader + "80"

    $membershipProviderName = "membership"
    $roleProviderName = "roleManager"
    $contentDatabaseName = "WSS_Content_"+$contentDatabaseName
    $appPoolName = $webAppName

    Write-Debug "hostHeader: $hostHeader"
    Write-Debug "webAppName: $webAppName"
    Write-Debug "appPoolName: $appPoolName"
    Write-Debug "appPoolUserName: $appPoolUserName"
    Write-Debug "contentDatabaseName: $contentDatabaseName"

    Write-Debug "Get service account for application pool ($appPoolUserName)..."
    $appPoolAccount = Get-SPManagedAccount -Identity $appPoolUserName `
        -Debug:$false -EA 0

    If ($appPoolAccount -eq $null)
    {
        Write-Host "Registering managed account ($appPoolUserName)..."

        Write-Debug "Get credential ($appPoolUserName)..."
        $appPoolCredential = Get-Credential $appPoolUserName

        $appPoolAccount = New-SPManagedAccount -Credential $appPoolCredential `
            -Debug:$false
    }

    $windowsAuthProvider = New-SPAuthenticationProvider -Debug:$false
    $formsAuthProvider = New-SPAuthenticationProvider `
        -ASPNETMembershipProvider $membershipProviderName `
        -ASPNETRoleProviderName $roleProviderName `
        -Debug:$false

    $authProviders = $windowsAuthProvider, $formsAuthProvider

    $webApp = New-SPWebApplication -Name $webAppName -AllowAnonymousAccess -SecureSocketsLayer `
        -ApplicationPool $appPoolName -AuthenticationMethod "NTLM" `
        -ApplicationPoolAccount $appPoolAccount -Url $webAppUrl -Port 443 `
        -AuthenticationProvider $authProviders -HostHeader $hostHeader `
        -DatabaseName $contentDatabaseName `
        -Debug:$false

    Write-Host -Fore Green "Successfully created Web application ($webAppUrl)."
Remove-PSSnapin Microsoft.SharePoint.PowerShell
}

Main

No comments:

Post a Comment