Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Param
- (
- )
- Write-Verbose "Get current Active Directory domain... "
- $ADForestInfo = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
- $ADForestInfoRootDomain = $ADForestInfo.RootDomain
- $ADForestInfoRootDomainDN = "DC=" + $ADForestInfoRootDomain -Replace("\.",',DC=')
- $ADDomainInfoLGCDN = 'GC://' + $ADForestInfoRootDomainDN
- Write-Verbose "Discovering service account SPNs in the AD Forest $ADForestInfoRootDomainDN "
- $root = [ADSI]$ADDomainInfoLGCDN
- $ADSearcher = new-Object System.DirectoryServices.DirectorySearcher($root,"(serviceprincipalname=kadmin/changepw)")
- $ADSearcher.PageSize = 5000
- $AllADKRBTGTAccountSPNs = $ADSearcher.FindAll()
- $AllADKRBTGTAccountSPNsCount = $AllADKRBTGTAccountSPNs.Count
- Write-Output "Processing $AllADKRBTGTAccountSPNsCount service accounts (user accounts) with SPNs discovered in AD Forest $ADForestInfoRootDomainDN `r "
- $AllKRBTGTAccountReport = $Null
- ForEach ($AllADKRBTGTAccountSPNsItem in $AllADKRBTGTAccountSPNs)
- {
- $KRBTGTAccountsItemDomain = $Null
- [array]$AllADKRBTGTAccountSPNsItemDNArray = ($AllADKRBTGTAccountSPNsItem.Properties.distinguishedname) -Split(",DC=")
- [int]$DomainNameFECount = 0
- ForEach ($AllADKRBTGTAccountSPNsItemDNArrayItem in $AllADKRBTGTAccountSPNsItemDNArray)
- {
- IF ($DomainNameFECount -gt 0)
- { [string]$KRBTGTAccountsItemDomain += $AllADKRBTGTAccountSPNsItemDNArrayItem + "." }
- $DomainNameFECount++
- }
- $KRBTGTAccountsItemDomain = $KRBTGTAccountsItemDomain.Substring(0,$KRBTGTAccountsItemDomain.Length-1)
- [string]$KRBTGTAccountsItemSAMAccountName = $AllADKRBTGTAccountSPNsItem.properties.samaccountname
- [string]$KRBTGTAccountsItemdescription = $AllADKRBTGTAccountSPNsItem.properties.description
- [string]$KRBTGTAccountsItempwdlastset = $AllADKRBTGTAccountSPNsItem.properties.pwdlastset
- [string]$KRBTGTAccountsItemPasswordLastSetDate = [datetime]::FromFileTimeUTC($KRBTGTAccountsItempwdlastset)
- [string]$KRBTGTAccountsItemlastlogon = $AllADKRBTGTAccountSPNsItem.properties.lastlogon
- [string]$KRBTGTAccountsItemLastLogonDate = [datetime]::FromFileTimeUTC($KRBTGTAccountsItemlastlogon)
- $KRBTGTAccountReport = New-Object -TypeName System.Object
- $KRBTGTAccountReport | Add-Member -MemberType NoteProperty -Name Domain -Value $KRBTGTAccountsItemDomain
- $KRBTGTAccountReport | Add-Member -MemberType NoteProperty -Name UserID -Value $KRBTGTAccountsItemSAMAccountName
- $KRBTGTAccountReport | Add-Member -MemberType NoteProperty -Name Description -Value $KRBTGTAccountsItemdescription
- $KRBTGTAccountReport | Add-Member -MemberType NoteProperty -Name PasswordLastSet -Value $KRBTGTAccountsItemPasswordLastSetDate
- $KRBTGTAccountReport | Add-Member -MemberType NoteProperty -Name LastLogon -Value $KRBTGTAccountsItemLastLogonDate
- [array]$AllKRBTGTAccountReport += $KRBTGTAccountReport
- }
- # $AllKRBTGTAccountReport | sort PasswordLastSet
- return $AllKRBTGTAccountReport
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement