Look through file for text and pull string that comes afterwards

Welcome Forums General PowerShell Q&A Look through file for text and pull string that comes afterwards

This topic contains 3 replies, has 4 voices, and was last updated by

Al
 
Participant
1 year, 12 months ago.

  • Author
    Posts
  • #65173

    Participant
    Points: 0
    Rank: Member

    Hi guys,

    I have a 2 folded issue. I have a file in my sites that contains info such as Database. I am trying to use that information to create a script that will look to the file, grab the db name and then input it into a script to do a DB backup on that database.

    I am new to powershell but have found Select-String:

    Select-String -Path \\server\d$\path\init.asp -Pattern "USERDB ="

    The result is:
    \\vs25\d$\path\init.asp:12: USERDB = "DB_name"

    I just need to grab the DB_name, no quotes, and then place that into a script to backup said DB.
    Any help would be greatly appreciated.

    Thank you!

  • #65175

    Participant
    Points: 17
    Rank: Member
    $file = Get-ChildItem \\server\d$\path\init.asp
    switch -Regex -File $file {
       'UserDB = "(.*)"' {$dbname = $Matches[1]}
    }
    # Example backup
    Backup-SqlDatabase -ServerInstance "Computer\Instance" -Database $dbname -BackupFile "\\fileserver\$dbname.mdf"
    
  • #65199

    Participant
    Points: 1
    Rank: Member

    Similarly you can:

    $dbname = (Select-String -Path "\\server\d$\path\init.asp" -Pattern 'USERDB = "(.*)"').Matches.Groups[1].Value
    Backup-SqlDatabase -ServerInstance "Computer\Instance" -Database $dbname -BackupFile "\\fileserver\$dbname.mdf"
  • #65259
    Al

    Participant
    Points: 0
    Rank: Member
    $path=... # your path
    $patt='^.+"(.+)".*$'
    
    $dbname=(sls 'USERDB\s*=' $path).line -replace $patt,'$1'
    

The topic ‘Look through file for text and pull string that comes afterwards’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort