Web Config Modifications using PowerShell

This script and its data xml file can be used to make modifications to a Web Applications web config file across all SharePoint Servers.

4.1 Star
2,777 times
Add to favorites
E-mail Twitter del.icio.us Digg Facebook
  • xception calling "ApplyWebConfigModifications" with "0" argument(s): "The '[' character, hexadecimal value 0x5B, cannot be included in a name."
    1 Posts | Last post September 25, 2013
    • Not working with that simple schema.
      <?xml version="1.0" encoding="utf-8"?> 
              <value>add key="tpm" value="*"</value> 
  • Backing up the original config
    1 Posts | Last post July 09, 2012
    • Hi Simon,
      Going from memory, I believe that IIS takes back-ups of the config file in each web application.  On the off-chance I'm wrong, would it be worth amending the script to take a back-up of the web.config before any changes are made?
  • SPWebApplication doesn't have a method named ApplyWebConfigModifications
    7 Posts | Last post March 21, 2012
    • Microsoft says (http://goo.gl/K1aJf) to use SPWebService which does have a method called ApplyWebConfigModifications. I am guessing from your code that $webapp.Parent is supposed to return the SPWebService object but this is not happening on my box. Any ideas about what might be the problem? 
    • Can you include a simple of you powershell script? If the Parent object is not null it should have access to the ApplyWebConfigModifications method.
    • I'd be happy to provide the entire script but there doesn't seem to be enough space here. The pertinent bits are:
         $webapp = Get-SPWebApplication $WebAppToConfig.url
         # I also tried $webapp.WebService.ContentService but this always came up null
         $websvc = @($webapp.Farm.Services | where-object { $_.TypeName -eq "Microsoft SharePoint Foundation Web Application" } ) | Select-Object -First 1
         Write-Host "`nFound the webapp with the name" $webapp.Name
         Write-Host "`nat the provided URL" $WebAppToConfig.url
         DeleteSections $websvc $webapp $modificationOwner $false
         # loop through to find configs here
         # at the end of the loop create the config
         $configChildNode = CreateConfigChildNode $modificationOwner $type $path $name $value 0
         # and add it to the webapp's WebConfigModifications
         # I don't want to add the config to ALL applications; just this one
      # after we've added all of the modifications for a given webapp, apply them
      # ==> this wouldn't been where I would have done 
      # $webapp.Parent.ApplyWebConfigModifications 
      # but when I did I received an error: 
      # Method invocation failed because [Microsoft.SharePoint.Administration.SPWebApplication] 
      # doesn't contain a method named 'ApplyWebConfigModifications'.
      # So I Googled the issue and found another solution that acquires the websvc as I did above
      # and uses it thusly
      # However this results in a different error:
      #    Exception calling "ApplyWebConfigModifications" with "0" argument(s): 
      #    "Expression must evaluate to a node-set.
      # which some seemingly knowledgeable people attribute to config errors (which I assume
      # means errors in the configs I am trying to add) but even when I try to add a single
      # node I still get this error so I am currently stuck.
    • Well I rerun my powershell script that I included and everything ran correctly. I did notice you are missing the '()' from the end of the $webapp.Parent.ApplyWebConfigModifications method call. Are you running the script on the actual sharepoint server? What edition of SharePoint 2010 are you using and are you running it as a Farm and how many servers are in the Farm. My script has not been tested in a multi farm environment. Also, if you are not using a Powers shell program like PowerGUI, I strongly suggest you use it - its free and have built in intellisense and the scripts can be debugged.
    • Based on the MSDN documentation, I don't understand how this call would work since $webapp.Parent should return SPFarm as the ultimate object and ApplyWebConfigModifications is not a method on that object either. Is there some hidden alias at work here?
      The missing parens in my post were a result of pasting my code (which I noticed unavoidably resulted in almost unreadable code); my original code did have the final parentheses.
      I am using the script on a single farm deployed to a single virtual machine on a virtual network. I am using SharePoint 2010 Server. I used the Powershell ISE to develop the script and I am positive that the error came with the call to $webapp.Parent.ApplyWebConfigModifications().
      Thanks for publishing your code; it gave me a start at least if not a finish.
    • Sorry I could not help more. If I were you I would still download and install PowerGUI.
    • I never tried using the powershell but using object model (SPWebConfigModification Class) was Okay.
      ScriptingGuy1 has a blog on the same. "http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/14/use-powershell-to-script-changes-to-the-sharepoint-web-config-file.aspx"
  • Can you provide an example of how you would do a SafeControl
    4 Posts | Last post December 13, 2011
    • I get an error that it does not allow "=" character in a name.
              <value>Assembly="CFNPortalFramework, Version=, Culture=neutral, PublicKeyToken=911ec5180054c111" Namespace="Portal.Framework.PortalChannelWebpart.WebPartPages.MobileAdapters" TypeName="*" Safe="True" SafeAgainstScript="False"</value>
      You code and examples have been very helpful especially with my appSettings, thanks! I have a similar issue with 2 of my appSettings because they contain &lt; and &gt;. Any way to get around these errors?
    • Of course, I figured out one of these issues right after I posted the question. It appears to me that the name attribute does not need to look similar to the value attribute, it just needs to be unique. In my case, I simply removed the version, culture, etc from the name attribute but left them in the value attribute.
      My second issue, with the &lt; and &gt; escape codes still throws an error when it sees these in the value attribute - so if anyone has an idea on solving that one please post!
    • Are you encoding the '<' and the '>' as '&lt;' and '&gt;' ? Can you give me an example. I don't see them in what you posted.
    • You also need to change the start of the value from "Add ..." to "SafeControl Add ..."