【发布时间】:2022-01-23 13:08:22
【问题描述】:
我需要将我域中所有 PC 的一些属性导出到 *.csv 表。 lastLogOn 是必需的属性之一。问题是我有两个域控制器,所以我需要从中选择最新的 lastLogOn。
我有一个解决方案,但需要很长时间(大约 1 分钟)才能为我提供最终的计算机阵列。这里是:
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
getComputers功能每1个DC大约需要1秒,主要问题是选择lastLogon最新的PC。
有没有更快的解决方案?
【问题讨论】:
-
另外 (
Sort samAccountName) 并且进行并排连接是非常不确定的,因为您可能只是在一个 DC 上创建了一台尚未复制到其他 DC 的计算机。如果没有Sort并使用Join-Object script/Join-Object Module(另请参阅:In Powershell, what's the best way to join two tables into one?),您可能会执行以下操作:$compsDC1 |Join $compsDC2 -on samAccountName -where { $Left.lastLogOn -ge $Right.lastLogOn }
标签: powershell active-directory