编写脚本来查找用户login到的计算机

所以我想指定一个用户名并返回计算机名称。 当我运行这个,它告诉我,我的$computers obj是null 。 为什么$computers = Get-ADComputer | where {$_.accountdisabled -eq $false} $computers = Get-ADComputer | where {$_.accountdisabled -eq $false}返回null

我的脚本到目前为止是这样的:

 Function Get-Username{ $Global:Username = read-Host "Enter a username" if ($Username -eq $null){ Write-Host "Username can't be blank. Enter username" Get-Username } $UserCheck = Get-ADUser -Filter 'Name -like $Username' | FT Name, SamAccountName -A if($UserCheck -eq $null){ Write-Host "Invalid username, enter username" Get-Username } } Get-username $computers = Get-ADComputer | where {$_.accountdisabled -eq $false} foreach($comp in $computers){ $Computer = $comp.Name $ping = New-Object System.Net.NetworkInformation.Ping $Reply = $null $Reply = $ping.send($Computer) if($Reply.status -like 'Success'){ $proc = gwmi win32_process -computer $Computer -Filter "Name = 'explorer.exe'" ForEach($p in $proc){ $temp = ($p.GetOwner()).User if($temp -eq $Username){ Write-Host "$Username is logged on $Computer" }}}} 

我看到的第一件事是当我运行“Get-ADComputer”时,它想要一个filter。

filter应用后,我仍然没有看到Get-AdComputer的“accountdisabled”属性,我想你的意思是“启用”

 $computers = Get-ADComputer -Filter * | where {$_.Enabled -eq $false} 

运行之前,您可能需要更改该filter。

编辑:对不起; find运行Get-ADComputer | Get-Member的属性 Get-ADComputer | Get-Member会显示所有属性的列表。这是我使用的第一个cmdlet。