Friday, November 13, 2015

PowerCLI script to collect "Computers that have not logged on since specified date"

I needed to collect and Inventory all VM's that have not logged on since a specified date. In my case I was looking for systems that hadn't logged on in the past 90 days. Substitute the "$DaysInactive" with what suits your case.

# Gets time stamps for all computers in the domain that have NOT logged in since after specified date   
$domain = "YouDomain.Name" 
$DaysInactive = 90 
$time = (Get-Date).Adddays(-($DaysInactive))
 
# Get all AD computers with lastLogonTimestamp less than our time
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp |
 
# Output hostname and lastLogonTimestamp into CSV
select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv -path c:\tmp\vmware\LastLogon_90_Days.csv -notypeinformation

No comments:

Post a Comment