views:

918

answers:

2

Hi, I'm building a script to read the Security Log from several computers. I can read the Security log from my local machine with no problem when using the Get-EventLog command, but the problem with it is that I can't run it against a remote machine (the script is for powershell v1). The command below never returns any results, although that with any other LogFile, it works perfectly:

gwmi -Class Win32_NTLogEvent | where {$_.LogFile -eq "Security"}

I've done some research, and I seems to be a impersonation issue, but the -Impersonation option for the Get-WmiObject does not seem to be implemented. Is there anyway around this problem? The solution could be running the Get-EventLog on a remote machine somehow, or dealing with the impersonation issue so that the security log can be accessed. Thanks

+3  A: 

You could use .NET directly instead of going through WMI. The scriptblock below will give you the first entry in the security log

$logs = [System.Diagnostics.EventLog]::GetEventLogs('computername')
$security = $logs | ? {$_.log -like 'Security'} 
$security.entries[0]
Andy Schneider
Works like a charm! Thanks
Pascal
A: 

Have you tried to use the -Credential parameter? Also, use the filter parameter instead of where-object, it gets just the security events (where-object gets ALL events from all logs and only then performs the filtering)

gwmi Win32_NTLogEvent -filter "LogFile='Security'" -computer comp1,comp2 -credential domain\user

Shay Levy
Thanks for the filter tip. You're completely right. As far as the use of the credential goes, I'm local admin, and even fetching the local security event log does not work.
Pascal
for V2 there is a -EnableAllPrivileges switch:gwmi Win32_NTLogEvent -filter "LogFile='Security'" -EnableAllPrivileges
Uros Calakovic