Monday, July 18, 2016

PowerShell v2 Function Add WebConfigAppSetting

,
Back in March I posted about a means to write down to web.config files
PowerShell (v2) - Adding AppSettings Elements to Web.config
After revisiting my post I realized it was pretty much wrong and completely useless.  So, I set about to rewrite it with the help of a few folks on the forums.
How can I discard output of pipeline without using Out-Null?
In the end I came up with this:
functionAdd-WebConfigAppSetting
{
     [CmdletBinding()]
     param(
           [Parameter(
                Mandatory = $true
           )]
           [String]
           [ValidateNotNullOrEmpty()]
           $Key,
          
           [Parameter(
                Mandatory = $true
           )]
           [String]
           [ValidateNotNullOrEmpty()]
           $Value,

           [Parameter(
                Mandatory = $true
           )]
           [String]
           [ValidateNotNullOrEmpty()]
           $WebConfigPath
     )

     # Configure function variables
     $timestamp = (Get-Date -Format yyyyMMddHHmmss)

     # Clear variables to prevent reuse
     $appSettings = $node = $xml = $null

     # Output status to host
     Write-Output "Loading file contents."

     # Load content of web.config
     $xml = [xml](get-content($webconfigpath))

     # Backup web.config
     if(-not(Test-Path "$webconfigpath.$timestamp"))
     {
           # Output status to host
           Write-Output "Creating backup."
          
           # Save file
           $xml.Save("$webconfigpath.$timestamp")
     }

     # If appSettings element does not exist
     if($xml.SelectSingleNode(//configuration/appSettings) -eq $null)
     {
           # Output status to host
           Write-Output The appSettings node does not exist. Adding it now.
          
           # if child nodes do not exist create it
           $appSettings = $xml.CreateElement("appSettings")
          
           # Output status to host
           Write-Output "The appSetting ($($key)/$($value)) does not exist. Adding it now."
          
           # add new element called add to //configuration/appSettings
           $node = $xml.CreateNode(element,add,)
          
           # Set attributes for new appSetting
           $node.SetAttribute(key,$key)
           $node.SetAttribute(value,$value)

           # Add $node to //configruation/appsetting element
           [Void] $appSettings.AppendChild($node)
          
           # Add $appSetting node to //configuration element
           [Void] $xml.configuration.AppendChild($appSettings)

           # Output status to host
           Write-Output "Saving changes." 
          
           # Save changes to file
           $xml.Save($webconfigpath)
     }
     else
     {
           # Output status to host
           Write-Output "The //configuration/appSettings element exists. Checking for children nodes."
          
           # Gather //configuration/appSettings data
           $appSettings = $xml.configuration[appSettings]
          
           # Test to see if element has child nodes
           if($appSettings.HasChildNodes)
           {
                # Output status to host
                Write-Output "The appSettings elements has children. Checking arguments against existing values."
          
                # Test to see if key exists.        
                if($appSetting = ($appSettings.ChildNodes | Where {$_.key -eq $key}))
                {
                     # Output status to host
                     Write-Output "The element ($($key)) exists. Comparing values."
               
                     # Test to see if value matches
                     if($appSetting.value -eq $value)
                     {
                           # Value matches. Output status to host.
                           Write-Output "The key/value arguments are already set correctly."
                     }
                     else
                     {
                           # Value does not match. Attempt to update and output status to host.
                     &

0 comments to “PowerShell v2 Function Add WebConfigAppSetting”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger