所有服务器上的关键字搜索 - Powershell

时间:2017-08-07 11:59:55

标签: powershell

下午全部,

我需要在所有服务器上进行搜索 我有一个文本文档中的服务器列表和另一个中的关键字列表

nib

有没有办法确定何时找到关键字并找到总关键字?我觉得我需要改变这条线但是我无法理解我实际会放的东西,我已经尝试了$ Keywords,但只是在每次目录更改时都会更改关键字

$Servers = get-content -path 'C:\support\Server Search\Server Test.txt'
$Keywords = get-content -path "C:\Support\Server Search\Keyword Test.txt"

Foreach ($Server in $Servers){
Foreach ($Keyword in $Keywords){

Get-ChildItem "$Server" -Recurse | Where-Object {$_.Name -like "$Keyword"} 

$i++
Write-Host "$found: $i - Current $ $_"

New-Object -TypeName PSCustomObject -Property @{
Directory = $_.Directory
Name = $_.Name
Length = $_.Length /1024
CreationTime = $_.CreationTime
LastWriteTime = $_.LastWriteTime
LastAccessTime = $_.LastAccessTime}|
select Directory,Name,Length,CreationTime,LastWriteTime,LastAccessTime |
Export-Csv "C:\support\server search\$Server.csv" -Append -NoTypeInformation
}}   

$i = 0

1 个答案:

答案 0 :(得分:0)

我假设您的$服务器设置为" \\ servername \ c $ \"

  

找到关键字并找到总关键字后

$Servers = get-content -path 'C:\support\Server Search\Server Test.txt'
$Keywords = get-content -path "C:\Support\Server Search\Keyword Test.txt"

$num = 0  #Total Keyword files Found

Foreach ($Server in $Servers){
Foreach ($Keyword in $Keywords){
    #Keyword found Check
    $Found = Get-ChildItem -Path "$Server" -Recurse -Include "$Keyword"
    if($Found){
        Foreach($File in $Found){
            $num++   #increment num of keyword files found by 1        

            Write-Host "found: $num - $File"

            New-Object -TypeName PSCustomObject -Property @{
            Directory = $File.Directory
            Name = $File.Name
            Length = $File.Length /1024
            CreationTime = $File.CreationTime
            LastWriteTime = $File.LastWriteTime
            LastAccessTime = $File.LastAccessTime}|
            select Directory,Name,Length,CreationTime,LastWriteTime,LastAccessTime |
            Export-Csv "C:\support\server search\$Server.csv" -Append -NoTypeInformation
        }    
    }
}
}

请告诉我这是否有助于您取得进步。如果要求,我可以进一步提供帮助。