I’m processing a web.config file using XML and trying to remove certain appsetting add key/values entires. I use the following code below:
$config.configuration.appSettings.ChildNodes |
where {
$_.key -ne $null -and $commonAppSettings -contains $_.key
} |
foreach {
$config.configuration.appSettings.RemoveChild($_.Node)
}
$config.Save($configPath)
return $settings
The problem is when it attempt to execute the RemoveChild(), it throws the following error:
Exception calling "RemoveChild" with "1" argument(s): "Object reference not set to an instance of an object."
At C:\ws\coreServices\BuildProcessTemplates\Deployment\CoreDeploy.ps1:98 char:49
+ $config.configuration.appSettings.RemoveChild <<<< ($_.Node)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
How do I properly reference the childnode to remove it?
Not having the whole script, it's a little hard to see what is going wrong but the error message is telling you that $config.configuration.appSettings object is not initialized.
Take a look at this posting and see it gives you some context to help solve your problem:
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/1b440c30-485f-4136-b1e4-3daf5e200170