locked
Change databases connection and testing RRS feed

  • Question

  • I have case

    We move databases to new server

    I have to search on all server after the databases connection

    This stament find all the databases connection on the server and a overview  

    Get-ChildItem C:\work\Newfolder -recurse -exclude  *.dll, *.pdb, *.csproj, *cache | Select-String -pattern "Peter" -Context 3

     

    And this stament change all

    Get-ChildItem \\path\to\folder -Filter '*.config' | foreach {

    $text = Get-Content $_.FullName

    $text = $text -replace 'old1','new1'

    $text = $text -replace 'old2','new2'

    $text | Set-Content $_.FullName

    }

    But I also like to test the database connection in powershell


    I will like to have the resulted from

    Get-ChildItem C:\work\Newfolder -recurse -exclude  *.dll, *.pdb, *.csproj, *cache | Select-String -pattern "Thedatabase" -Context 3

    In array so I can create a open close, but I cannot find out how set the result in array so I take the result and find the connection

    Must I really write to a file and read again

    Or are there a smarter way ?

    .

    .

    C:\Projects\X\x\X\Release\1.2.0 x\x\ConfigFiles\KMDmiljoer\ITC_Web.config:10:                    <add key="SqlLogConnStr" value="SERVER=xxxxx; Data

    Source=xxx,1440; UID=xxxxxx; PWD=xxxxxx; DATABASE=Thedatabase; NETWORK LIBRARY=DBMSSOCN"/>

    .

    .

    I'm taken on a Christmas holiday now,  so iT can take be a little long time to answer

    Friday, December 22, 2017 10:33 AM

Answers

  • Hi ,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,

    Candy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, December 26, 2017 8:42 AM

All replies

  • You can use XML to do all of the list of items you are requesting however we cannot do this for you.

    This Forum is for Scripting Question Rather than script requests

    Script Gallery.

    Learn PowerShell  

    Script requests


    \_(ツ)_/

    Friday, December 22, 2017 10:48 AM
  • Hi again

    It was not meaning you should write it for me, but I cannot find out to get the output from Get-ChildItem C:\work\Newfolder -recurse -exclude  *.dll, *.pdb, *.csproj, *cache | Select-String -pattern "Thedatabase" -Context 3 into an array and the connection may not be in an XML file, We have also old also aps

     

    Friday, December 22, 2017 11:27 AM
  • The code you are trying to use cannot be bused to do what you want. Putting it in an array will not help. You must select all files then open them with Get-Content and then replace any strings. ALL .config files are XML files. If you have many kinds of files you will have to write code that accounts for that.


    \_(ツ)_/

    Friday, December 22, 2017 11:31 AM
  • $a = Get-ChildItem C:\work\Newfolder -recurse -exclude *.dll, *.pdb, *.csproj,
    *cache | Select-String -pattern "Thedatabase" -Context 3
    # In this case, $a should be an array...

    # Convert xml file to object and then back to xml file:
    $a = [xml](get-content .\file.xml)
    $a.Save('file.xml')
    • Edited by JS2010 Monday, December 25, 2017 4:06 PM
    Friday, December 22, 2017 5:43 PM
  • Hi ,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,

    Candy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, December 26, 2017 8:42 AM