-Replace strange output

This topic contains 6 replies, has 3 voices, and was last updated by  Mark Prior 6 months ago.

  • Author
    Posts
  • #68922

    Mark Prior
    Participant

    ok so i thought this would work but im getting a strange result

    so i have

    
    $DBS = Get-ChildItem "C:\ExchangeDatabases" -Recurse | where {$_.extension -eq ".db"} | select fullname
    

    producing the below $dbs

    
    $DBs
    
    ##which looks like
    #FullName                                 
    #--------                                 
    #C:\ExchangeDatabases\eur_db01\eur_db01.db
    #C:\ExchangeDatabases\eur_db01\eur_db04.db
    #C:\ExchangeDatabases\eur_db01\eur_db05.db
    #C:\ExchangeDatabases\eur_db01\eur_db07.db
    #C:\ExchangeDatabases\eur_db04\eur_db01.db
    #
    
    #now i want to replace the ".db" with ".yes"
    
    $output3 = foreach ($DB in $DBs)
    {
        $log = $db -replace".db", ".yes" 
    
          [PSCustomObject]@{
                            
                            6 = $log
    
                            }
    }$output3
    
    

    but my output shows like this

    @{FullName=C:\ExchangeDatabases\eur.yes01\eur.yes01.yes}
    @{FullName=C:\ExchangeDatabases\eur.yes01\eur.yes04.yes}
    @{FullName=C:\ExchangeDatabases\eur.yes01\eur.yes05.yes}
    @{FullName=C:\ExchangeDatabases\eur.yes01\eur.yes07.yes}
    @{FullName=C:\ExchangeDatabases\eur.yes04\eur.yes01.yes}
    

    why is it replacing the "_db" in the string aswell as the ".db", aslo i want to get rid of the @{Fullname=}

  • #68925

    Mark Prior
    Participant

    Found it

    using the $ in ".db"

    
    $log2 = $db -replace ".db$", ".yes"
    
    
  • #68928

    Max Kozlov
    Participant

    Use single quotes for replacement if you haven't plans for $variables in replace

  • #68929

    Olaf Soyk
    Participant

    May I suggest some changes?

    $DBS = Get-ChildItem 'c:\ExchangeDatabases' -Filter *.db -Recurse 
    
    $output = foreach ($DB in $DBs)
    {
        [PSCustomObject]@{
            'Logfile' = Join-Path -Path $DB.Directory -ChildPath ($DB.BaseName + ".log")
        }
    }
    $output
  • #68935

    Mark Prior
    Participant

    Many thanks both for suggestions 🙂

  • #68962

    Max Kozlov
    Participant

    ...and... '.' in regex matches any symbol, if you want match only real period symbol you should use '\.'. that the cause of you initial problem

  • #69018

    Mark Prior
    Participant

    Thanks Max, Regex is a black art 🙂

You must be logged in to reply to this topic.