Last active
January 5, 2024 08:22
-
-
Save dotps1/9918522 to your computer and use it in GitHub Desktop.
I know this script can be found in a billion different places, but i wanted to turn it into a cmdlet, so here it is. the 'OU' Param will default to the root of the current domain, so be careful if you have hundereds of workstations that have not logged on in long time. Enjoy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Queries AD DS for workstation accounts that have not logged in for a given amount of time. | |
| .DESCRIPTION | |
| Queries AD DS in spcific OU and uses the LastLogonTimeStamp to determine the last time a computer account has logged on. | |
| The Default Value of is 180 for days a computer account is inactive. | |
| The Default Value of the SearchBase is the root of the domain of the currently logged on user. | |
| .EXAMPLE | |
| Get-ADInactiveWorkstations | |
| In this example, all workstations that have not logged on within 180 days from the root of the current users domain will be returned. | |
| .EXAMPLE | |
| Get-ADInactiveWorkstations -DaysInactive 90 -CsvFile C:\InactiveComputers.csv | |
| In this example, all workstations that have not logged on within 90 days from the root of the current users domain will be returned and then exported to a .csv file. | |
| .EXAMPLE | |
| Get-ADInactiveWorkstations -OU "ou=Computers,dc=MyDomain,dc=org" -Csv "InactiveComputers" | |
| In this example, all workstations that have not logged on within 180 days beggining in the OU named "Computers" in the domain "MyDomain,org" will be returned and then exported to a .csv file. | |
| .NOTES | |
| Requires ActiveDirectory Module available with Remote Server Administration Tools | |
| RSAT 7SP1: http://www.microsoft.com/en-us/download/details.aspx?id=7887 | |
| RSAT 8: http://www.microsoft.com/en-us/download/details.aspx?id=28972 | |
| RSAT 8.1: http://www.microsoft.com/en-us/download/details.aspx?id=39296 | |
| .LINK | |
| http://dotps1.github.io | |
| #> | |
| function Get-ADInactiveWorkstations | |
| { | |
| [CmdletBinding()] | |
| [OutputType([PSObject])] | |
| Param | |
| ( | |
| # DaysInactive, Type Int, The number of days a computer account has not logged in. | |
| [Parameter(Position = 0, | |
| HelpMessage = "Amount of time, in Days, that have passed since the workstations has logged on.")] | |
| [Int] | |
| $DaysInactive = 180, | |
| # OrganizationalUnit, Type String, The LDAP OU String to enumerate. | |
| [Alias("OU")] | |
| [Parameter(Position = 1, | |
| ValueFromPipeLine = $true, | |
| HelpMessage = "The top most Organizational Unit to recursvily search from.")] | |
| [String] | |
| $OrganizationalUnit = "dc="+($env:USERDNSDOMAIN).Split('.')[0]+",dc="+($env:USERDNSDOMAIN).Split('.')[1], | |
| # CsvFile, Type String, Location to output report in csv format to. | |
| [Alias("Csv")] | |
| [ValidateScript({ if (-not(Test-Path (($_ -split '\\')[0..(($_ -split '\\').count -2)] -join '\'))){ throw "$_ is not a valid path." } else { return $true } })] | |
| [Parameter(Position = 2, | |
| HelpMessage = "The Fully Quallifed path to place the output of a this cmdlet into a CSV.")] | |
| [String] | |
| $CsvFile | |
| ) | |
| Begin | |
| { | |
| if (-not(Get-Module -Name ActiveDirectory)) | |
| { | |
| try | |
| { | |
| Import-Module -Name ActiveDirectory -ErrorAction Stop | |
| } | |
| catch | |
| { | |
| throw "ActiveDirectory Module is not available on this computer, and this cmdlet cannot be used. Use Get-Help Get-ADInactiveWorkstations -Full and see the .NOTE section for links about RSAT." | |
| } | |
| } | |
| } | |
| Process | |
| { | |
| $dateTimeEnum = (Get-Date).AddDays(-($DaysInactive)) | |
| $workstations = Get-ADComputer -Filter { (LastLogonTimeStamp -lt $dateTimeEnum) -and (OperatingSystem -notlike "Windows Server*") } -SearchBase $OrganizationalUnit -Properties LastLogonTimeStamp,OperatingSystem | | |
| Select-Object Name,OperatingSystem,@{ Name = "LastLogonTimeStamp";Expression = { [DateTime]::FromFileTime($_.LastLogonTimeStamp) } } | | |
| Sort-Object LastLogonTimeStamp | |
| return $workstations | |
| } | |
| End | |
| { | |
| if ($PSBoundParameters.ContainsValue($CsvFile)) | |
| { | |
| if (-not($CsvFile.ToString().EndsWith(".csv"))) | |
| { | |
| $CsvFile += ".csv" | |
| } | |
| $workstations | Export-Csv -Path $CsvFile -NoTypeInformation | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment