2017-07-04 76 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