How to find PC from list of users

。_饼干妹妹 提交于 2021-01-27 17:22:41

问题


I need some help. I'm not so sure that this possible.

I have list of samAccountName in .csv file, and from this I need to get their PC name and IP.

I'm not so sure how to build script like this.


回答1:


One way of doing this could be to loop through all computers in your environment and test each one. This of course will be SLOW

There is no example of what your CSV file looks like in the question, but if it looks something like this:

    "SamAccountName","title"
    "jdoe","testuser"
    "rboizov","system administrator"

You can do:

# 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

There might be a faster way, but that can only work if all home directories of your users have been redirected to a central server\share. If that is the case in your environment, let me know


Experimental

The below method uses Win32_ServerConnection to find all sessions to user HomeDrive folders.

This can only work if all home directories of your users have been redirected to a central \\server\share, and of course if your permissions allow it

 # 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


来源:https://stackoverflow.com/questions/60829234/how-to-find-pc-from-list-of-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!