Created
October 3, 2024 15:47
-
-
Save qcomer/5358905caeb5467059076cfe4afd4c35 to your computer and use it in GitHub Desktop.
aaa
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
| param( | |
| [parameter(mandatory = $false, position = 0)] | |
| [string]$GlobalWhitelist = '{%^tp:whitelist_localadmin^%}', | |
| [parameter(mandatory = $false, position = 1)] | |
| [string]$ClientWhiteList = '{%^el:whitelist_staleusers^%}', | |
| [parameter(mandatory = $false, position = 2)] | |
| [string]$ClientWhiteList_wks = '{%^el:whitelist_staleusers_wks^%}' | |
| ) | |
| $deviceType = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty ProductType | |
| $whitelist = @() | |
| $whitelist += $globalwhitelist.split(',') | |
| if ($deviceType -ne 1 -and $devicetype -ne 2) { | |
| $whitelist += $clientwhitelist.split(',') | |
| } else { | |
| $whitelist += $ClientWhiteList_wks.split(',') | |
| } | |
| $whitelistString = $whitelist -join ',' | |
| TRY { | |
| if ($deviceType -eq 2) { | |
| Write-Output 'SUCCESS: Computer is Domain Controller. Exiting.' | |
| } | |
| $Results = @() | |
| $Threshold = 60 | |
| $Users = Get-LocalUser | Where-Object { $_.Lastlogon -le (Get-Date).AddDays(-$Threshold) -and $_.enabled -eq $true -and $_.name -notlike 'AEAdmin*' -and $_.name -NotLike 'QBData*' } | Select-Object Name, SID, Lastlogon | |
| if ($GlobalWhitelist -notmatch 'whitelist') { | |
| $users = $users | Where-Object { $_.name -notmatch $whiteliststring } | |
| } | |
| if (-not($Users)) { | |
| Write-Output 'Success: No Users Found' | |
| } else { | |
| foreach ($U in $users) { | |
| $User = [PSCustomObject][ordered]@{ | |
| Name = $U.Name | |
| LastLogin = switch ($U.Lastlogon) { | |
| { $null -eq $_ } { | |
| 'Never' | |
| } | |
| default { | |
| $U.LastLogon | |
| } | |
| } | |
| } | |
| $Results += "$($user.name) ($($User.lastlogin))" | |
| } | |
| } | |
| if (-not($Results)) { | |
| Write-Output 'Success: No Users Found' | |
| } else { | |
| Write-Output "ERROR: $($Results -join ';')" | |
| } | |
| } catch { | |
| Write-Output "WARNING: $($_.exception.message)" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment