简体   繁体   English

搜索登录到AD的OU内的多台计算机的用户的最快方法

[英]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.

相关问题 如何在AD中搜索“计算机” OU中的名称,然后将这些计算机移至正确的位置? - How do i search AD for names like the ones in the “Computers” OU and then move those computers over to their proper location? 在每个AD OU中创建AD计算机组 - Creating AD Groups of computers in each AD OU 对组中不包括 OU 的用户运行 AD 搜索 - Run AD search for users not in Group excluding OU's 需要帮助编写Powershell脚本以在AD用户和计算机>> OU >>分发组中将Extension Attribute 1值更改为“Staff”? - Need help in writing Powershell script to change the Extension Attribute 1 value to ‘Staff’ in AD Users and Computers >> OU >> Distributions groups? 在PowerShell中计算广告用户最快的方法? - Fastest way to count ad users in PowerShell? 将用户/计算机的 OU 添加到安全组 - Add OU of users / computers to a security group 使用powershell在多台计算机上监视特定进程的最快方法 - Fastest way to monitor specific process on multiple computers using powershell Powershell脚本可搜索AD中的特定OU并查找属于组成员的禁用用户 - Powershell Script to search specific OU in AD and find disabled users that is member of a group Powershell在Active Directory中登录用户OU - Powershell get Logged on Users OU in Active Directory Powershell:查找AD中的所有计算机,但排除某些OU - Powershell: Find all computers in AD but exclude certain OU's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM