Если в вашей среде AD у вас есть общий ресурс на сервере, где хранятся все домашние каталоги пользователей, это может быть решением.
Он подключается к серверу и получает список пользовательских подключений с этим домом.общий доступ к каталогам.
Используя этот список, он ищет соединения, сделанные данным пользователем, и проверяет, находится ли компьютер, с которого было установлено соединение, в данной 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