We are receiving notifications of stale AD computer accounts, but those reports seem to be faulty and I got asked to create an application that would basically re-check the results. I am using jobs to check every computer, so that the process does not freeze the form. What I have is basically a Start button and a timer, as follows:
Code: Select all
$buttonStart_Click={
if ($datagridview1.DataSource)
{
$global:Computers = $datagridview1.DataSource
$timerJobs.Start()
$buttonOpenExcel.Enabled = $false
$buttonOpenExcelAndAppend.Enabled = $false
$buttonClearTable.Enabled = $false
$buttonStart.Text = 'Working ...'
$buttonStart.Enabled = $false
$datagridview1.Cursor = 'WaitCursor'
foreach ($Computer in $global:Computers)
{
$checkJob = Start-Job { Get-ADComputer $Computer."Computer Name" -Properties PasswordLastSet, LastLogonDate }
$Computer | Add-Member -NotePropertyName "CheckJob" -NotePropertyValue $checkJob -Force
}
}
}
$timerJobs_Tick={
foreach ($Computer in $global:Computers)
{
if (($Computer.CheckJob.State -eq 'Completed') -and $Computer.CheckJob.HasMoreData)
{
$results = Receive-Job $Computer.CheckJob
$Computer."Checked PW Last Set" = ((Get-Date) - $results.PasswordLastSet).Days
$Computer."Checked Last Logon" = ((Get-Date) - $results.LastLogonDate).Days
Remove-Job $Computer.CheckJob
}
}
Update-DataGridView -DataGridView $datagridview1 -Item $global:Computers
if ('N/A' -notin $global:Computers."Checked PW Last Set")
{
Get-Job | Remove-Job -Force
$buttonOpenExcel.Enabled = $true
$buttonOpenExcelAndAppend.Enabled = $true
$buttonClearTable.Enabled = $true
$buttonStart.Text = 'Start!'
$buttonStart.Enabled = $true
$datagridview1.Cursor = 'Default'
$timerJobs.Stop()
}
}
Code: Select all
$PCList = New-Object System.Data.DataTable
$PCList.Columns.Add("Computer Name")
$PCList.Columns.Add("OU Location")
$PCList.Columns.Add("Password Last Set")
$PCList.Columns.Add("Last Logon TimeStamp")
$PCList.Columns.Add("Checked PW Last Set")
$PCList.Columns.Add("Checked Last Logon")
foreach ($row in $ExcelFile)
{
$PCList.Rows.Add($row."Computer Name", $row."OU Location", $row."Password Last Set", $row."Last Logon TimeStamp", 'N/A', 'N/A')
}
Code: Select all
ERROR: Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
ERROR: + CategoryInfo : InvalidData: (:) [Get-ADComputer], ParameterBindingValidationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
ERROR: + PSComputerName : localhost
Thanks!