如何使用CSV文件作为powershell脚本编辑日志文件的输入?

7fhtutme  于 2023-04-27  发布在  Shell
关注(0)|答案(1)|浏览(174)

我有时需要发送日志文件给供应商以获得技术支持。由于安全限制,这些日志文件不能包含可识别的数据,如主机名、IP地址等。
我需要一种方法来快速轻松地查找和替换日志文件中的这些数据,但也要能够计算出每次修订所替换的内容。
我的计划是扫描日志文件中的IP地址,创建一个IP地址和密文的查找CSV,然后使用密文CSV对输入文件进行查找/替换。
我已经设法创建了用于编辑的查找CSV,但对于我的生活,我一直无法解决如何使用它作为输入来查找和替换实际文件中的IP地址。

$ipNum = 1
$ipRedactions = "D:\ps_script\ipRedactions.csv"
#$global:ipPrefix = "Redacted-IP-"
$ipPrefix = "Redacted-IP-"
$ipRegex = "((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?){4}"
$inputFile = "D:\ps_script\ipsample.txt"
$fileContent = Get-Content $inputFile
$outputFile = ($inputFile + "_Redacted")
#$filecontent | % {[Regex]::Replace($_, $ipRegex, {return $global:ipNum += 1}) } | Set-Content $outputFile
$ipMatches = $fileContent | Select-String $ipRegex -AllMatches | ForEach-Object { $_.Matches.Value }

#Create a csv file of IP addresses and Redactions so we can do a find and replace in the source file later
$CSVPathTest = Test-Path $ipRedactions #Check if csv file exists
If ($CSVPathTest) {Remove-Item -Path $ipRedactions -Force} #Delete csv file if it exists
ForEach ($ipMatch in $ipMatches)
{
    $CSVList += @([pscustomobject]@{"IP Address"= $ipMatch; "Redaction"= $ipPrefix + $ipNum})| Export-Csv -Path $ipRedactions -Append -NoTypeInformation
    $ipNum = $ipNum + 1 #Increment the counter
}

#Create a new output file
$CSVPathTest = Test-Path $outputFile #Check if csv file exists
If ($CSVPathTest) {Remove-Item -Path $outputFile -Force} #Delete csv file if it exists

#Use the ipRedactions to find and replace IP addresses in the inputFile and create a redacted copy
????

我的简化测试数据看起来像:

test 127.0.0.1test 
test 10.0.0.1 test 
test 172.28.69.77test 
blah blah blahtest 
test 15.26.32.159 test 
test 15.26.32.1594test 
blah blah serverhostname test

密文的查找csv如下所示:

"IP Address","Redaction"
"127.0.0.1","Redacted-IP-1"
"10.0.0.1","Redacted-IP-2"
"172.28.69.77","Redacted-IP-3"
"15.26.32.159","Redacted-IP-4"
"15.26.32.159","Redacted-IP-5"

到目前为止,我的尝试还在上面。也许我只是累了,但我甚至还没有设法想到一种方法,这可能是做到目前为止。

bvhaajcl

bvhaajcl1#

事实上,我想我已经得到了它的工作后,多一点搜索互联网的想法。

$ipNum = 1
$ipRedactions = "D:\ps_script\ipRedactions.csv"
#$global:ipPrefix = "Redacted-IP-"
$ipPrefix = "Redacted-IP-"
$ipRegex = "((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?){4}"
$inputFile = "D:\ps_script\ipsample.txt"
#$fileContent = Get-Content $inputFile
$outputFile = ($inputFile + "_Redacted")
#$filecontent | % {[Regex]::Replace($_, $ipRegex, {return $global:ipNum += 1}) } | Set-Content $outputFile
$ipMatches = $fileContent | Select-String $ipRegex -AllMatches | ForEach-Object { $_.Matches.Value }

#Create a csv file of IP addresses and Redactions so we can do a find and replace in the source file later
$CSVPathTest = Test-Path $ipRedactions #Check if csv file exists
If ($CSVPathTest) {Remove-Item -Path $ipRedactions -Force} #Delete csv file if it exists
ForEach ($ipMatch in $ipMatches)
{
    $CSVList += @([pscustomobject]@{"IP_Address"= $ipMatch; "Redaction"= $ipPrefix + $ipNum})| Export-Csv -Path $ipRedactions -Append -NoTypeInformation
    $ipNum = $ipNum + 1 #Increment the counter
}

#Create a new output file
$CSVPathTest = Test-Path $outputFile #Check if csv file exists
If ($CSVPathTest) {Remove-Item -Path $outputFile -Force} #Delete csv file if it exists

#Use the ipRedactions to find and replace IP addresses in the inputFile
$redactions = Import-Csv $ipRedactions 
$fileContent = Get-Content $inputFile 
foreach ($row in $redactions) 
{ $field1 = $row.IP_Address 
$field2 = $row.Redaction
$fileContent = $fileContent | Foreach-Object { $_ -replace $field1,$field2} } 
$fileContent | Out-File $outputFile

相关问题