Contents

Script Dumpster: Online-ADComputers

Contents

The Problem

Today I ran into something simple….but I just wanted to get it solved through PowerShell and make the solution re-usable.

We had found a machine in Active Directory which didn’t turn up in the physical inventory check… And worse of all, the machine was active!

The Script

This script simply checks if machines with a specific name [or in a specific OU] is online at the moment. If it’s online, it will try and figure out who’s actually logged on to the machine.

The output of course is in objects, so you can use whatever kind of formatting on the result of the script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<#
            .SYNOPSIS
                Find specific machines in AD, check if they're online and if so, who's logged on

            .DESCRIPTION
                Find a specific machine in Active Directory using filters.
                If machine is [or multiple machines are] found, check if you can connect to said machine.
                If you can connect to the machine, try and find out who's currently logged on to the machine.
                If noone's logged on, display this as well

            .PARAMETER ComputerName
                The ComputerName you are looking for.
                You can include wildcards in the ComputerName to make sure similarly names machines are also found

            .PARAMETER OU
                In case you want to restrict your search to a specific Organization Unit you can enter the OU's Distinguished Name to limit your search results

            .NOTES
                Name: Online-ADComputer.ps1
                author: "robert" Prüst
                DateCreated: 20-08-2015

            .EXAMPLE
                .\Online-ADComputers -ComputerName CONTOSO-WKS*
                Look for machines named like CONTOSO-WKS
    #>

Param (
    $ComputerName = '*',
    $OU
)


if ($OU) {
        $Computers = Get-ADComputer -Filter {Name -like $ComputerName} -SearchBase $OU
    }

else {

        $Computers = Get-ADComputer -Filter {Name -like $ComputerName}
    }

if ($Computers) {

foreach ($Computer in $Computers){
    $Connection = Test-Connection -Count 1 -Quiet -ComputerName $Computer.Name

    if ($Connection) {
        $user = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $($Computer.Name) -ErrorAction SilentlyContinue | ForEach-Object {$_.UserName}
        if ([string]::IsNullOrEmpty($user)) { $user = 'No User Logged on'}
    }
    else {
        $user = 'Machine turned off'
    }

    $properties = @{'ComputerName'=$Computer.Name;
                    'Online'=$Connection;
                    'User'=$user}


    $obj = New-Object -TypeName PSObject -Property $properties
    $obj

    }
}

else {
	Write-Output "No computers like $ComputerName known in Active Directory"
	}

Happy scripting! 🙂