Solved

Escaping for path with powershell

Posted on 2013-05-15
5
2,503 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 70

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 70

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

626 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