01 July, 2021

User info Function

I call this function into both Console, and ISE profiles.
It will pull in the contents of the clipboard, and pull in pertinent user info.
For me - The users location is really helpful, but It will also show if a users account is locked, the password, or the account is expired, etc...

If the clipboard contents don't work, it will rerun the function and prompt...



Function Office {

$EAP = $ErrorActionPreference 
$ErrorActionPreference = "SilentlyContinue"
$Who = Get-Clipboard

if ($Who -eq "x") {Write-Host " - Exiting..."; break}
If ($Who -is [array]) {
$Who =  [string]::Join(" ",$Who)
}
If (!$Who -or $Who.Length -gt 30) {
Write-Host "Full name, username, or email address? " -NoNewline -ForegroundColor Yellow -BackgroundColor DarkMagenta
Write-Host "('X' to exit)" -NoNewline -ForegroundColor Cyan -BackgroundColor DarkMagenta
Write-Host ":" -NoNewline -ForegroundColor Yellow -BackgroundColor DarkMagenta; Write-Host " " -NoNewline
$Who = (Read-Host).Trim()
}
If ($Who -eq "x") {break}
If ($Who) {$Who = "$(($Who).Trim())"}

If (!$Who) {Write-host "Nothing to look for / Nothing entered." -ForegroundColor Yellow -BackgroundColor Black; break}
$Quality = 0
If ($Who -NOTmatch " " -and $Who -NOTmatch "@") { $Quality = 1; Write-Host "Looking for this username: $Who" -ForegroundColor Cyan }
If ($Who -match "@") { $Quality = 2; Write-Host "Looking for this email address: $Who" -ForegroundColor Cyan }
If ($Who -match " ") { $Quality = 3; Write-Host "Looking for this name: $Who" -ForegroundColor Cyan }

Function ADU1 {
$ErrorActionPreference = "SilentlyContinue"
Get-ADUser -filter * -Properties City, State, 
telephoneNumber, mail, Title, Manager, Description, 
PasswordLastSet, LastLogonDate, PasswordExpired, PasswordNeverExpires, 
lockoutTime, LockedOut, AccountExpirationDate
$ErrorActionPreference = "Continue"
} 
$Output = $null
If ($Quality -eq 1) { $Output = ADU1 | ? {$_.SamAccountName -match $Who} }
If ($Quality -eq 2) { $Output = ADU1 | ? {$_.mail -match $Who} }
If ($Quality -eq 3) { $Output = ADU1 | ? {$_.Name -match $Who} }

If (!$Output) {Write-host "`'$Who`' is not valid." -ForegroundColor Yellow -BackgroundColor Black
Set-Clipboard $null
office
}

$Pwd_Age = "$( ( (Get-Date)-($Output.PasswordLastSet) ).Days ) day(s), $(((Get-Date)-($Output.PasswordLastSet) ).Hours) hour(s), $(((Get-Date)-($Output.PasswordLastSet) ).Minutes) minute(s) ago"

$lockoutTime = $Output.lockoutTime
If ($lockoutTime -gt 0) {$lockoutTime = [datetime]::FromFileTime($($lockoutTime))}

$Output = $Output | Select Name, `
@{Name='Username';Expression ={($_.SamAccountName).ToUpper()}}, `
@{Name='Desc';Expression ={(($_.Description).Replace('|','-'))}},`
@{Name='Location';expression={"$($Output.City), $($Output.State)"}},` `
@{Name='Phone';Expression={$("{0:# ###-###-####}" -f [int64](($_.telephoneNumber).Trim('+')))}}, `
Mail, Title, `
@{Name='Manager';Expression ={"$((Get-ADUser ((get-aduser ($_.SamAccountName) -Properties Manager).Manager) | Select `
    @{name="Manager";expression={"$($_.Name) ($(($_.SamAccountName).ToUpper())) - $($_.UserPrincipalName)"}}).Manager)"}}, `
@{name="Last Logon";expression={$_.LastLogonDate}},`
@{name="Lockout time";expression={$lockoutTime}},` 
@{name="Locked out";expression={$_.LockedOut}},` 
@{name="Pwd No Exp";expression={$_.PasswordNeverExpires}},`
@{name="Expiration";expression={$_.AccountExpirationDate}},` 
@{name="Pwd Last Set";expression={$_.PasswordLastSet}},` 
@{name="Pwd Age";expression={$Pwd_Age}},` 
@{name="Pwd Expired";expression={$_.PasswordExpired}}

Write-Host " "

($Output | Out-String).Split("`n|`r",[System.StringSplitOptions]::RemoveEmptyEntries)  | % {
If ($_ -like "Name *") {Write-Host "   $_ ($($Output.Username))"}
If ($_ -like "Location *" ) { Write-Host "     $(($_).Replace('  :',':'))" -ForegroundColor Green }
If ($_ -like "Desc *" -and $Output.Desc -ne $null) {Write-Host "   $_"}
If ($_ -like "Phone *" -and $Output.Phone -ne $null) {Write-Host "   $_"}
If ($_ -like "Mail *") {Write-Host "   $_"}
If ($_ -like "Title *") {Write-Host "   $_"}
If ($_ -like "Manager *") {Write-Host "   $_"}
If ($_ -like "Last Logon *") {Write-Host "   $_"}
If ($_ -like "Expiration *" -and $Output.Expiration -ne $null) { Write-Host "   " -No; Write-Host "$_"  -Fore Red -Back Yellow }
If ($_ -like "Locked out *" -and $Output."Lockout time" -gt 0) {Write-Host "   $_"}
If ($_ -like "Last Logon *" -and $Output."Lockout time" -gt 0) {Write-Host "   $_"}
If ($_ -like "Pwd Last Set *" -and $Output."Pwd No Exp" -ne $True) {Write-Host "   $_" -No}
If ($_ -like "Pwd Expired *" -and $Output."Pwd Expired" -eq $True) {Write-Host " <EXPIRED> " -Fore Red -Back Yellow -No}
If ($_ -like "Pwd Age *" -and $Output."Pwd No Exp" -ne $True) {Write-Host " [$($Output.'Pwd Age')]" -Fore Yellow}
}

If ($host.name -eq "ConsoleHost") {''}
Set-Clipboard $null
$ErrorActionPreference = $EAP 

} # END Function Office {

set-alias -name ofc -value Office

No comments:

Post a Comment