Public/Get-ALHAccountInfo.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
<#PSScriptInfo
.VERSION 1.0.0 .GUID b94366ad-e928-4b4d-9140-ad332576416e .AUTHOR Dieter Koch .COMPANYNAME .COPYRIGHT (c) 2021-2023 Dieter Koch .TAGS .LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE .PROJECTURI https://github.com/admins-little-helper/ALH .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES 1.0.0 Initial release #> <# .DESCRIPTION Contains a function to get an account's SID by account name and domain name, or get an account's domain and name by it's SID. #> function Get-ALHAccountInfo { <# .SYNOPSIS Get an account's SID by account name and domain name, or get an account's domain and name by it's SID. .DESCRIPTION The 'Get-ALHAccountInfo' function gets an account's SID by account name and domain name, or get an account's domain and name by it's SID. .PARAMETER Identity An accounts SID or username in the format '<domain>\<username>' or '<user@domain.tld>'. To get the SID for a computer account, remember to add the '$' trailing character to the account name. .EXAMPLE Get-ALHAccountInfo -Identity "Domain\Computer1$" AccountName DomainName SIDValue ----------- ---------- -------- Computer1 DOMAIN S-1-5-21-3332716652-4045636879-1442444979-2117 Get SID for computer account 'Computer1' in domain 'Domain'. .EXAMPLE Get-ALHAccountInfo -Identity "S-1-5-32-544" AccountName DomainName SIDValue ----------- ---------- -------- administrators BUILT-IN S-1-5-32-544 Get account information for a SID value. .EXAMPLE Get-Content -Path "C:\Temp\Accountnames.txt" | Get-ALHAccountInfo Get SID for a multiple accounts retrieved from a text file via pipeline input. .INPUTS System.String .OUTPUTS PSCustomObject .NOTES Author: Dieter Koch Email: diko@admins-little-helper.de .LINK https://github.com/admins-little-helper/ALH/blob/main/Help/Get-ALHAccountInfo.txt #> [CmdletBinding()] [OutputType([PSCustomObject])] param( [parameter(Mandatory, ValueFromPipeline, HelpMessage = 'Enter account name or SID')] [string[]]$Identity ) begin { Write-Verbose -Message "Defining SID regex pattern." $SIDPattern = '^S-\d-(\d+-){1,14}\d+$' } process { foreach ($SingleIdentity in $Identity) { $IdentityInfo = [PSCustomObject]@{ AccountName = $null DomainName = $null SIDValue = $null } Write-Verbose -Message "Testing SID value for validity." if ($SingleIdentity -match $SIDPattern) { Write-Verbose -Message "SID is a valid SID: '$SIDElement'" $IdentityInfo.SIDValue = $SingleIdentity try { $IdentitySID = [System.Security.Principal.SecurityIdentifier]::new($IdentityInfo.SIDValue) $IdentityObject = $IdentitySID.Translate([System.Security.Principal.NTAccount]) $SingleIdentity = $IdentityObject.Value.ToString() } catch { $_ } } else { Write-Verbose -Message "SID is not a valid SID: '$SIDElement'" } if ($SingleIdentity -match "\\") { Write-Verbose -Message "Username contains '\' - parsing username and domain name from this" $IdentityInfo.DomainName = ($SingleIdentity -split "\\")[0] $IdentityInfo.AccountName = ($SingleIdentity -split "\\")[1] } elseif ($SingleIdentity -match "@") { Write-Verbose -Message "Username contains '@' - assuming UPN and getting username and domain values from it" $IdentityInfo.AccountName = ($SingleIdentity -split "@")[0] $IdentityInfo.DomainName = ($SingleIdentity -split "@")[1] } else { $IdentityInfo.AccountName = $SingleIdentity $IdentityInfo.DomainName = $env:COMPUTERNAME } $IdentityInfo.AccountName = ($IdentityInfo.AccountName).ToLower() $IdentityInfo.DomainName = ($IdentityInfo.DomainName).ToUpper() if ([string]::IsNullOrEmpty($IdentityInfo.SIDValue)) { Write-Verbose -Message "Trying to get SID by account domain and name." try { $IdentityObject = [System.Security.Principal.NTAccount]::new($IdentityInfo.DomainName, $IdentityInfo.AccountName) $IdentitySID = $IdentityObject.Translate([System.Security.Principal.SecurityIdentifier]) $IdentityInfo.SIDValue = $IdentitySID.Value } catch { $_ } } $IdentityInfo } } } #region EndOfScript <# ################################################################################ ################################################################################ # # ______ _ __ _____ _ _ # | ____| | | / _| / ____| (_) | | # | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_ # | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __| # | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_ # |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__| # | | # |_| ################################################################################ ################################################################################ # created with help of http://patorjk.com/software/taag/ #> #endregion |