Skip to content

Instantly share code, notes, and snippets.

@qcomer
Created October 3, 2024 15:47
Show Gist options
  • Select an option

  • Save qcomer/5358905caeb5467059076cfe4afd4c35 to your computer and use it in GitHub Desktop.

Select an option

Save qcomer/5358905caeb5467059076cfe4afd4c35 to your computer and use it in GitHub Desktop.
aaa
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