Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

replace not matching my string

replace not matching my string

replace not matching my string

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 --> powershell

#$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

RE: replace not matching my string

It worked for me


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, ""
PS C:\> 

RE: replace not matching my string

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 --> powershell

$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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close