Skip to content

Instantly share code, notes, and snippets.

@janikvonrotz
Last active September 17, 2019 08:07
Show Gist options
  • Save janikvonrotz/8403604 to your computer and use it in GitHub Desktop.
Save janikvonrotz/8403604 to your computer and use it in GitHub Desktop.
PowerShell: Create a new SharePoint Site with a custom web application and managed account and application pool #PowerShell #SharePoint
# modules
Import-Module ActiveDirectory
if((Get-PSSnapin 'Microsoft.SharePoint.PowerShell' -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin 'Microsoft.SharePoint.PowerShell'}
# new service account
$UserName = "SharePoint Service User Wiki"
$UserUPN = "sa-spwiki@vbl.ch"
$UserSam = "sa-spwiki"
$UserPassword = "pass."
if(-not (Get-ADUser -Filter {sAMAccountName -eq $UserSam})){
New-ADUser -Name $UserName -UserPrincipalName $UserUPN -SamAccountName $UserSam
Get-ADUser $UserSam | %{
Set-ADAccountPassword -Identity $UserSam -NewPassword (ConvertTo-SecureString -String $UserPassword -AsPlainText -Force) -Reset
Enable-ADAccount -Identity $_
Set-ADAccountControl -Identity $_ -PasswordNeverExpires $true -CannotChangePassword $true
Set-ADUser -Identity $_ -ChangePasswordAtLogon $false
$_
}
}
# new sp service account
$ManagedAccountName = $UserSam
$ManagedAccountPassword = $UserPassword
if(-not (Get-SPManagedAccount $ManagedAccountName)){New-SPManagedAccount -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ManagedAccountName,(ConvertTo-SecureString -String $ManagedAccountPassword -AsPlainText -Force))}
# New Applicaton Pool
$PoolName = "SP_wiki"
$PoolAccount = $UserSam
if(-not (Get-SPServiceApplicationPool $PoolName)){New-SPServiceApplicationPool -Name $PoolName -Account $UserSam}
# New SPWeb Application
$AppName = "SP_wiki"
$AppPort = 80
$AppUrl = "http://wiki.vbl.ch"
$AppHostHeader = "wiki.vbl.ch"
$AppDatabaseServer = "VBL_SHAREPOINT"
$AppDatabaseName = "SP_Content_wiki"
$AppPoolName = $PoolName
$AppPoolAccount = $UserSam
if(-not (Get-SPWebApplication $AppName)){New-SPWebApplication -Name $AppName -ApplicationPool $AppPoolName -ApplicationPoolAccount (Get-SPManagedAccount $AppPoolAccount) -HostHeader $AppHostHeader -Port $AppPort -URL $AppUrl -DatabaseServer $AppDatabaseServer -DatabaseName $AppDatabaseName}
# New SPSite
$SiteName = "Informatik Wiki"
$SiteUrl = $AppUrl
$SiteTemplate = "ENTERWIKI#0"
$SiteOwnerMail = "janik.vonrotz@vbl.ch"
$SiteOwnerAlias = "vonrotz"
if(-not (Get-SPSite -Identity $Siteurl)){New-SPSite -Url $SiteUrl -Template $SiteTemplate -Name $SiteName -OwnerEmail $SiteOwnerMail -OwnerAlias $SiteOwnerAlias}
# Import-Module ActiveDirectory
if((Get-PSSnapin 'Microsoft.SharePoint.PowerShell' -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin 'Microsoft.SharePoint.PowerShell'}
# add managed path
$WebApplication = "http://sharepoint.domain.ch"
$RelativeURLExplicit = "/itwiki"
New-SPManagedPath -RelativeURL $RelativeURLExplicit -WebApplication $WebApplication -Explicit
# add content database
$SPContentDatabaseName = "SP_Content_ITWiki"
New-SPContentDatabase -Name $SPContentDatabaseName -WebApplication $WebApplication
$SiteName = "Informatik Wiki"
$SiteUrl = "http://sharepoint.domain.ch/itwiki"
$SiteTemplate = "ENTERWIKI#0"
$SiteOwnerAlias = "SP1_Pool_Intranet"
$SiteSecondaryOwnerAlias = "SP1_Admin"
New-SPSite -Url $SiteUrl -Template $SiteTemplate -Name $SiteName -OwnerAlias $SiteOwnerAlias -SecondaryOwnerAlias $SiteSecondaryOwnerAlias -ContentDatabase $SPContentDatabaseName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment