从computers.txt获取Powershell Get-EventLog并保存数据

时间:2019-04-04 17:34:36

标签: powershell get get-eventlog

获取EventLog和保存数据时遇到一些问题。我可以获取我的EventLogs,但不能从网络计算机上获取日志。

这是我正在运行的代码:

$logFileName = "Application" 
$path = $MyInvocation.MyCommand.Path +"\Output\" 
$path = $PSScriptRoot+"\Output\"

new-item $path -ItemType directory

$array = ("System", "Security")

$file = $PSScriptRoot +"\computers.txt"


$users = ForEach ($machine in $(Get-Content $file)) {

    $pathMachine = $path+$machine
    new-item $pathMachine -ItemType directory
    ForEach ($logFileName in $array){
        # do not edit


        $logFileName
        $exportFileName = (get-date -f yyyyMMdd) + "_" + $logFileName +  ".evt"
        $logFile = Get-WmiObject Win32_NTEventlogFile -ComputerName $machine | Where-Object {$_.logfilename -eq $logFileName}
        $logFile
        $exportFileName
        $pathMachine
        $temp = $pathMachine + "\"+ $exportFileName
        $temp
        $fff = $logFile.BackupEventLog($temp)

    }
}

2 个答案:

答案 0 :(得分:0)

这可能被认为是重复的。

Reading event log remotely with Get-EventLog in Powershell

# swapped from this command
get-eventlog -LogName System -computername <ServerName>

# to this
invoke-command {get-eventlog -LogName System} -ComputerName <ServerName>

不要从头开始编写。好吧,除非这是一项学习练习。有预构建的脚本供您按需使用,也可以根据需要进行调整。

在远程主机上运行命令需要使用Invoke cmdlet和/或与该主机建立的PSRemoting会话。

Get Remote Event Logs With Powershell

使用wmi,备用凭据和多个运行空间收集一个或多个系统的远程事件日志信息。如果出现wmi问题,该函数支持自定义超时参数,并返回指定小时数内的事件日志信息。

下载:Get-RemoteEventLogs.ps1

该脚本太长(超过100行),无法在此处发布,但在摘要中位于此处。

Function Get-RemoteEventLogs 
{ 
    <# 
    .SYNOPSIS 
       Retrieves event logs via WMI in multiple runspaces. 
    .DESCRIPTION 
       Retrieves event logs via WMI and, if needed, alternate credentials. This function utilizes multiple runspaces. 
    .PARAMETER ComputerName 
       Specifies the target computer or comptuers for data query. 
    .PARAMETER Hours 
       Gather event logs from the last number of hourse specified here. 
    .PARAMETER ThrottleLimit 
       Specifies the maximum number of systems to inventory simultaneously  
    .PARAMETER Timeout 
       Specifies the maximum time in second command can run in background before terminating this thread. 
    .PARAMETER ShowProgress 
       Show progress bar information 
    .EXAMPLE 
       PS > (Get-RemoteEventLogs).EventLogs 

       Description 
       ----------- 
       Lists all of the event logs found on the localhost in the last 24 hours. 

    .NOTES 
       Author: Zachary Loeber 
       Site: http://www.the-little-things.net/ 
       Requires: Powershell 2.0 

       Version History 
       1.0.0 - 08/28/2013 
        - Initial release 
    #> 

或者这个。

PowerShell To Get Event Log of local or Remote Computers in .csv file

当您要从远程或本地计算机提取事件日志时,此脚本非常方便。它具有多个过滤器,将有助于过滤数据。您可以按日志名称,事件类型,来源等进行过滤。这也具有根据日期范围获取数据的功能。您可以更改

下载:eventLogFromRemoteSystem.ps1

同样,太大了,无法在此处发布,因为长度与另一个相似。

答案 1 :(得分:-2)

我正在做一些假设,但这也许会有所帮助。

当我运行您的代码时,我得到了

Get-Content : Cannot find path 'C:\computers.txt' because it does not exist.

我必须制作C:\computers.txt文件,然后再次运行您的代码并收到此错误。

Get-Content : Cannot find path 'C:\Output\computers.txt' because it does not exist.

我在该位置创建了该文件,然后再次运行您的代码,并获得了事件日志文件。也许尝试使用类似命令

来创建这两个丢失的文件
Get-WmiObject Win32_NTEventlogFile -ComputerName $machine
mkdir C:\Output\$machine
$env:computername | Out-File -FilePath c:\Output\Computers.txt

您可能还需要设置网络共享并输出到该位置,以便可以从单台计算机访问事件日志。设置共享和权限后,只需将unc路径放入其中即可。