Create an Powershell Query to get count of workstations and servers
Re-posting from: http://blogs.metcorpconsulting.com/tech/?p=88
If copying and pasting ensure to check quotation marks as they sometimes need to be changed from the web.
import-module ActiveDirectory# Get Domain Info$ADInfo = Get-ADDomain$ADDomainDNSRoot = $ADInfo.DNSRootWrite-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 attributesWrite-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.CountWrite-host ""`rWrite-host "There were $AllActiveWorkstationsCount active workstations discovered in $ADDomainDNSRoot …" `rWrite-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 attributesWrite-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.CountWrite-host ""`rWrite-host "There were $AllActiveServersCount active servers discovered in $ADDomainDNSRoot …" `rWrite-host ""`rWrite-Host "Finished gathering workstation & server data…" `rWrite-host ""`rWrite-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 UTF8Write-host ""`rWrite-host "Operations completed…"`rWrite-host ""`r
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.