Powershell [System.Object[]不';t包含名为'的方法;更换';

Powershell [System.Object[]不';t包含名为'的方法;更换';,powershell,powershell-2.0,Powershell,Powershell 2.0,以下是edit07.html文件的一部分: 从$array我可以访问$empid=$user.employeeid和 $seat=$user.Position。代码中断的部分是 $filecontent = $filecontent.replace($pattern01,$new01) 这适用于PowerShell版本3,但我得到 [System.Object[]不包含名为“replace”的方法 当我使用PowerShell版本2运行它时 edit07.html: 该行: href=”h

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

$filecontent = $filecontent.replace($pattern01,$new01)
这适用于PowerShell版本3,但我得到

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

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

edit07.html


该行:

href=”https://athena/empstatus/getcontact.asp?empid=39#23-028
">
需要替换为:

“href=”“#”“data`在此处输入代码`toggle=”“tooltip”“title=”“$tooltip”“onClick=”“window.打开('
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 : 当我使用PowerShell版本2时,会收到以下错误消息:

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 = Get-Content 'c:\temp\edit07.html'


Get Content返回的不是单个字符串,而是字符串数组(在新行上拆分)和数组本身没有替换方法。您可以使用-Raw参数将其转换为单个字符串,或者对数组中的每个字符串调用replace方法。

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

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

  • 将文件读入单个字符串(正如您自己发现的那样)。有几种方法可以做到这一点:

    $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
    products,例如:

    $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 ...
    
    或者像这样:

    $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()
    方法执行简单的字符串替换,因此不需要转义


您发布的代码在第一次使用变量之前从未填充过
$filecontent
,因此您应该会收到不同的错误。请使您的示例代码尽可能独立。另外,您的其他尝试是否会产生相同的错误?PowerShell v3引入了参数
-Raw
。OP使用PowerShell v2。
$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 ...