Solved

Escaping for path with powershell

Posted on 2013-05-15
5
2,177 Views
Last Modified: 2013-05-15
I have started to make the plunge into converting our VBS to Powershell scripts and have had pretty good luck so far..

However, I am running into an issue with our Apache Log detection script.  It works great under VBS, but Powershell does a great replacement, but still doubles the path..

For example: d:\\logs\\some_server.log  and I need this to be parsed as: d:\logs\some_server.log

$tomcat = Get-Service tomcat* | select -Expand name
Write-Host $tomcat

$description=Get-wmiobject win32_service | where-object {$_.name -match $tomcat} | Foreach-Object {$_.Description}
$PathName=Get-wmiobject win32_service | where-object {$_.name -match $tomcat} | Foreach-Object {$_.PathName}

$TomcatVer = $description.split(" ")[2]
$TomcatPath = $PathName.Substring(0,$PathName.lastindexofany("\bin\")-3)

Write-Host $TomcatVer
Write-Host $TomcatPath

$Serverxml = $TomcatPath + "conf\server.xml"
Write-Host $Serverxml

$sxmls = Get-Content $Serverxml #-encoding String
foreach ($sxml in $sxmls)
{
If ($sxml.contains("appBase"))
{
$LogPath = $sxml.split("=")[1]
$LogPath =$LogPath.Split(" ")[0]
$LogPath = $LogPath -replace([Char](34), "")
}
}

$FL = $LogPath + "\some_server\WEB-INF\classes\log4j.properties"
If ($TomcatVer = "6.0")
{
$LogPath = $TomcatPath + "\webapps\OurApplication"
$FL = $LogPath + "\WEB-INF\classes\log4j.properties"
}

$FLSs = Get-Content $FL
foreach ($FLS in $FLSs)
{
If ($FLS.contains("log4j.appender.LOGFILE.File"))
{
$LogPath = $FLS.Split("=")[1]
$LogPath = $LogPath.Trim()
If ($LogPath.contains("\\"))
{
Write-Host "LogPath "$LogPath
$Separator = [Char](92)+[Char](92)
Write-Host "Separator "$Separator
# Did find: http://stackoverflow.com/questions/548060/replacing-escape-characters-in-powershell
#-replace '\\\\(\w+)\\(\w+)\$', '$1'
$NewLogPath=$LogPath -replace ('\\(.)\1', "\")
Write-Host "first "$NewLogPath
##Still Shows: first  d:\\logs\\aa_server.log
}
If ($LogPath.contains("/"))
{
Write-Host "LogPath "$LogPath
$Separator = "/"
Write-Host "Separator "$Separator
$NewLogPath=$LogPath -replace ($Separator, "\")
Write-Host "second "$NewLogPath
}
}
}

Open in new window


Thanks,

Kent
0
Comment
Question by:Kent Dyer
5 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39168750
Your usage of lastindexofany in line 8 is wrong. That methods checks for the occurance of ANY of the characters you provide, not of a substring. It will search for '\', 'b', 'i', or 'n'.
But that is not the issue.

I would use -replace '\\\\', '\' for removing the double backslashes.
0
 
LVL 18

Expert Comment

by:x-men
ID: 39168783
-replace ("\\.","\")
0
 
LVL 17

Author Closing Comment

by:Kent Dyer
ID: 39168797
Very cool!  I know I read about doubling up the slashes, but I had to get it in front of me again..  Also, a good solution in the sense you don't have to use RegEXP...
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39168824
$LogPath = "d:\\logs\\some_server.log"
$LogPath.replace('\\','\')

Open in new window

Should also work..
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39168955
Of course that should also work.
[String]::Replace does not use RegExp, hence no escaping necessary.
-replace uses RegExp, and '\' is a special character needing escaping.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question