5.19.2014

Create an Powershell Query to get count of workstations and servers

Create an Powershell Query to get count of workstations and servers


If copying and pasting ensure to check quotation marks as they sometimes need to be changed from the web.

Set-ExecutionPolicy Unrestricted

import-module ActiveDirectory
# Get Domain Info
$ADInfo = Get-ADDomain
$ADDomainDNSRoot = $ADInfo.DNSRoot
Write-host "Configuring script options"
[int] $ComputerAge = 90
$DateTime = get-date
$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 ""`r
Write-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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.