[System.Object[]] 不包含名为“replace"的方法 [英] [System.Object[]] doesn't contain a methodnamed 'replace'

查看:66
本文介绍了[System.Object[]] 不包含名为“replace"的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是edit07.html文件的一部分:
$array 我可以访问 $empid = $user.employeeid$seat = $user.Position.代码中断的部分是

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)

这适用于 PowerShell 版本 3,但我明白了

This works on PowerShell version 3, but I get

[System.Object[]] 不包含名为replace"的方法

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

当我使用 PowerShell 版本 2 运行它时.

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;">

行:

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

需要替换为:

"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')")

$array 如下所示

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'

当我使用 PowerShell 版本 2 时,我收到此错误消息:

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

我也试过:

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

$filecontent -replace $pattern01,$new01

推荐答案

如果数组对象本身没有该方法,PowerShell v2 不会展开数组以对数组的每个元素调用该方法.该功能是在 PowerShell v3 中引入的.基本上有三种方法可以避免这个问题:

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:

  • 升级到 PowerShell v3 或更新版本.这是首选的解决方案.

  • 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')

  • Get-Content 生成的数组的每一行进行替换,例如像这样:

  • 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 ...
    

    或者像这样:

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

    请注意,-replace 运算符执行正则表达式匹配,因此您需要对搜索词中的特殊字符(如 ?)进行转义(这就是 [regex]::Escape() 确实如此)..Replace() 方法执行简单的字符串替换,因此不需要转义.

    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.

    这篇关于[System.Object[]] 不包含名为“replace"的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆