【问题标题】:Powershell Get-EventLog from computers.txt and save dataPowershell Get-EventLog 从computer.txt 并保存数据
【发布时间】:2019-08-26 13:28:54
【问题描述】:

我在获取 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)

    }
}

【问题讨论】:

  • 起初,我以为您使用的是“Get-EventLog”,但是您正在执行 WMI 调用,因此您需要使用“调用命令”在远程计算机上运行它。这需要使用该机器的凭据调用,因为该命令需要直接在该机器上运行。

标签: powershell get get-eventlog


【解决方案1】:

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

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 问题,并返回指定过去小时数的 Event Log 信息。

下载: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

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

【讨论】:

    【解决方案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 路径放入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      相关资源
      最近更新 更多