如何通过命令查询域用户所登录的计算机

问题

2020年3月24日星期二 上午4:59

公司是域环境架构,域的服务器是安装win 2019 stand server。平时都是自建excel表;,表;记录了每个域用户所对应的分配给他的计算机。现在为了加快help desk的处理速度,有什么DOS命令或PowerShell 命令,通过命令搜索域用户所对应的登录的计算机?

全部回复 (2)

2020年3月24日星期二 上午9:48

Hi,

针对您提出的问题:

  1. 在AD当中并不会记录域用户和计算机之间的联系,通常来说,一个域用户可以登陆多个计算机,这样的信息只会被存放在计算机日志当中,所以没有直接的命令可以直接完成您的需求。
  2. 我们可以通过powershell脚本来检索合适的计算机日志来获得您想要的信息。
  3. 论坛并不会为您编写脚本,但我们正在努力搜索合适的脚本提供给您使用。

希望对您的疑问有所帮助。

祝好,

Young Yang.


2020年3月26日星期四 上午2:00

Hi,

也许你可以尝试下这个脚本。
脚本中含有三个参数分别是-computer –all –ou。
-computer参数中是获取特定电脑的登陆用户,-ou是搜索此OU运行中所有计算机的所有登录用户,最后一个参数-all提供包含前两个所有的信息。

function Get-UserLogon {
 
[CmdletBinding()]
 
param
 
(
 
[Parameter ()]
[String]$Computer,
 
[Parameter ()]
[String]$OU,
 
[Parameter ()]
[Switch]$All
 
)
 
$ErrorActionPreference="SilentlyContinue"
 
$result=@()
 
If ($Computer) {
 
Invoke-Command -ComputerName $Computer -ScriptBlock {quser} | Select-Object -Skip 1 | Foreach-Object {
 
$b=$_.trim() -replace '\s;',' ' -replace '>','' -split '\s'
 
If ($b[2] -like 'Disc*') {
 
$array= ([ordered]@{
'User' = $b[0]
'Computer' = $Computer
'Date' = $b[4]
'Time' = $b[5..6] -join ' '
})
 
$result;=New-Object -TypeName PSCustomObject -Property $array
 
}
 
else {
 
$array= ([ordered]@{
'User' = $b[0]
'Computer' = $Computer
'Date' = $b[5]
'Time' = $b[6..7] -join ' '
})
 
$result;=New-Object -TypeName PSCustomObject -Property $array
 
}
}
}
 
If ($OU) {
 
$comp=Get-ADComputer -Filter * -SearchBase "$OU" -Properties operatingsystem
 
$count=$comp.count
 
If ($count -gt 20) {
 
Write-Warning "Search $count computers. This may take some time ... About 4 seconds for each computer"
 
}
 
foreach ($u in $comp) {
 
Invoke-Command -ComputerName $u.Name -ScriptBlock {quser} | Select-Object -Skip 1 | ForEach-Object {
 
$a=$_.trim() -replace '\s;',' ' -replace '>','' -split '\s'
 
If ($a[2] -like '*Disc*') {
 
$array= ([ordered]@{
'User' = $a[0]
'Computer' = $u.Name
'Date' = $a[4]
'Time' = $a[5..6] -join ' '
})
 
$result;=New-Object -TypeName PSCustomObject -Property $array
}
 
else {
 
$array= ([ordered]@{
'User' = $a[0]
'Computer' = $u.Name
'Date' = $a[5]
'Time' = $a[6..7] -join ' '
})
 
$result;=New-Object -TypeName PSCustomObject -Property $array
}
 
}
 
}
 
}
 
If ($All) {
 
$comp=Get-ADComputer -Filter * -Properties operatingsystem
 
$count=$comp.count
 
If ($count -gt 20) {
 
Write-Warning "Search $count computers. This may take some time ... About 4 seconds for each computer ..."
 
}
 
foreach ($u in $comp) {
 
Invoke-Command -ComputerName $u.Name -ScriptBlock {quser} | Select-Object -Skip 1 | ForEach-Object {
 
$a=$_.trim() -replace '\s;',' ' -replace '>','' -split '\s'
 
If ($a[2] -like '*Disc*') {
 
$array= ([ordered]@{
'User' = $a[0]
'Computer' = $u.Name
'Date' = $a[4]
'Time' = $a[5..6] -join ' '
})
 
$result;=New-Object -TypeName PSCustomObject -Property $array
 
}
 
else {
 
$array= ([ordered]@{
'User' = $a[0]
'Computer' = $u.Name
'Date' = $a[5]
'Time' = $a[6..7] -join ' '
})
 
$result;=New-Object -TypeName PSCustomObject -Property $array
 
}
 
}
 
}
}
Write-Output $result
}

如果您希望能够预加载自定义模块,请打开PowerShell ISE。将功能复制到您的ISE会话中。在C:\ Program Files \ Windows PowerShell \ Modules中创建一个文件夹,并将代码另存为psm1文件。确保您的文件名和文件夹名匹配。

希望能够对您有所帮助。
祝好,
Young Yang