I need to export some properties of all PCs of my domain to *.csv table. One of the necessary properties is lastLogOn. The problem is that i have two Domain Controllers, so I need to choose the latest lastLogOn from them.
I have one solution, but it takes really a lot of time (about ~ 1 min) to give me a final array of computers. Here it is:
function getComputers([string]$dc) {
return Get-ADComputer -SearchBase ‘DC=mydomain,DC=com’ -Server $dc -Filter * `
-Properties name, samAccountName, DistinguishedName, lastLogOn, OperatingSystem | `
Sort samAccountName
}
function getComputersFromsBothDCs {
$compsDC1 = getComputers 'dc1'
$compsDC2 = getComputers 'dc2'
$comps = @()
for ($i = 0; $i -le $compsDC1.Length - 1; $i++) {
$comp1 = $compsDC1[$i]
$comp2 = $compsDC2[$i]
if ($comp1.lastLogOn -ge $comp2.lastLogOn) {
$comps += $comp1
} else {
$comps += $comp2
}
}
return $comps
}
$comps = getComputersFromsBothDCs
# Then export and some other stuff
Function getComputers takes about 1 second per 1 DC, main problem is in choosing the PC with latest lastLogon.
Are there any faster solutions?