Powershell 如何在.conf文件中查找文件路径?

Powershell 如何在.conf文件中查找文件路径?,powershell,Powershell,我必须打开并搜索很多目录和.conf/.xml文件。我现在有这个: $Path = "D:\Logs" $Text = "*\log" $PathArray = @() $Results = "D:\Logs\Search.txt" Get-Childitem $Path -Filter "*.conf" -Recurse | Where-Object {$_.Attributes -ne "Directory"} | ForEach-Object

我必须打开并搜索很多目录和.conf/.xml文件。我现在有这个:

$Path = "D:\Logs"

$Text = "*\log"

$PathArray = @()

$Results = "D:\Logs\Search.txt"

Get-Childitem $Path -Filter "*.conf" -Recurse | 
    Where-Object {$_.Attributes -ne "Directory"} | 
        ForEach-Object
        {
            If (Get-Content $_.FullName | Select-String -Pattern $Text -AllMatches)
            {
                $PathArray += $_.FullName
                $PathArray += $_.FullName
            }
        }

Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}

$PathArray | % {$_} | Out-File “D:\Logs\Search.txt” 

我正在尝试创建这个脚本,这样我就可以让out文件报告一个txt文件中的所有.conf文件,该文件具有.conf文件所在的正确路径。我还将通过简单地用.xml替换.conf来处理.xml文件。到目前为止,我正在获取.txt文件,但没有路径。我知道我遗漏了一两件事,但我不知道是什么。我必须用我已经创建的新路径手动更改旧路径。我想运行此脚本来搜索所有包含*\log或*\logs的.conf/.xml文件。

正则表达式没有转义反斜杠存在问题,它显然与您显示的.conf文件的典型内容不匹配。此外,它还可以简化。尝试此操作-调整$Text regex以实际匹配.conf文件中所需的文本:

$Path = "D:\Logs"
$Text = "\\log"
$Results = "D:\Logs\Search.txt"

$PathArray = @(Get-Childitem $Path -Filter *.conf -Recurse -File | 
               Where {(Get-Content $_.FullName -Raw) -match $Text})

Write-Host "Contents of ArrayPath:"
$PathArray

$PathArray | Out-File $Results -Encoding ascii

有几个问题。Keith得到的最大的一个结果是
selectstring
默认使用正则表达式。同样,您有一些冗余,比如两次添加到
$pathArray
,以及使用
ForEach对象{$\uz}

我想展示一个仍然使用
selectstring
的解决方案,但是使用其中的一些开关来获得您想要的用途。主要的一个是
-simplematch
,它按字面意思处理模式,而不是按正则表达式处理。我在示例文本中看到日志前面有一个下划线,所以我在这里使用它。如果您不想或它与您的数据不匹配,只需将其删除即可

$Path = "D:\Logs"
$Text = "_log"
$Results = "D:\Logs\Search.txt"

$PathArray = Get-Childitem $Path -Filter "*.conf" -Recurse | 
    Where-Object {$_.Attributes -ne "Directory"} | 
    Where-Object{Select-string $_ -Pattern $Text -SimpleMatch -Quiet} |
    Select-Object -ExpandProperty FullName

# Show results on screen
$PathArray

# Export results to file
$PathArray | Set-Content $Results

我可以在这里看到一些问题,包括一些冗余问题。您可以包含一个.conf的小示例,因为它可能是其中的任何内容。您希望得到什么样的输出?在文件中找到的匹配项是什么?那么basic.conf文件的日志路径将类似于“c:\apache\tomcat\bin”。许多日志文件是分散的,搜索它们很麻烦。至于输出,我想要.conf文件的路径,其中包含“\log”。我希望搜索模式能够查找日志的原始位置,这样我就可以手动将它们更改为集中式日志文件夹系统?这意味着您也需要匹配的行?这将是文件的一个示例谢谢您的帮助。非常感谢你的帮助。我会尽快测试它。