$_ -replace and powershell 2.0

Welcome Forums General PowerShell Q&A $_ -replace and powershell 2.0

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
5 years, 4 months ago.

  • Author
    Posts
  • #8114

    Participant
    Points: 0
    Rank: Member

    I am having problems getting the following code to work on Powershell 2.0.

    Get-BackupData -BACKUPID $UniqueIDS |
                   Select-Object @{Name='StartTime';Expression={$_.StartTime -as [Datetime]}},FilesSkipped,JobName,BytesProcessed,DeviceName,JobStatus,FilesCorrupt |
                   ConvertTo-Html -Fragment |
                   ForEach{
    
                             $_ -replace "Completed","Completed"`
                                -replace "3 (Completed with exceptions)","Completed with exceptions"`
                                -replace "6 Completed Failed"," Completed Failed"
    
                          }[{pre]
  • #8115

    Participant
    Points: 1
    Rank: Member

    It would be helpful to know what specific problems you are having with your code.

    Also, if you have issues with -replace, run the first of the two strings (the search string) through [regex]::Escape to see if it contains any escape characters.

    For example, when I invoke this:

    [regex]::Escape('(Completed)')
    

    I get the following back:

    \(Completed\)
    

    That shows me that the round brackets are special characters in regular expressions, and they need to be escaped when I want them treated as literal characters.

    Kirk out.

  • #8116

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    You might also look into the free ebook here (on the Newsletter tab), "Creating HTML Reports in PowerShell." There's an EnhancedHTML module that can do conditional table cell styling as it produces the HTML, so you don't have to jury-rig a replace.

  • #8117

    Participant
    Points: 0
    Rank: Member

    Thanks for the replies, I didnt realise I had actually pressed post I was waiting to add more but Andy Murray was playing at wimbledon :P. The main problem I have is when moving from powershell 3 to 2 If I want to run multiple replaces on one string in version 3.0 I do it like the way shown above but in Powershell 2.0 this doesnt work. I have seen the free ebook which has given me some excellent tips, but I understood the enhanced html module would not work on version 2.0? I still support a lot of older servers especially SBS 2008 which I have read has huge problems if powershell 3.0 is installed. Basically I am looking for the easiest way to perform multiple replaces on one string in powershell 2.0.

  • #8118

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    The module should work fine on v2, actually. I don't think I used any v3-specific functionality.

    If you're doing serialized replaces, using a method is probably easier.

    $string.Replace($this,$that).Replace($this,$that)

    Something along those lines. You can use the -replace operator, but you have to group them:

    (($string -replace $this,$that) -replace $this,$that) -replace $this,$that

    It gets uglier faster.

  • #8119

    Participant
    Points: 1
    Rank: Member

    Actually that's not quite accurate. You can use multiple -replace calls in sequence in PowerShell 2.0 and PowerShell 3.0. I use -replace (I prefer the regular expression matching capabilities that come with replace), and I routinely use multiple -replace calls in sequence, often mixing -replace, -split, and -join calls up in one line to get the output I need, no brackets required.

    If you switch to the System.String Replace method, be aware that it does not perform regular expression matching.

    If you are having problems switching between PowerShell 3.0 and 2.0, I don't think your use of multiple -replace calls in one line is the culprit.

The topic ‘$_ -replace and powershell 2.0’ is closed to new replies.