Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace not matching my string

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
I have a script that uses convertToHtml, which all works fine, except it produces multiple table headers due to the way I'm looping through a list of server names. I'm trying to tidy this up if only the replace procedure would play nicely. Instead it seems to fail matching the string that I want to replace/ remove as it contains spaces.

I know that a -replace will use a reg-exp to match the string, but when trying a .replace I get a message saying that "[system.object[]] doesn't contain a method named 'replace'". I have tried entering the string as a reg exp string, which matches correctly when used with online regular expression testers.

Code:
#$stringtoreplace = " </table> <table> <colgroup> <col/> <col/> <col/> <col/> </colgroup> <tr><tr><th>MachineName</th><th>Name</th><th>DisplayName</th><th>Status</th></tr> <tr>"
$stringtoreplace = "\s<\/table>\s<table>\s<colgroup>\s<col\/>\s<col\/>\s<col\/>\s<col\/>\s<\/colgroup>\s<tr><tr><th>MachineName<\/th><th>Name<\/th><th>DisplayName<\/th><th>Status<\/th><\/tr>\s<tr>"
$body = $body -replace($stringtoreplace,"")

I've already tried escaping the slashes & using \s where the spaces are, but still can't seem to get the string to match. I've also tried specifying that the $body variable is a string, but I can't seem to get this to work either.

Thanks in advance
 
It worked for me
Code:
PS C:\> $body = "BEGIN </table> <table> <colgroup> <col/> <col/> <col/> <col/> </colgroup> <tr><tr><th>MachineName</th><th>Name</th><th>DisplayName</th><th>Status</th></tr> <tr>END"
PS C:\> $stringtoreplace = " </table> <table> <colgroup> <col/> <col/> <col/> <col/> </colgroup> <tr><tr><th>MachineName</th><th>Name</th><th>DisplayName</th><th>Status</th></tr> <tr>"
PS C:\> $body -replace $stringtoreplace, ""
BEGINEND
PS C:\>
 
I happened to do the same testing on the command line & found that the matching is working too, so it must have been the way or where in the script I was doing the replace. I've managed to overcome this now by writing out the file & then doing the replace & outputting that using set-content. I don't think it's the best way of doing it, but it worked. I've pasted the code below in case anyone can correct what I was doing (I've commented out the $body replace bit).


Code:
$ServerList = Get-Content -path C:\servers.txt
$serviceList = Get-Content -path C:\Services.txt
$OutputFile = C:\out.html

$body = $ServerList | foreach { (Get-Service -Name $serviceList -computername $_ -ErrorAction SilentlyContinue ) | 
        Select-Object MachineName, Name, Status | ConvertTo-HTML -Fragment}

$colorTagTable = @{Stopped = ' bgcolor="#FE2E2E">Stopped<';
                   Running = ' bgcolor="#01DF01">Running<'}

$colorTagTable.Keys | foreach { $body = $body -replace ">$_<",($colorTagTable.$_) }

$string = " </table> <table> <colgroup> <col/> <col/> <col/> </colgroup> <tr><th>MachineName</th><th>Name</th><th>Status</th></tr> <tr>"

#$body = ($body -replace $string,"<tr>")
#write-host $body

ConvertTo-HTML -head $Style $Title -body "<font color =""#B40404""><H2>Service Status</H2>$body" | Out-File $OutputFile
(Get-Content $OutputFile) -replace $string,"<tr>" | Set-Content $OutputFile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top