Author Posts

February 2, 2017 at 5:59 pm

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.

February 2, 2017 at 6:15 pm

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"

February 7, 2017 at 12:00 pm

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

February 9, 2017 at 12:05 pm

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

February 9, 2017 at 12:24 pm

Just do?

$NewVar = $Var + "`""

February 9, 2017 at 1:52 pm

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

February 9, 2017 at 2:31 pm

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

February 10, 2017 at 5:30 pm

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.

February 15, 2017 at 6:37 am

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.

February 15, 2017 at 8:09 am

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

February 15, 2017 at 5:05 pm

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

February 15, 2017 at 5:10 pm

Thanks, I will try this