Change a web.config connectionstring error

Hello, I am new to powershell just watched the jumpstart videos two days ago. Being a former Linux admin/dev I really like powershell and I am very excited to get involved with the community.

Here is the problem I am trying to solve. I am creating a script to promote a web page from DEV to UAT and then Production if all goes well. So the first thing I am really trying to do is change the web.config file to set debug = false/true depending on some logic I will define. Then I want to change the SQL DB connection string to point to the target environment. The debug value works great.

The problem I am having is with the connection string.

CODE:

$webConfig = "C:\users\%user%\desktop\gsrs\web\web.config"
$doc = new-object System.Xml.XmlDocument
$doc.Load($webConfig)
$doc.get_DocumentElement()."system.web".compilation.debug = "true"
$doc.Save($webConfig)
$doc.get_documentelement().configuration.connectionstring = "test"

ERROR:
The property ‘connectionstring’ cannot be found on this object. Verify that the property exists and can be set.
At line:6 char:1

  • $doc.get_documentelement().configuration.connectionstring = “test”
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : PropertyNotFound
    
    
    

I have tried with a configuration.connectionstring.add
.name
and a few other variations.

I know this is something small and I am just missing it. Your help is greatly appreciated.

Thank you in advance…
Mike

I’ve often had more success in using an Xpath query to select a node, rather than trying to traverse the DOM tree. So, you’d query for the ConnectonString node, and then set it. I’m guessing now that whatever element you’re referencing isn’t the one you think you got.

Thanks Don. I will read the documentation on that and give it a try.

I’m with Don on this one. Once you get the hang of Xpath it’s a one-stop shop where you don’t really have to use any other method.

I pulled this from one of my scripts to give you an idea on the syntax

$Configuration.SelectSingleNode(“//ConfigurationItems/ConfigurationItem[@Name=‘SourceFolder’]”).Value

Thank you both. I was able to get this running last night. unfortunately I was unable to post because my companies web filter blocked this site due to spam issues. I was able to work with the infrastructure team to get this site added back to the approved list.

Thank you.

I’d be interested in knowing what the spam issues were, if you’re able to share. Apart from opt-in notifications from these forums, the site doesn’t send any email.

When I asked the infrastructure guys about it they said it was just how scansafe categorized powershell.org. I was able to get to it in the morning when I first asked this question. Then by that afternoon it was blocked. I had the admin fix it but still not sure on why cisco connect anywhere scansafe blocked it. I wish I could be more help.