Одним из способов сделать это может быть l oop на всех компьютерах в вашей среде и проверка каждого. Это, конечно, будет SLOW
Нет примера того, как ваш CSV-файл выглядит в вопросе, но если он выглядит примерно так:
"SamAccountName","title"
"jdoe","testuser"
"rboizov","system administrator"
Вы можете сделать:
# get an aray if SamAccountNames from the Csv
$userNames = Import-Csv -Path 'PathToYOurCsvFile' | Select-Object -ExpandProperty SamAccountName
# get all computer objects from AD and loop through
# this of course can take a LONG time to finish..
$result = Get-ADComputer -Filter * | ForEach-Object {
Write-Host "Testing computer $($_.Name)"
if (Test-Connection -ComputerName $_.Name -Count 1 -Quiet) {
$pc = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $_.Name
# or use: $pc = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $_.Name
$user = ($pc.UserName -split '\\')[-1]
if ($userNames -contains $user) {
[PSCustomObject]@{
'SamAccountName' = $user
'ComputerName' = $pc.Name
'IP' = (Test-Connection -ComputerName $pc.Name -Count 1).IPV4Address.IPAddressToString
}
}
}
else {
Write-Warning "Computer $($_.Name) could not be reached"
}
}
#output in console
$result
# output to Csv
$result | Export-Csv -Path 'UsersOnComputers.csv' -NoTypeInformation
Возможно, существует более быстрый способ, но он может работать, только если все домашние каталоги ваших пользователей были перенаправлены на центральный сервер \ общий ресурс. Если это так в вашей среде, дайте мне знать
Экспериментальный
Приведенный ниже метод использует Win32_ServerConnection
для поиска всех сеансов для пользователя HomeDrive папки.
Это может работать, только если все домашние каталоги ваших пользователей были перенаправлены на центральный \\server\share
, и, конечно, если ваши разрешения позволяют это
# the UNC \\Server\Share name of the network share where all user homedirectories are
$usersHomePath = '\\HomesServer\HomesShare$'
# get an aray if SamAccountNames from the Csv
$userNames = Import-Csv -Path 'PathToYOurCsvFile' | Select-Object -ExpandProperty SamAccountName
# split the UNC path to get the server name and share name separate
$svr = $usersHomePath.TrimStart("\") -split '\\' #"# fix syntax highlighting for SO..
$server = $svr[0]
$share = $svr[-1]
$result = Get-CimInstance -ClassName Win32_ServerConnection -ComputerName $server | # or use: Get-WmiObject -Class Win32_ServerConnection
Where-Object { $_.ShareName -eq $share -and $userNames -contains $_.UserName } |
Select-Object @{Name = "SamAccountName"; Expression = { $_.UserName }},
@{Name = "ComputerName"; Expression = {(([System.Net.Dns]::GetHostEntry($_.ComputerName).HostName) -split "\.")[0]}},
@{Name = "IP"; Expression = { (Test-Connection -ComputerName $_.ComputerName -Count 1).IPV4Address.IPAddressToString }} |
Sort-Object SamAccountName
#output in console
$result
# output to Csv
$result | Export-Csv -Path 'UsersOnComputers.csv' -NoTypeInformation