2015-10-15 30 views
0

我嘗試沒有成功運行下一個腳本:如何查找文件並根據內容添加新行?

1.try找到下d所有.properties文件:\ 如果文件內容包括「IIS」,然後追加一個單獨的兩行到 文件。

2.try找到所有.prop2文件在C:\ 如果文件內容include'win88' 追加一個單獨的兩行的文件。

腳本:

$file1 = Get-ChildItem -Path D:\* -Filter *.properties -Recurse 
$file2 = GetChildItem -Path C:\* -Filter *.prop2 -Recurse 
if ($file1 -contains '*iis*') {Add-Content $file1 "'nServer1'nServer2"}; 
if ($file2 -contains '*win88*') {Add-Content $file2 "'nServer3'nServer4"}; 

回答

0

Get-ChildItem可以返回多個文件。您需要遍歷文件列表,獲取其內容,然後檢查它們是否包含您要查找的字符串。這是一個簡單的解決方案*.properties

$file1 = Get-ChildItem -Path D:\* -Filter *.properties -Recurse | % { 
    $fileContents = Get-Content $_ 
    if ($fileContents -contains '*iis*') {Add-Content $file1 "'nServer1'nServer2"}; 
}