如何从用户列表中查找 PC
How to find PC from list of users
我需要一些帮助。我不太确定这是否可能。
我在 .csv
文件中有 samAccountName
的列表,我需要从中获取他们的 PC 名称和 IP。
我不太确定如何构建这样的脚本。
执行此操作的一种方法是遍历您环境中的所有计算机并测试每一台计算机。这当然会 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
可能有更快的方法,但只有当您的用户的所有主目录都已重定向到中央 server\share 时,该方法才有效。如果您的环境是这种情况,请告诉我
实验性
以下方法使用 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
我需要一些帮助。我不太确定这是否可能。
我在 .csv
文件中有 samAccountName
的列表,我需要从中获取他们的 PC 名称和 IP。
我不太确定如何构建这样的脚本。
执行此操作的一种方法是遍历您环境中的所有计算机并测试每一台计算机。这当然会 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
可能有更快的方法,但只有当您的用户的所有主目录都已重定向到中央 server\share 时,该方法才有效。如果您的环境是这种情况,请告诉我
实验性
以下方法使用 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