none
Updating web.config entry in multiple front end servers

    Question

  • Hi,

    I am using a powershell to update an entry in the "App.settings" of the web.config. I have 3 front-end servers. When I run the powershell script, it is currently updating only one server, where the powershell is run. Is it possible to update all the front-end server web.config at once? I am using the below powershell script:

    function UpdateList($weburl)
    {
    
    Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue 
    $WebApp = Get-SPWebApplication -Identity $webUrl
    
    
    $WebAppZone = $WebApp.AlternateUrls[0].UrlZone    
    $IISSettings = $WebApp.IisSettings[$WebAppZone]    
    $WebAppConfigPath = $IISSettings.Path.ToString() + "\Web.Config"
    
    
    
    #Write-Host -NoNewline "(Update-AppSettingsSection)Loading Web.Config as XML in "$WebAppConfigPath"...." -ForegroundColor Cyan    
    
    [xml]$xml = Get-Content $WebAppConfigPath    
    Write-Host "Done" -ForegroundColor Green   
      
    # Check if appSettings node exists    
    $appSettingsNode = $xml.SelectSingleNode("//configuration/appSettings")    
    
    if ($appSettingsNode -eq $null)    
    {        
    Write-Host -NoNewline "appSettings node does not exist. Adding the node..." -ForegroundColor Cyan 
             
    # Node does not exist. So create it   
         
    $root = $xml.get_DocumentElement();                 
    $appSettingsNode = $xml.CreateNode('element',"appSettings","")            
    $configurationNode = $xml.configuration.AppendChild($appSettingsNode)        
    $appSettingsNode = $xml.SelectSingleNode("//configuration/appSettings")         
            
    #Apply to Web.Config        
    $xml.Save($WebAppConfigPath)        
    Write-Host "Done." -ForegroundColor Green    
    }      
    # Check for serviceURL    
    $serviceURLNode = $xml.SelectSingleNode("//configuration/appSettings/add[@key='CustomURL']")    
    if ($serviceURLNode -ne $null)    
    {        
    Write-Host -NoNewline "serviceURL entry found in Web.Config...." -ForegroundColor Cyan   
         
    # Delete the entry to recreate and update    
        
    $temp = $appSettingsNode.RemoveChild($serviceURLNode)        
    Write-Host -NoNewline "Node removed." -ForegroundColor Cyan
    }
    
    # Recreate the servieURL node   
     
    $root = $xml.get_DocumentElement();             
    $serviceURLNode = $xml.CreateNode('element',"add","")        
    $serviceURLNode.SetAttribute("key", "CustomURL")    
    
    $serviceURLNode.SetAttribute("value", "http://server/sites/sitename/" )  
      
    $appSettingsNode =  $xml.SelectSingleNode("//configuration/appSettings").AppendChild($serviceURLNode)     
    
    #Apply to Web.Config    
    $xml.Save($WebAppConfigPath)   
     
    Write-Host "Done." -ForegroundColor Green         
    Write-Host "appSettings section updated successfully." -foregroundcolor "Green" 
    
    
    Write-Host -f Green "Added entried to web.config file"
    
    }

    How to update multiple front end servers web.config entry?

    Thanks

    Monday, March 13, 2017 12:58 PM

Answers

  • Hi,

    Try to use Microsoft.SharePoint.Administration.SPWebConfigModification for your requirement.

    Here is the link for your reference.

    https://blogs.technet.microsoft.com/heyscriptingguy/2010/09/14/use-powershell-to-script-changes-to-the-sharepoint-web-config-file/

    Sample code:

    Add-PSSnapin Microsoft.SharePoint.PowerShell
    $wa = Get-SPWebApplication http://sp:12001
    $wa.WebConfigModifications
    # Create a new modification, add, and apply
    $mod = New-Object -TypeName Microsoft.SharePoint.Administration.SPWebConfigModification
    $mod.name = "add[@key='Test']"
    $mod.path = "/configuration/appSettings"
    $mod.value = "<add key='Test' value='value here' />"
    $mod.owner = "Test"
    $mod.sequence = 0
    $mod.type = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureChildNode
    $wa.WebService.WebConfigModifications.Add($mod)
    $wa.WebService.Update()
    $wa.WebService.ApplyWebConfigModifications()

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 14, 2017 2:09 AM

All replies