El TecnoBaúl de Kiquenet

Kiquenet boring stories

Modify machine.config (config files) using Powershell

Posted by kiquenet en 22 enero 2015

ConfigurationManager

$config = [System.Configuration.ConfigurationManager]::OpenMachineConfiguration() 

"{0,-25}     {1,25}"  -f "   Name", "   Allow Definition"
"{0,-25}     {1,25}"  -f "   —-", "   —————-"
$i = 0 

foreach ($section in $config.Sections) 

     "{0,-25}     {1,25}" -f $section.SectionInformation.Name,$section.SectionInformation.AllowExeDefinition 
     $i++ 

"Total number of sections: {0}" -f $i

get-content and DocumentElement

$xml = [xml](get-content $machineConfig)
$xml.Save($machineConfig + "_$currentDate")
$root = $xml.get_DocumentElement()
$system_web = $root."system.web"
if ($system_web.machineKey -eq $nul) {
$machineKey = $xml.CreateElement("machineKey")
$a = $system_web.AppendChild($machineKey)
}
$system_web.SelectSingleNode("machineKey").SetAttribute("validationKey","$validationKey")
$system_web.SelectSingleNode("machineKey").SetAttribute("decryptionKey","$decryptionKey")
$system_web.SelectSingleNode("machineKey").SetAttribute("validation","$validation")
$a = $xml.Save($machineConfig)

XmlDocument

if (!$origXml.configuration.’system.serviceModel’.bindings)
{
    $tempXmlDoc = new-object System.Xml.XmlDocument
    $tempXmlDoc.LoadXml($newXml)
    $newNode = $origXml.ImportNode($tempXmlDoc.DocumentElement, $true)
    $origXml.configuration.’system.serviceModel’.AppendChild($newNode)
}

Sources:
http://sunauskas.com/blog/edit-config-file-with-powershell/
http://geekswithblogs.net/nharrison/archive/2011/05/25/updating-the-machine.config–with-powershell.aspx

http://pshscripts.blogspot.com.es/2009/02/get-machineconfig.html
http://jeffgraves.me/2012/06/05/read-write-net-machine-key-with-powershell/
http://stackoverflow.com/questions/10342657/how-can-i-add-a-section-to-a-web-config-using-powershell

Deja un comentario