Change a web.config connectionstring error

This topic contains 6 replies, has 3 voices, and was last updated by  Mike Chacker 3 years, 10 months ago.

  • Author
  • #17658

    Mike Chacker

    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.


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

    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
    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.....

  • #17659

    Don Jones

    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.

  • #17661

    Mike Chacker

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

  • #17673

    Adam Bertram

    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


  • #17695

    Mike Chacker

    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.

  • #17696

    Don Jones

    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.

  • #17697

    Mike Chacker

    When I asked the infrastructure guys about it they said it was just how scansafe categorized 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.

You must be logged in to reply to this topic.