Powershell 查找和替换脚本
Powershell Find and Replace Script
模式匹配正在返回 $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
}
它只是坐在那里的原因是 -Wait
参数。根据 Get-Content
的文档:
Keeps the file open after all existing lines have been output. While
waiting, Get-Content checks the file once each second and outputs new
lines if present. You can interrupt Wait by pressing CTRL+C. Waiting
also ends if the file gets deleted, in which case a non-terminating
error is reported.
Remove that and the script will complete as expected.
除此之外,您应该在 Get-ChildItem
cmdlet 中过滤文件,而不是获取所有文件然后使用 Where-Object
语句过滤。它要快得多,也是一个很好的做法。
$Path = "D:\Program Files\test"
$File = "service.log"
$result = Get-ChildItem $Path -recurse -filter $File
我也没有在任何地方看到 $a
定义。
模式匹配正在返回 $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
}
它只是坐在那里的原因是 -Wait
参数。根据 Get-Content
的文档:
Keeps the file open after all existing lines have been output. While waiting, Get-Content checks the file once each second and outputs new lines if present. You can interrupt Wait by pressing CTRL+C. Waiting also ends if the file gets deleted, in which case a non-terminating error is reported. Remove that and the script will complete as expected.
除此之外,您应该在 Get-ChildItem
cmdlet 中过滤文件,而不是获取所有文件然后使用 Where-Object
语句过滤。它要快得多,也是一个很好的做法。
$Path = "D:\Program Files\test"
$File = "service.log"
$result = Get-ChildItem $Path -recurse -filter $File
我也没有在任何地方看到 $a
定义。