2

The following is part of the file edit07.html:
From $array I'm able to access the $empid = $user.employeeid and $seat = $user.Position. The part where is code breaks is

$filecontent = $filecontent.replace($pattern01,$new01)

This works on PowerShell version 3, but I get

[System.Object[]] doesn't contain a methodnamed 'replace'

when I run it with PowerShell version 2.

edit07.html:

</a></td>
</tr>
<tr>
<td height=1></td>
<td colspan=5></td>
<td colspan=3 rowspan=4 align=left valign=top><a
href="https://athena/empstatus/getcontact.asp?empid=238735#23-006&#13;">

</a></td>
<td></td>
<td colspan=2></td>
<td colspan=3 rowspan=4 align=left valign=top><a
href="https://athena/empstatus/getcontact.asp?empid=126086#23-017&#13;">

</a></td>
<td></td>
<td colspan=3 rowspan=4 align=left valign=top><a
href="https://athena/empstatus/getcontact.asp?empid=39#23-028&#13;">

The line:

href="https://athena/empstatus/getcontact.asp?empid=39#23-028&#13;">

needs to be replaced with:

"href=""#"" data`enter code here`toggle=""tooltip""title=""$tooltip""onClick=""window.open('
https://athena/empsta   tus/getcontact.asp?empid=" + $empid + "','mywindow','width=282,height=325,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no,style=text-`decoration:none')")

The $array looks like the following

Position     : 28-015
FirstName    : Marry
LastName     : Hay
PhoneNumber  : 448
Department   : IT
ComputerName : KURO-36
employeeid   : 245423
mail         : 

Position     : 28-016
FirstName    : Jimmy
LastName     : Jay
PhoneNumber  : 346
Department   : Researchers
ComputerName : SOBU-04
employeeid   : 231690
mail         : 

Position     : 28-018
FirstName    : Jack
LastName     : Johnson
PhoneNumber  : 454
Department   : Operations
ComputerName : SOBU-06
employeeid   : 384737
mail         : 

Position     : 28-022
FirstName    : Joe
LastName     : Blow
PhoneNumber  : 319
Department   : Operations
ComputerName : MITA-54
employeeid   : 100083
mail         : 
$fileContent = Get-Content 'c:\temp\edit07.html'

foreach ($user in $array) {
  $empid = $user.employeeid

  $fname = $user.FirstName
  $lname = $user.LastName
  $phone = $user.PhoneNumber
  $pc = $user.ComputerName
  $seat = $user.Position

  $tooltip = @"
$fname
$lname
$phone
$pc
$seat
"@

  $find01    = 'href="https://athena/empstatus/getcontact.asp?empid=' + $empid + '#' + $seat + '&#13;">'

  $replace01 = "href=""#"" data-toggle=""tooltip""title=""$tooltip""onClick=""window.open('https://athena/empstatus/getcontact.asp?empid=" + $empid +       "','mywindow','width=282,height=325,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no,style=text-        decoration:none')"

  $empid
  $pattern01 = $find01
  $new01 = $replace01
  $strreplace01 = [regex]::replace($empid, $pattern01, $new01) 

  $filecontent = $filecontent.replace($pattern01,$new01)
  #$filecontent  -replace $pattern01,$new01
}

$fileContent | Set-Content 'c:\temp\edit08.html'

when I use PowerShell version 2 I get this error message:

Method invocation failed because [System.Object[]] doesn't contain a method
named 'replace'.
At line:38 char:40
+     $filecontent = $filecontent.replace <<<< ($pattern01,$new01)
    + CategoryInfo          : InvalidOperation: (replace:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

I also tried:

$filecontent = [System.IO.File]::ReadAllText($filecontent).replace($pattern01,$new01)

and

$filecontent -replace $pattern01,$new01
1
  • 3
    The code you posted never populates $filecontent before using the variable the first time, so you should be getting a different error. Please make your sample code as self-contained as possible. Also, do your other attempts yield the same error? Commented Feb 26, 2016 at 2:06

3 Answers 3

2

PowerShell v2 doesn't unroll arrays to call a method on each element of an array if the array object itself doesn't have that method. That feature was introduced with PowerShell v3. There are basically three ways to avoid this problem:

  • Upgrade to PowerShell v3 or newer. This is the preferred solution.

  • Read the file into a single string (as you have found out yourself). There are several ways to do this:

    $fileContent = Get-Content 'C:\path\to\your.html' | Out-String
    $fileContent = (Get-Content 'C:\path\to\your.html') -join "`r`n"
    $fileContent = [IO.File]::ReadAllText('C:\path\to\your.html')
    
  • Do the replacement for each line of the array Get-Content produces, e.g. like this:

    $search  = '...'
    $replace = '...'
    
    $fileContent = Get-Content 'C:\path\to\your.html'
    
    $fileContent -replace [regex]::Escape($search), $replace | Set-Content ...
    

    or like this:

    $search  = '...'
    $replace = '...'
    
    $fileContent = Get-Content 'C:\path\to\your.html'
    
    $fileContent | ForEach-Object { $_.Replace($search, $replace) } |
      Set-Content ...
    

    Note that the -replace operator does a regular expression match, so you need to escape special characters (like ?) in the search term (that's what [regex]::Escape() does). The .Replace() method does a simple string replacement, and thus doesn't require escaping.

Sign up to request clarification or add additional context in comments.

Comments

1

Get-Content returns not a single string, but array of strings (split on new lines) and array itself doesn't have a replace method. You can turn it into a single string with -Raw parameter or call replace method on each string in the array.

1 Comment

The parameter -Raw was introduced with PowerShell v3. The OP uses PowerShell v2.
0

Changed

$fileContent = Get-Content 'c:\temp\edit07.html'

to

$fileContent = [IO.file]::ReadAllText('c:\mgmt\schart\edit07.html')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.