PowerShell远程事件日志解析

时间:2015-08-06 17:13:37

标签: powershell csv active-directory

我编写了一个小脚本来从远程计算机中获取事件日志条目并将其写入.csv文件。该脚本在定位单个计算机时有效,但是当我尝试实现for循环并将其循环到Active Directory中的所有计算机上时,我收到此错误:

Method invocation failed because [Microsoft.ActiveDirectory.Management.ADComputer] 
does not contain a method named 'op_Addition'.
At Y:\srp.ps1:7 char:143
+ ... | Export-Csv $($computer + ".csv")
+                    ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Export-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty. 
Provide an argument that is not null or empty, and then try the command again.
At Y:\srp.ps1:7 char:141
+ ... 0 | Export-Csv $($computer + ".csv")
+                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Export-Csv],
                              ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,
                              Microsoft.PowerShell.Commands.ExportCsvCommand

错误表明Export-Csv命令存在问题,但单独运行该命令会创建所需的日志文件。这是完整的脚本,供参考:

# Gets SRP event log entries from remote machine and writes them to a .csv file 
# of the same name.

Write-Output "Running..."

$computers = Get-ADComputer -filter {(Name -like "PC*") -or (Name -like "LT*")}
foreach ($computer in $computers) {
    Get-EventLog -LogName Application -Source Microsoft-Windows-SoftwareRestrictionPolicies 
    -ComputerName $computer -Newest 10 | Export-Csv $($computer + ".csv")
} #end foreach

Write-Host "Done."

当我尝试在AD中循环计算机时,是否出现此错误的原因?

1 个答案:

答案 0 :(得分:0)

看起来Get-ADComputer会返回ADComputer个对象,但是您将它传递给Get-EventLog的{​​{1}}参数,该参数带有一个字符串,按原样。我假设你需要从ComputerName对象中获取name属性。

相关问题