> I have an excel sheet with OU, Source network address and timestamp unique records.
> I would like to use the CSV file to read data and would like to go to each IP/source network address and fetch data from the event viewer for event id 4624 in Security logs that occurred at the same time stamp corresponding to that IP address.
> I would also like to detect the OS on the IP address and look for event id as per the OS. Windows server 2003 it is event id 540 and for Windows 2008 onwards it is event 4624
> I would like a powershell script using Get-winevent and Filter XML. If an IP address is not reachable it should go to the next IP and mention not reachable.
> I have a the following script which gives me errors.
PLEASE HELP!!
$logFile = 'C:\test\test.csv'
$myOU = "OU=ABC,dc=contosso,DC=com"
$Computer = "Server1"
$logontype = @{ # LogonType as per technet
2 = "Interactive"
3 = "Network"
4 = "Batch"
5 = "Service"
7 = "Unlock"
8 = "NetworkCleartext"
9 = "NewCredentials"
10 = "RemoteInteractive"
11 = "CachedInteractive"
}
$time = import-csv -path C:\test\Results.csv | select-object timecreated
$OU = import-csv -path C:\test\Results.csv | select-object OU
$data = import-csv -path C:\test\Results.csv | select-object sourcenetworkaddress
ForEach ($server in $data)
{
Get-WinEvent -FilterXml "<QueryList><Query Id=""0"" Path=""Security""><Select Path=""Security"">*[System[EventID=4624 and TimeCreated -eq [$time]]]</Select><Suppress
Path=""Security"">*[EventData[Data[@Name=""SubjectLogonId""]=""0x0"" or Data[@Name=""TargetDomainName""]=""NT AUTHORITY"" or Data[@Name=""TargetDomainName""]=""Window
Manager""]]</Suppress></Query></QueryList>" -ComputerName $Server |
ForEach-Object {
$Event = $_ | select *
$curOU = ([ADSI]"LDAP://<SID=$(($Event.Properties[4]).Value.Value)>").distinguishedName # TargetUserSid
If (( $curOU -like $OU" ) -AND (($logontype[ [int] $Event.Properties[8].Value ]) -like "Interactive")) {{
$Props = @{
OU = $curOU.ToString();
LogonType = $logontype[ [int] $Event.Properties[8].Value ];
TimeCreated = $Event.TimeCreated;
SourceNetworkAddress = $Event.Properties[18].Value
}
$LogonRecord = New-Object -TypeName psobject -Property $Props
$Result += $LogonRecord
}
}
}
$Result | Export-Csv -Path $logFile -append -UseCulture -NoTypeInformation # Log it to CSV