长度函数不给出输出

时间:2017-07-04 09:57:00

标签: powershell

$select.Length没有提供输出。请在下面找到代码:

clear
$filePath = "c:\temp\result.txt"
$select = Select-String -Pattern "Final result:" -Path 'C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt' #| Out-File c:\pattern.txt
$select1 = Select-String -Pattern "Final result:" -Path 'C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt' | Out-File D:\temp\pattern.txt
Write-Host($select.Length)

if ($select.Length -gt 0) {
    Write-Host 'Contains String'
    $select2 = Select-String -Pattern "Passed" -Path 'D:\temp\pattern.txt'

    if ($select2.Length -gt 0) {
        #Out-File C:\temp\result.txt $filePath -Append 
        New-Item $filePath -ItemType file
        'Success' | Out-File -FilePath $filePath -Append   
    } else {
        New-Item $filePath -ItemType file
        'Failed' | Out-File -FilePath $filePath -Append  
    }
} else {
    Write-Host 'Does not contain String'
}

理想情况下,它应该生成成功的文件作为result.txt中的文本,但它不起作用。我没有收到任何错误。

2 个答案:

答案 0 :(得分:1)

这会计算匹配次数:

$select2.Matches.Count

答案 1 :(得分:1)

  • $select不包含[String]类型,我假设您希望拥有属性Length。相反,请使用$select.Line.Length,其中$select.Line是匹配的行。重复$select2

Get-Member shows Line property

  • $select1 - 不要在分配变量的同时管道输出文件。我认为下面可以找到你想要的东西并避免这种情况。
$select = Select-String -Pattern "Final result:" -Path 'C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt'

# This outputs the matching line only
$select.Line |Out-File D:\temp\pattern.txt