Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2826
  • Last Modified:

Escaping for path with powershell

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
Kent Dyer
Asked:
Kent Dyer
1 Solution
 
QlemoC++ DeveloperCommented:
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
 
x-menIT super heroCommented:
-replace ("\\.","\")
0
 
Kent DyerAuthor Commented:
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
 
SubsunCommented:
$LogPath = "d:\\logs\\some_server.log"
$LogPath.replace('\\','\')

Open in new window

Should also work..
0
 
QlemoC++ DeveloperCommented:
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

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now