Struggling with Find and Replace

This topic contains 11 replies, has 5 voices, and was last updated by  Stuart 10 months ago.

  • Author
    Posts
  • #63396

    Stuart
    Participant

    Hi Guys,

    I am trying to add quotes around the output of this script which is a UNC path. Baffled for hours. Here is the script:

    Copy-Item -Path "\\fileserv10\UserProfiles\$env:username.adatum\uProfile\UserDriveMappings.ini" -Destination "\\fileserv3\$env:username"
    
    $path = "\\fileserv3\$env:username\UserDriveMappings.ini"
    $word = "MapDrive"
    $replacement = "New-PSDrive –Name"
    $text = get-content $path 
    $newText = $text -replace $word,$replacement
    $newText > $path
    
    $path = "\\fileserv3\$env:username\UserDriveMappings.ini"
    $word = ":,"
    $replacement = " -Scope Global -Persist –PSProvider FileSystem –Root"
    $text = get-content $path 
    $newText = $text -replace $word,$replacement
    $newText > $path
    $newName = "\\fileserv3\$env:username\UserDriveMappings.ps1"
    
    Rename-Item -Path $path -NewName $newName
    Invoke-Expression -Command $newName

    I managed to get a " at the start of the UNC by joining the $replacement variable with a quote mark, but it leaves a space before the \\ of the UNC path. Totally failed to find a way to remove the space, or how to add a quote mark at the end:

    $replacement=($replacement + ' "')

    The script basically collects an INI file, copies it to a home drive, then re-writes it into a PowerShell script that then maps a set of drives. Works a treat for 95% of users. But the 5% have spaces in the UNC path. Hence the need to add quotes around the UNC.

    When the script collects the orignal INI file it looks like this:

    MapDrive I:, \\fileserv1\shared
    MapDrive M:, \\fileserv3\jondoe

    The output of the above script is then this:

    New-PSDrive –Name I -Scope Global -Persist –PSProvider FileSystem –Root \\fileserv1\shared
    New-PSDrive –Name M -Scope Global -Persist –PSProvider FileSystem –Root \\fileserv3\jondoe

    But what I want is this:

    New-PSDrive –Name I -Scope Global -Persist –PSProvider FileSystem –Root "\\fileserv1\shared"
    New-PSDrive –Name M -Scope Global -Persist –PSProvider FileSystem –Root "\\fileserv3\jondoe"

    Any help would be really appreciated.

  • #63403

    Rob Simmers
    Participant

    Trim() or RTrim() will remove whitespace:

    $lines = "MapDrive I:, \\fileserv1\shared","MapDrive M:, \\fileserv3\jondoe"
    
    $commands = foreach ($line in $lines) {
        $arrLine = $line -split ","
        $drive = ($arrLine[0] -replace "MapDrive", "New-PSDrive -Name ") -replace ":", ""
        $share = $arrLine[1].Trim()
        '{0} -Scope Global -Persist –PSProvider FileSystem –Root "{1}"' -f $drive, $share
    }
    
    $commands
    

    Output:

    New-PSDrive -Name  I -Scope Global -Persist –PSProvider FileSystem –Root "\\fileserv1\shared"
    New-PSDrive -Name  M -Scope Global -Persist –PSProvider FileSystem –Root "\\fileserv3\jondoe"
    
    • #63666

      Stuart
      Participant

      Hi Rob,

      Thank you for the suggestion about TRIM() for removing whitespace.

      Any thoughts about how to get double quote marks over a variable length UNC path? I can get the first double quote marks in place and use the Trim to remove the whitespace. But still need to find a way to add double quote marks at the end.

      is there a function that will find the last string character and append with " ?

      Many Thanks

    • #63830

      Stuart
      Participant

      Struggling here, and assume it must be possible to accomplish wrapping " around a UNC path when the UNC path is variable in length.

      Woudl be very grateful to hear from anyone with ideas

  • #63838

    Sonny Puijk
    Participant

    Just do?

    $NewVar = $Var + "`""
  • #63848

    Stuart
    Participant

    Jesus, that simple! Thanks Sonny, will try that later

  • #63854

    random commandline
    Participant
    $replacement = '"-Scope Global -Persist –PSProvider FileSystem –Root"'
    $replacement
    
  • #63950

    Stuart
    Participant

    Ah no sorry this still wont work. The final output (the bit with the UNC path) is not in a varaible. So:

    $NewVar = $Var + "`"" 

    Wont work. Also I am not trying to wrap " around -Scope Global -Persist –PSProvider FileSystem –Root.

    This script is picking up an existing file (INI file) and converting it into PowerShell language. So the last bit of each line is a UNC path. I need to add a " onto the end of each line of that UNC path. So that UNC path is NOT in a variable.

    Help so far appreciated.

  • #64219

    Sonny Puijk
    Participant

    Maybe post (part of) the ini file so we can assist?

    If all the lines in the ini file are a unc path it's pretty simple. If not (which makes more sense to me) it's a little bit more complicated though.

    • #64288

      Stuart
      Participant

      Hello Sonny, the INI file excerpt is included in the original post. Thank you

  • #64227

    Max Kozlov
    Participant
    $example = @'
    MapDrive I:, \\fileserv1\shared
    MapDrive M:, \\fileserv3\jondoe
    MapDrive Q:, \\fileserv3\strange share
    '@ -split "`n"
    $example | Foreach-Object {
      $_ -replace "^MapDrive (\w):,\s*(.*?)\s*$",'New-PSDrive –Name $1 -Scope Global -Persist –PSProvider FileSystem –Root "$2"'
    }
    

    and output is

    New-PSDrive –Name I -Scope Global -Persist –PSProvider FileSystem –Root "\\fileserv1\shared"
    New-PSDrive –Name M -Scope Global -Persist –PSProvider FileSystem –Root "\\fileserv3\jondoe"
    New-PSDrive –Name Q -Scope Global -Persist –PSProvider FileSystem –Root "\\fileserv3\strange share"
    
    • #64290

      Stuart
      Participant

      Thanks, I will try this

You must be logged in to reply to this topic.