I'm trying to generate a custom report based off the return values of a PowerShell script. I've tried looking over the tutorial docs and SDK, but I can't seem to piece it all together.
The function below accepts a given user's samAccountName, then searches the AD Security logs to find the date/time and source host that's causing lockouts on their account.
Overall, I want to provide a search box for an admin to lookup a given user in Adaxes, then pass that as a parameter value to the function. At the end, I want a report that displays the infromation that's being returned from PowerShell (i.e. Username, Source Host, Date/Time of Event).
function Get-LockoutBlame
{
[CmdletBinding(DefaultParameterSetName = 'Filtered')]
[OutputType('System.Diagnostics.Eventing.Reader.EventLogRecord')]
param
(
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty()]
[string] $UserName,
[Parameter(ParameterSetName = 'Filtered')]
[ValidateNotNullOrEmpty()]
[int] $PastHours = 1,
[Parameter(ParameterSetName = 'Unfiltered')]
[switch] $All,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string] $ComputerName = ((Get-ADDomainController -Discover -Service PrimaryDC).HostName)
)
process
{
try
{
$filter = '*[System[EventID=4740'
if (!$all)
{
$PastMilliseconds = $PastHours * 3600000
$filter += " and TimeCreated[timediff(@SystemTime) <= $PastMilliseconds]]"
}
else
{
$filter += ']'
}
if ($username)
{
$filter += " and EventData[Data[@Name='TargetUserName']='$UserName']]"
}
else
{
$filter += ']'
}
$Events = Get-WinEvent -ComputerName $ComputerName -LogName Security -FilterXPath $filter
$Events | Select-Object TimeCreated,
@{Name = 'User Name'; Expression = { $_.Properties[0].Value } },
@{Name = 'Source Host'; Expression = { $_.Properties[1].Value } }
}
catch
{
Throw $_.Exception
}
}
}