模式匹配回傳 $True 所以我認為應該繼續運行替換的測驗,但只是處于無回應模式。我的測驗沒有將最后 3 行替換為“現在擔任主要或次要角色”有什么問題
service.log 檔案具有以下語法:
NOW in the PRIMARY or SECONDARY role
End of Job Run
NOW in the PRIMARY or SECONDARY role
End of Job Run
NOW in the PRIMARY or SECONDARY role
End of Job Run
NOW in the PRIMARY or SECONDARY role
End of Job Run
not in the PRIMARY or SECONDARY role
End of Job Run
not in the PRIMARY or SECONDARY role
End of Job Run
not in the PRIMARY or SECONDARY role
$Path = "D:\Program Files\test"
$File = "service.log"
$result = Get-ChildItem $Path -recurse | Where-Object { $_.Name -match $File }
$a = Get-Content $result.FullName -Tail 10 | Select-string -Pattern "not in the PRIMARY or SECONDARY role" -Quiet
if ($a -eq $True) {
((Get-Content $result.FullName -Raw) -Replace 'not in the PRIMARY or SECONDARY role', 'NOW in the PRIMARY or SECONDARY role') | Set-Content -Path $Path\$File }
現在作業——
$Directory = "D:\Program Files\test"
$Source = "service.log"
$result = Get-ChildItem $Directory -recurse -filter $Source
if (( Get-Content $result.FullName -Tail 10 | Select-string -Pattern "not in the PRIMARY or SECONDARY role" -Quiet ) ) {
((Get-Content -path $Directory\$Source -Raw) -Replace 'not in the PRIMARY or SECONDARY role', 'NOW in the PRIMARY or SECONDARY role') | Set-Content -Path $Directory\$Source
}
uj5u.com熱心網友回復:
它只是坐在那里的原因是-Wait引數。根據以下檔案Get-Content:
輸出所有現有行后保持檔案打開。在等待期間,Get-Content 每秒檢查一次檔案并輸出新行(如果存在)。您可以通過按 CTRL C 來中斷等待。如果檔案被洗掉,等待也會結束,在這種情況下會報告非終止錯誤。洗掉它,腳本將按預期完成。
除此之外,您應該在Get-ChildItemcmdlet 中過濾檔案,而不是獲取所有檔案,然后使用Where-Object陳述句進行過濾。它要快得多,而且是一個很好的做法。
$Path = "D:\Program Files\test"
$File = "service.log"
$result = Get-ChildItem $Path -recurse -filter $File
我也沒有$a在任何地方看到定義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438279.html
下一篇:以不同的表單標簽C#顯示計算結果
