如何获取特定OU中的计算机列表沿着.csv格式的描述和上次登录用户?
$userName = (Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $machine -ErrorAction:SilentlyContinue).UserName
$DisComp = Get-ADComputer -LDAPFilter "(Name=LN-*)" -SearchBase "OU=Computers - Disabled,DC=XXXXX,DC=com" | Select-Object Name
$results = foreach ($Machine in $DisComp) {
$Description = Get-AdComputer -Identity $Machine -Properties * | Select-Object Description
$UserName
$Machine
$Description
}
$results | Export-Csv -Path C:\XXXXX
2条答案
按热度按时间rta7y2nd1#
定义OU和CSV文件路径
使用Get-ADComputer cmdlet获取OU中的计算机列表
遍历每台计算机并获取描述和上次登录的用户
将计算机数据添加到CSV文件
8i9zcol22#
AFAIK没有名为LastLogonUser的AD计算机属性或任何其他包含此信息的属性。若要获取上次登录的用户,您需要查询该计算机上的windows事件日志,并搜索ID为4672的事件
另外,如果除了返回的默认属性之外,您只想返回Description属性,则不要使用
-Properties *
。试试看:
P.S.您当然需要管理员权限来查询事件日志,因此(如果您不是域管理员)您可能还需要在Get-WinEvent行上使用
-Credential
参数。