Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created January 22, 2020 18:31
Show Gist options
  • Save JustinGrote/4ef0576c224a3761a5dece2316ca7de6 to your computer and use it in GitHub Desktop.
Save JustinGrote/4ef0576c224a3761a5dece2316ca7de6 to your computer and use it in GitHub Desktop.
Find all Enabled Servers in Active Directory that have checked in within a certain timeframe
#Requires -module ActiveDirectory
function Get-ActiveADServers {
param(
[String]$SearchBase,
[String]$Server,
[PSCredential]$Credential,
[DateTime]$After = (get-date).AddMonths(-1)
)
$GetADComputerParams = @{
Filter = {
enabled -eq $true -and
operatingsystem -like '*server*' -and
lastlogondate -gt $After
}
SearchBase = $SearchBase
Property = 'lastLogonDate', 'operatingsystem'
Credential = $Credential
}
('SearchBase','Server','Credential').foreach{
$optionalValue = (Get-Variable $PSItem -ErrorAction SilentlyContinue -ValueOnly)
if ($optionalValue) {$GetADComputerParams.$PSItem = $OptionalValue}
}
Get-AdComputer @GetAdComputerParams |
Select lastlogondate, operatingsystem, dnshostname, distinguishedname
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment