我有一個檔案,我希望 Powershell 搜索 Include1,如果是,請查看該行的其余部分是否包含 XYZ。如果存在,很好,但如果不存在,請查看 Include2 是否存在并重復。但是一旦 Powershell 到達 IncludeX 不存在并且我們還沒有找到 XYZ 的地步,在檔案末尾,用 XYX 撰寫下一個可用的 IncludeX,然后洗掉所有雙空行 \r\n\r\n。我可以找到很多 Powershell 在檔案中查找字串的示例,如果字串不存在,一些可以撰寫設定行,但不能將 IncludeX 作為新行的一部分增加。請幫忙!
該檔案是ccleaner.ini,如果你有CCleaner,很容易參考。Include1后面的Include<n>行用于設定檔案路徑以包含在不在其默認串列中的清理中。我有一個腳本可以修改檔案中的一些設定,但我無法弄清楚。
我的檔案中的示例:
Include1=PATH|C:\ProgramData\Quest\KACE\downloads\|*.*|RECURSE|0|0|24
Include2=PATH|D:\Temp\|*.*|RECURSE|0|0|24
我想確保我將在其上運行此腳本的計算機具有=PATH|C:\ProgramData\Quest\KACE\downloads\|*.*|RECURSE|0|0|24,無論Include它是什么數字。
uj5u.com熱心網友回復:
您可以使用以下switch陳述句:
$file = 'ccleaner.ini' # Path to the file of interest.
$(
$num = 0; $found = $false
$requiredValue = 'PATH|C:\ProgramData\Quest\KACE\downloads\|*.*|RECURSE|0|0|24'
switch -Regex -File $file {
'^\s*Include(\d )=(.*)' {
$_ # Pass line through
if ($found) { continue }
[int] $num = $Matches[1]
$found = $Matches[2] -eq $requiredValue
}
default {
$_ # Pass other lines through.
}
}
# If the value of interest wasn't found, add it now
# with the next sequence number.
if (-not $found) {
"Include$($num 1)=$requiredValue"
}
) | Set-Content $file -WhatIf
注意:上面命令中的-WhatIf常用引數是預覽操作。-WhatIf 一旦您確定該操作將執行您想要的操作,請洗掉。
還要將多個連續的換行符折疊成一個以洗掉空行(...代表上述解決方案的一部分):
$(...) -join "`r`n" -replace '(\r\n){2,}', "`r`n" | Set-Content ...
uj5u.com熱心網友回復:
如果我了解您要做什么,那么我最近在做類似的事情。所以我重寫了它來制作這個。
老實說,我不明白你想用雙空行做什么,所以我沒有試圖解決這個問題。如果您的源檔案有空行,并且您想洗掉它們,則可能需要在搜索包含行和附加新的包含行之前執行此操作。
如果您只處理一個檔案,請使用注釋掉的“$Files = ...”陳述句并為其提供所需檔案的確切名稱。并且不要忘記將 Get-ChildItem 中的路徑設定為正確的檔案夾。
完成后,$Files 陣列應該與 $Results 陣列具有相同的長度,并且您應該能夠通過檢查 $Results 中的匹配結果來找出每個檔案發生了什么。結果為 0 表示已找到該值且未執行任何操作。高于 0 的結果意味著添加了新行,結果是給出的包含編號。
在測驗中使用了一些您想要洗掉的 Write-host 陳述句。
function Process-File {
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$WhatToLookFor,
[Parameter(ValueFromPipeline)]
$FilePath
)
begin {
$LookingFor = $WhatToLookFor.Trim()
}
Process {
[int]$Return = 0
[bool]$FoundIt = $false
$FileFullName = $FilePath.FullName
Write-Host
Write-Host "Working on $FileFullName"
switch -Regex -File $FileFullName {
'(?i)^\s*Include(?<Number>[0-9] )\s*=\s*(?<Value>. ?)\s*$' {
[int]$Number = [int]($Matches.Number)
if($Number -gt $Return) { $Return=$Number}
$Value = $Matches.Value
if($Value -eq $LookingFor) {$FoundIt = $true}
Write-Host "$Number=[$Value]"
continue
}
default { continue }
}
$Return
if( $FoundIt) {
return 0
} else {
"Include$Return=$LookingFor" | Add-Content $FileFullName
return $Return
}
}
}
#$Files = Get-ChildItem -Path 'D:\Temp\StackOverflow\71160426' -Filter 'TextIn.txt'
$Files = Get-ChildItem -Path 'D:\Temp\StackOverflow\71160426' -Filter '*.txt' -Recurse
$Results = $Files | Process-File 'NPR'
foreach ($Result in $Results) {
Write-Host
if($Result -gt 0) {
Write-Host "Saved as Include$Result."
} else {
Write-Host "Item was found and nothing added to file."
}
}
在 2 個檔案 TextIn1.txt 和 TextIn2.txt 上對其進行了測驗,每個檔案都包含 1 到 8 個已經定義的檔案。TextIn1.txt 還包含值為“NPR”的 include9。以下是對這兩個檔案運行腳本的結果。完成后,兩個檔案最后都有相同的“Include9=NPR”。
下面輸出中的最后 2 個陳述句解釋了對 TextIn1.txt 和 TextIn2.txt 采取的操作。我沒有花時間在陳述句中添加檔案名,但您知道剛剛發生了什么。
這沒有針對不同內容的檔案進行嚴格測驗。所以你可能需要做一些修改來處理你的真實世界的源檔案。
Working on D:\Temp\StackOverflow\71160426\TextIn1.txt
1=[ABC]
2=[DEF]
3=[GHI]
4=[JKL]
5=[MNO]
6=[PQR]
7=[STU]
8=[XYZ]
9=[NPR]
Working on D:\Temp\StackOverflow\71160426\TextIn2.txt
1=[ABC]
2=[DEF]
3=[GHI]
4=[JKL]
5=[MNO]
6=[PQR]
7=[STU]
8=[XYZ]
Item was found and nothing added to file.
Saved as Include9.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/428272.html
標籤:电源外壳
上一篇:獲取所有OU中的用戶計數并總計
下一篇:無法從存盤帳戶中洗掉診斷設定
