【发布时间】:2015-09-05 18:20:52
【问题描述】:
我已经实习了几天,最近被要求编写一些 Powershell 脚本,我很高兴能学到一些新东西!
然而,这是一项耗时的任务,因为搜索东西很难找到你想要的东西。
无论如何,我的任务是从 word 文件中删除所有敏感数据。除了直到现在,情况还不算太糟。例如来自文本文件:
User pass created now moving on..
Password 7 ##########
All done
在文件中搜索“密码 7”和类似任务后,我不得不删除所有数字,这并没有花费我太长时间。
现在,我有一些固定长度的东西:
Self-Service certificate ####### ######## #######
######## ######## ######## ########## #########
########## ##### ######## ########## ##########
多行字符串。我可以删除第一行,但无法弄清楚下一行,因为它们只是随机数,我没有什么可搜索的。我尝试过nr \n \r 和许多组合。我被难住了。
$configFiles=get-childitem . *.txt -rec
foreach ($file in $configFiles)
{
$readIn = @(Get-Content $file.PSPath)
$readIn -replace "Password 7.*" , "Password 7 <REMOVED>" -replace "Secret 5.*" , "Secret 5 <REMOVED>" -replace "snmp-server community\s\S*" , "snmp-server community <REMOVED>" |
Set-Content $file.PSPath
}
这是我当前的代码,到目前为止运行良好。我一直在一个单独的脚本中处理多行删除。感谢您的帮助。
【问题讨论】:
标签: shell powershell replace scripting