Solved

Escaping for path with powershell

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

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 68

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now