编写脚本来查找用户登录的计算机

编写脚本来查找用户登录的计算机

所以我想指定一个用户名并返回计算机名称。当我运行这个时,它告诉我我的$computersobj 是null。为什么会 $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"
}}}}

答案1

我看到的第一件事是当我运行“Get-ADComputer”时它需要一个过滤器。

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

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

您可能希望在运行之前更改该过滤器。

编辑:抱歉;要查找属性,运行Get-ADComputer | Get-Member它将显示所有属性的列表。这是我使用的第一个 cmdlet。

相关内容