如何从用户列表中查找PC [英] How to find PC from list of users

查看:71
本文介绍了如何从用户列表中查找PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助.我不太确定是否有可能.

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

我在 .csv 文件中有 samAccountName 的列表,因此我需要获取其PC名称和IP.

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.

推荐答案

一种方法是遍历环境中的所有计算机并测试每台计算机.这当然是

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

在问题中没有关于您的CSV文件外观的示例,但是如果它看起来像这样:

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"

您可以这样做:

# 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文件夹的所有会话.

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

仅当您的用户的所有主目录都已重定向到中央 \\ server \ share ,并且当然在您的权限允许的情况下,此功能才可用

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

这篇关于如何从用户列表中查找PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆