Struggling with Find and Replace

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.

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



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

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

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

Just do?

$NewVar = $Var + "`""

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

$replacement = '"-Scope Global -Persist –PSProvider FileSystem –Root"'

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.

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.

$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"

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

Thanks, I will try this