0

我在获取 EventLog 和保存数据时遇到了一些问题。我能够获取我的事件日志,但不能从网络计算机获取日志。

这是我正在运行的代码:

$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)

    }
}
4

2 回答 2

0

这可以被认为是这个的副本。

在 Powershell 中使用 Get-EventLog 远程读取事件日志

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

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

不要为从头开始写这个而挣扎。好吧,除非它是一个学习练习。有预先构建的脚本供您按原样利用和/或根据需要进行调整。

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

使用 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 在 .csv 文件中获取本地或远程计算机的事件日志

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

下载:eventLogFromRemoteSystem.ps1

再一次,太大了,不能在这里发帖,因为长度和另一个一样。

于 2019-04-04T20:33:55.137 回答
-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 路径放入。

于 2019-04-04T18:32:44.983 回答