2017-07-04 89 views
0

$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文本文件,但它不工作。我没有收到任何错误。

+0

你要管的东西了文件。目前out-file将不会写任何 – guiwhatsthat

+0

这是好的至少它应该进入内部没有得到任何东西写入主机($ select.length) – deepti

+0

你怎么知道长度?它匹配多少次? – guiwhatsthat

回答

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 
+0

真棒工作,非常感谢我标记为答案 – deepti

+0

@deepti很高兴听到它的排序。强烈建议使用'Get-Member'与不同的变量进行交互,并找出你可以用它们做什么....不知道'Select-String'返回的类型为'MatchInfo',并且具有'Line'和' LineNumber'直到我运行该命令。 – gms0ulman