Recently I had to pull together a quick inventory of active workstations on the net, so after a quick google search pulled up Inventorying Computers with AD Powershell, I got a great start on the task.
Here’s what I pulled together:
import-module ActiveDirectory
# Get Domain Info
$ADInfo = Get-ADDomain
$ADDomainDNSRoot = $ADInfo.DNSRootWrite-host “Configuring script options …” `r
[int] $ComputerAge = 90
$ComputerStaleDate = $DateTime.AddDays(-$ComputerAge)# Set the ComputerAge to 90. This defines the oldest date that a computer can be considered “active”
# Since Windows computers automatically update their AD account password every 30 days (by default), this should cover computers that were offline for a short while.# Gather a list of all active workstations in AD including necessary attributes
Write-host “Discovering active workstations in $ADDomainDNSRoot …” `r
$AllActiveWorkstations = Get-ADComputer -filter {(OperatingSystem -notlike “*Server*”) -and (Enabled -eq $TRUE) -and (passwordLastSet -ge $ComputerStaleDate)} -property * | `
Select-Object Name,CanonicalName,Location,OperatingSystem,OperatingSystemVersion,OperatingSystemServicePack,Modified,PasswordLastSet# Set the filter to only return workstations (notlike Server) that are enabled (not disabled), and active (password changed in the last 90 days).
# Get the entire list of computer properties and then select which ones to show in the CSV file. These properties can be identified after -property instead of “*” which reduces the amount of data gathered, but this method makes it easier to add more properties to the CSV by adding them after Select-Object$AllActiveWorkstationsCount = $AllActiveWorkstations.Count
Write-host “”`r
Write-host “There were $AllActiveWorkstationsCount active workstations discovered in $ADDomainDNSRoot …” `r
Write-host “”`r
# Count number of workstations discovered and display.# Do the same again for servers
# Gather a list of all active servers in AD including necessary attributes
Write-host “Discovering active servers in $ADDomainDNSRoot …” `r
$AllActiveServers = Get-ADComputer -filter {(OperatingSystem -like “*Server*”) -and (Enabled -eq $TRUE) -and (passwordLastSet -ge $ComputerStaleDate)} -property * | `
Select-Object Name,CanonicalName,Location,OperatingSystem,OperatingSystemVersion,OperatingSystemServicePack,Modified,PasswordLastSet
$AllActiveServersCount = $AllActiveServers.Count
Write-host “”`r
Write-host “There were $AllActiveServersCount active servers discovered in $ADDomainDNSRoot …” `r
Write-host “”`rWrite-Host “Finished gathering workstation & server data…” `r
Write-host “”`r
Write-host “Writing data to files…”`r
$AllActiveWorkstations | Export-CSV c:\temp\ActiveWorkstations.csv -NoTypeInformation -Encoding UTF8
$AllActiveServers | Export-CSV c:\temp\ActiveServers.csv -NoTypeInformation -Encoding UTF8
Write-host “”`r
Write-host “Operations completed…”`r
Write-host “”`r
This works really well and is fairly lightweight since it only queries AD. Combine this with WMI calls to get computer system data.
Cheers,
Sean