Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Escaping for path with powershell

Posted on 2013-05-15
5
Medium Priority
?
2,685 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

704 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