如何从多台远程计算机上的日志文件中提取句子

时间:2019-08-12 16:35:43

标签: powershell

我需要从多台远程计算机上的日志文件中提取特定的句子。我已经有了所有的计算机名称,但是我不知道如何从文件中提取文件内容并将其全部复制到文件中,这样我就可以从每台计算机的句子中引用其机器名称。基本上,每台机器都有一个自己需要的特定编号。

在进行解释之前,我假设使用powershell作为工具。

大约有1800台机器,所有这些机器都有一个变量。然后,我假定我必须在每个计算机上运行某种循环。

该循环将从我需要​​的文件中提取文本并将其全部保存到文件中。我的Net Admin职位基本上是新手,没有太多的PowerShell经验,我想知道是否有人可以提供帮助。

$computers = ***list of computers***

$computers | ForEachObject{ 
    Add-Content -Path C:\Users\Public\Activant\Eagle\3log.log -Value "Terminal information for ***need the info that is here***"
}

获取内容-路径。\ TERMINAL NUMBERS.txt

2 个答案:

答案 0 :(得分:0)

这似乎可以满足您的要求。 [ grin ]会构建一个脚本块来完成工作,将其移交给Invoke-Command并附带要运行的系统列表,收集结果,创建{{1}列表},删除由$Non-Responders cmdlet添加的不需要的属性,最后显示两个集合。

I-C

输出...

#requires -RunAsAdministrator

# fake reading in a text file
#    in real life, use Get-Content
$ComputerNameList = @'
LocalHost
10.0.0.1
127.0.0.1
BetterNotBeThere
'@ -split [System.Environment]::NewLine

$IC_ScriptBlock = {
    $TargetFileName = 'C:\Temp\Grouping-Strings-List_2019-07-31.log'
    # the " \b\w+\b \b\w+\b " is two words delimited by spaces
    #    so this will find any line that has two words between the listed phrases
    $LinePattern = '^Acid Drum \b\w+\b \b\w+\b Psychedelic$'
    # the next line is a no-match patern for testing
    #$LinePattern = '^Acid Drum \b\w+\b$'

    $Line =  (Get-Content -LiteralPath $TargetFileName |
        Select-String -Pattern $LinePattern).Line

    if ([string]::IsNullOrEmpty($Line))
        {
        $Line = '__Not Found__'
        }

    [PSCustomObject]@{
        ComputerName = $env:COMPUTERNAME
        LineText = $Line
        }
    }
$IC_Params = @{
    ComputerName = $ComputerNameList
    ScriptBlock = $IC_ScriptBlock
    # comment out the next line to see any errors in the I-C call
    ErrorAction = 'SilentlyContinue'
    }

$Responders = Invoke-Command @IC_Params

$Non_Responders = $ComputerNameList.Where({$_ -notin $Responders.PSComputerName})
# the next line removes unwated properties added by "Invoke-Command"
$Responders = $Responders |
    Select-Object -Property * -ExcludeProperty PSComputerName, PSShowComputerName, RunspaceId

$Responders
'=' * 40
$Non_Responders

如果需要,您可以直接从上述两个创建单个集合。 [咧嘴]

答案 1 :(得分:0)

认为,您要尝试做的是读取列表中所有计算机应具有的文件中的行,该文件位于C:\Users\Public\Activant\Eagle\3log.log

在这种情况下,应执行以下操作:

# use UNC naming for the remote file path
$inputFile  = 'C$\Users\Public\Activant\Eagle\3log.log'  # I'm guessing this is the file you want to read
$outputFile = 'C:\TERMINAL NUMBERS.txt'
$computers  = ***list of computers***                    # the array of computer names

$result = $computers | ForEach-Object { 
    # test if the computer is online
    if (Test-Connection -ComputerName $_ -Count 1 -Quiet) {
        # create the full UNC path by prepending the common file path with the computer name
        $file = '\\{0}\{1}' -f $_, $inputFile

        # test if the file can be found or not
        if (Test-Path -LiteralPath $file -PathType Leaf) {
            # using non regex string search
            $line = (Select-String -LiteralPath $file -Pattern "whatever you're looking for" -SimpleMatch).Line
            if (!$line) {
                # the file is there, but the pattern was not found
                $line = "Pattern not found"
            }
        }
        else {
            $line = "File '$inputFile' not found."
        }
    }
    else {
        $line = 'Computer is Off-Line'
    }

    # Finally, add this info to your text file
    Add-Content -Path $outputFile -Value "$_ --> $line"
    # And emit an object for the $result collection. This will display nicely on screen,
    # but also allow for the creation of a CSV file which might be better as output.
    [PsCustomObject]@{
        'Computer' = $_
        'LogInfo'  = $line
    }
}

之后,您可以阅读输出文本文件,或者(我认为更好)将$ result集合用于输出:

在屏幕上

$result | Format-Table -AutoSize

到CSV文件

$result | Export-Csv -Path 'C:\TERMINAL NUMBERS.csv' -NoTypeInformation -Force
相关问题