如何从用户列表中查找PC

2024-04-22

我需要一些帮助。我不太确定这是否可能。

我有清单samAccountName in .csv文件,我需要从中获取他们的 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

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


实验性的

下面的方法使用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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从用户列表中查找PC 的相关文章

随机推荐