Wednesday, January 30, 2013

SharePoint 2013 : PowerShell : Creating membership and role provider entries in web.config

The following script creates membership and role manager entries in web.config required in case of configuring SharePoint web application using Forms Authentication. entries will be created if not already found.
 

Function UpdateMemberAndRoleSections([string] $ConfigurationFilePath){ 

# Get the path to the config file from $WebApp
# Prepare variables required to extract Web.Config from SPWebApplication

SetupEnvironment

 $AssemblyFullSignature = "XXXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxx" 

Write-Host -NoNewline "Loading Web.Config as XML in " $ConfigurationFilePath "...." -ForegroundColor Cyan
 [xml]$xml = Get-Content $ConfigurationFilePath
 Write-Host "Done." -ForegroundColor Green 

# Check for configuration section if exists, if not create one
 $systemwebNode = $xml.SelectSingleNode("//configuration/system.web"
 

if ($systemwebNode -eq $null){

$root = $xml.get_DocumentElement();         

$systemwebNode = $xml.CreateNode('element',"system.web","") 

$xml.configuration.AppendChild($systemwebNode)      


#Apply to Web.Config

 $xml.Save($ConfigurationFilePath)}



# Check for membership section if exists, if not create one

 $membershipNode = $xml.SelectSingleNode("//configuration/system.web/membership")

 

if ($membershipNode -eq $null){

$root = $xml.get_DocumentElement();         

$membershipNode = $xml.CreateNode('element',"membership","") 

$systemwebNode = $xml.SelectSingleNode("//configuration/system.web")

 $systemwebNode.AppendChild($membershipNode)


#Apply to Web.Config

 $xml.Save($ConfigurationFilePath)}

#Check for membership/providers section if exists, if not create one 
$providersNode = $xml.SelectSingleNode("//configuration/system.web/membership/providers")

 if ($providersNode -eq $null){

$root = $xml.get_DocumentElement(); 

$providersNode = $xml.CreateNode('element',"providers","") 

$membershipNode = $xml.SelectSingleNode("//configuration/system.web/membership")

$membershipNode.AppendChild($providersNode)

        

#Apply to Web.Config

 $xml.Save($ConfigurationFilePath)

}

 

# Check for membership provider in providers section

 $MembershipProvidersNode = $xml.SelectSingleNode("//configuration/system.web/membership/providers/add[@name='MembershipNameXXXX']")

    

if ($MembershipProvidersNode -ne $null){ 

Write-Host "Membership provider entry found in Web.Config. Exiting." -ForegroundColor Cyan

}

else

{       

Write-Host -NoNewline "Membership provider entry not found in Web.Config. So creating a new one...." 
-ForegroundColor Cyan

 

# Node does not exist. So create it

 $root = $xml.get_DocumentElement(); 

$providerNode = $xml.CreateNode('element',"add","") 

$providerNode.SetAttribute("name", "MembershipNameXXXX")

 $providerNode.SetAttribute("applicationName", "/")

$typeFullSignature = "MembershipProviderClassNameWithAssembly, " + $AssemblyFullSignature

 $providerNode.SetAttribute("type",$typeFullSignature)

 

$SystemWebNode =
$xml.SelectSingleNode("//configuration/system.web/membership/providers").AppendChild($providerNode)

 

#Apply to Web.Config

 

$xml.Save($ConfigurationFilePath)

 Write-Host "Done." -ForegroundColor Green

 }

# Check for roleManager section if exists, if not create one

 $roleManagerNode = $xml.SelectSingleNode("//configuration/system.web/roleManager")

 if ($roleManagerNode -eq $null){

$root = $xml.get_DocumentElement(); 

$roleManagerNode = $xml.CreateNode('element',"roleManager","") 

$systemwebNode = $xml.SelectSingleNode("//configuration/system.web")

 $systemwebNode.AppendChild($roleManagerNode)

 

#Apply to Web.Config      

$xml.Save($ConfigurationFilePath)

}    # Check for roleManager/providers section if exists, if not create one   

$providersNode = $xml.SelectSingleNode("//configuration/system.web/roleManager/providers")    

if ($providersNode -eq $null){   

$rroot = $xml.get_DocumentElement(); 

$providersNode = $xml.CreateNode('element',"providers","") 

$roleManagerNode = $xml.SelectSingleNode("//configuration/system.web/roleManager")

 $roleManagerNode.AppendChild($providersNode)

       

#Apply to Web.Config

$xml.Save($ConfigurationFilePath)

 }    # Check for role provider in providers section

$RoleProvidersNode = $xml.SelectSingleNode("//configuration/system.web/roleManager/providers/add[@name='RoleManagerNameXXXX']")

 

if ($RoleProvidersNode -ne $null){

 Write-Host "Role provider entry found in Web.Config. Exiting." -ForegroundColor Cyan

}

else

{

Write-Host -NoNewline "Role provider entry not found in Web.Config. So creating a new one...." 
-ForegroundColor Cyan


# Node does not exist. So create it

$root = $xml.get_DocumentElement(); 

$providerNode = $xml.CreateNode('element',"add","") 

$providerNode.SetAttribute("name", "RoleManagerNameXXXX")

 $providerNode.SetAttribute("applicationName", "/")

 $typeFullSignature = "RoleManagerClassNameWithAssembly, " + $AssemblyFullSignature

 $providerNode.SetAttribute("type",$typeFullSignature)

 

$SystemWebNode =
$xml.SelectSingleNode("//configuration/system.web/roleManager/providers").AppendChild($providerNode)

 Write-Host "Done." -ForegroundColor Green}   

  # Save changes

 #Apply to Web.Config

 

Write-Host -NoNewline "Saving Web.Config...." -ForegroundColor Cyan

 $xml.Save($ConfigurationFilePath)

Write-Host "Done." -ForegroundColor Green


}

No comments:

Post a Comment