[英]Fastest way to search for users logged into multiple computers within an OU of AD
Im trying to create a powershell script where you enter a username and it will parse all machines in a specific ou and see if they are logged into multiple machines. 我试图创建一个powershell脚本,您输入一个用户名,它将解析特定的所有机器,看看它们是否登录到多台机器。
I am not quite sure whether WMI, CIM or query will provide me with the fastest query to see if a user will be logged into additional machines that could prevent account lockout in AD. 我不太确定WMI,CIM或查询是否会为我提供最快的查询,以查看用户是否将登录可以防止AD中的帐户锁定的其他计算机。
What is the best way to enter an a username, then have it scan a specific OU and output the results to a .csv file? 输入用户名的最佳方法是什么,然后让它扫描特定的OU并将结果输出到.csv文件?
Thanks 谢谢
If in your AD environment you have a share on a server where all user home directories are stored, this may be a solution. 如果在AD环境中,您在存储所有用户主目录的服务器上拥有共享,则这可能是一种解决方案。
It connects to the server and gets a list of user connections with that home directories share. 它连接到服务器并获取与该主目录共享的用户连接列表。
Using that list, it looks for connections made by the given user and tests if the computer the connection was made from is in the given OU. 使用该列表,它查找给定用户所做的连接,并测试连接所在的计算机是否在给定的OU中。
$ouDN = 'THE DISTINGHUISHED NAME OF THE OU'
$homeDirServer = 'THE NAME OF THE SERVER WHERE THE USER HOME DIRECTORIES ARE KEPT'
$homeDirShare = 'THE NAME OF THE SHARED ROOT FOLDER OF THE USERS HOME DIRECTORIES'
$userName = 'SAMACCOUNTNAME OF THE USER TO SEARCH FOR'
$outputFile = 'THE PATH AND FILENAME OF THE EXPORTED CSV FILE'
# first get a list of computernames in the given OU
$computers = Get-ADComputer -Filter * -SearchBase $ouDN | Select-Object -ExpandProperty SamAccountName
# next, get user connections on the homedir share and loop through them
Get-CimInstance -Class Win32_ServerConnection -ComputerName $homeDirServer |
# or use WMI:
# Get-WmiObject -Class Win32_ServerConnection -ComputerName $homeDirServer |
Where-Object { $_.ShareName -eq $homeDirShare -and $_.UserName -eq $userName } |
# if you want a list of all user connections, use this instead:
# Where-Object { $_.ShareName -eq $homeDirShare -and (!$($_.UserName).EndsWith("$")) } |
ForEach-Object {
# get the computername from the IP Address you usually get in '$_.ComputerName'
$computerName = (([System.Net.Dns]::GetHostEntry($_.ComputerName).HostName) -split "\.", 2)[0]
# is this a computer in the given OU?
if ($computers -contains $computerName) {
# emit an object
[PSCustomObject]@{
'DisplayName' = Get-ADUser -Identity $_.UserName -Properties DisplayName | Select-Object -ExpandProperty DisplayName
'AccountName' = $_.UserName
'ComputerName' = $computerName
}
}
} | Export-Csv -Path $outputFile -NoTypeInformation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.