Skip to content

Commit 8d7a161

Browse files
committed
Update Show-UserSID
additional params, output format types and extension features, added blocks, etc.
1 parent 3a451a6 commit 8d7a161

File tree

1 file changed

+105
-31
lines changed

1 file changed

+105
-31
lines changed

ps-win-groups-users/Show-UserSID.ps1

Lines changed: 105 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1+
#Requires -Version 3.0
12
Function Show-UserSID {
23
<#
34
.SYNOPSIS
4-
Gets the Security Identifier (SID) for specified users.
5+
Gets the Security Identifier (SID) of one or more user accounts.
56
67
.DESCRIPTION
7-
Retrieves the Security Identifier (SID) for users by translating their account names.
8+
This function retrieves the Security Identifier (SID) for specified user accounts, providing flexibility in output format, domain filtering, SID verification, and logging options.
89
910
.PARAMETER UserName
10-
Specifies the user names for which to retrieve the SID, accepts an array of user names.
11+
Specifies the user names for which to retrieve the SID. You can provide a single username or an array of usernames.
1112
.PARAMETER OutputFormat
12-
Specifies the format for displaying the output, options: 'Table' (default), 'List', 'JSON'.
13+
Specifies the output format for displaying the SID information, values are "Table" (default), "List", "JSON", "CSV", "HTML", "XML".
14+
.PARAMETER OutputPath
15+
Specifies the path where the file will be created. If provided, output will be saved to a file with the appropriate extension.
1316
.PARAMETER Domain
14-
Domain to filter users, if specified, only users from this domain will be processed.
15-
.PARAMETER LogToFile
16-
File path to log verbose information.
17+
Specifies the domain(s) to filter user accounts. If provided, only user accounts from the specified domain(s) will be processed.
18+
.PARAMETER SIDCheck
19+
Specifies a SID to check if it belongs to the specified user(s). If provided, only users with a matching SID will be processed.
1720
.PARAMETER IncludeFullName
18-
Indicates whether to include the user's full name in the output.
21+
Includes the user's full name in output. If specified, the output will include the full name; otherwise, it will display "n/a".
1922
2023
.EXAMPLE
21-
"user" | Get-UserSID
22-
Get-UserSID -UserName "user1", "user2" -OutputFormat JSON
24+
"user0" | Show-UserSID
25+
Show-UserSID -UserName "user1", "user2" -OutputFormat JSON -Domain "domain" -IncludeFullName
26+
Show-UserSID -UserName "user1", "user2" -OutputFormat CSV -SIDCheck "S-1-5-21-123456789-1234567890-1234567890-1001" -OutputPath "$env:USERPROFILE\Desktop\uSID.csv"
2327
2428
.NOTES
25-
v0.0.5
29+
v0.3.2
2630
#>
2731
[CmdletBinding()]
2832
param (
@@ -32,57 +36,127 @@ Function Show-UserSID {
3236
[string[]]$UserName,
3337

3438
[Parameter(Mandatory = $false, Position = 1, HelpMessage = "Specify the output format")]
35-
[ValidateSet("Table", "List", "JSON")]
39+
[ValidateSet("Table", "List", "JSON", "CSV", "HTML", "XML")]
3640
[Alias("o")]
3741
[string]$OutputFormat = "Table",
3842

39-
[Parameter(Mandatory = $false, Position = 2, HelpMessage = "Specify the domain to filter users")]
43+
[Parameter(Mandatory = $false, HelpMessage = "Specify a path where the file will be created")]
44+
[Alias("l")]
45+
[string]$OutputPath,
46+
47+
[Parameter(Mandatory = $false, Position = 2, HelpMessage = "Specify the domain(s) to filter users")]
4048
[Alias("d")]
41-
[string]$Domain,
49+
[string[]]$Domain,
4250

43-
[Parameter(Mandatory = $false, HelpMessage = "Specify a file path to log verbose information.")]
44-
[Alias("l")]
45-
[string]$LogToFile,
51+
[Parameter(Mandatory = $false, HelpMessage = "Check if SID belongs to the declared user(s)")]
52+
[Alias("s")]
53+
[string]$SIDCheck,
4654

47-
[Parameter(Mandatory = $false, HelpMessage = "User's full name in output")]
55+
[Parameter(Mandatory = $false, HelpMessage = "Include user's full name in output")]
4856
[Alias("if")]
4957
[switch]$IncludeFullName
5058
)
59+
BEGIN {
60+
if ($OutputPath -and -not $OutputFormat) {
61+
Write-Error "If OutputPath is specified, OutputFormat must also be specified."
62+
return
63+
}
64+
if ($OutputFormat -and $OutputFormat -notin ("Table", "List", "JSON", "CSV", "HTML", "XML")) {
65+
Write-Error "Invalid OutputFormat. Please specify a valid format: Table, List, JSON, CSV, HTML, XML."
66+
return
67+
}
68+
}
5169
PROCESS {
5270
foreach ($User in $UserName) {
5371
try {
54-
if ($Domain -and $User -notmatch "@$Domain") {
55-
Write-Warning -Message "Skipping user '$User' as it does not belong to the specified domain '$Domain'"
72+
if ($Domain -and $User -notmatch "@($Domain -join '|')") {
73+
Write-Warning -Message "Skipping user '$User' as it does not belong to the specified domain(s) '$($Domain -join ', ')'"
5674
continue
5775
}
5876
$NTAccount = New-Object System.Security.Principal.NTAccount($User)
5977
$UserSID = $NTAccount.Translate([System.Security.Principal.SecurityIdentifier]).Value
6078
$FullName = if ($IncludeFullName) {
6179
$NTAccount.Translate([System.Security.Principal.NTAccount]).Value
6280
}
81+
if ($SIDCheck -and $UserSID -ne $SIDCheck) {
82+
Write-Warning -Message "Skipping user '$User' as it does not match the specified SID '$SIDCheck'"
83+
continue
84+
}
6385
$Result = [PSCustomObject]@{
6486
UserName = $User
6587
SID = $UserSID
66-
FullName = if ($FullName) { $FullName } else { 'n/a' }
88+
FullName = if ($FullName) { $FullName } else { "n/a" }
89+
}
90+
$OutputFileExtension = @{
91+
"List" = '';
92+
"JSON" = '.json';
93+
"CSV" = '.csv';
94+
"HTML" = '.html';
95+
"XML" = '.xml';
96+
default = '.txt';
97+
}[$OutputFormat]
98+
$OutputFile = if ($OutputPath -match '\.\w+$') {
99+
$OutputPath
100+
}
101+
else {
102+
Join-Path $OutputPath "$($User)_SID$OutputFileExtension"
67103
}
68104
switch ($OutputFormat) {
69-
"List" {
70-
Write-Output $Result.PSObject.Properties | ForEach-Object { "$($_.Name): $($_.Value)" }
105+
"List" {
106+
if ($OutputPath) {
107+
$Result.PSObject.Properties | ForEach-Object { "$($_.Name): $($_.Value)" } | Set-Content -Path $OutputFile -Encoding UTF8
108+
}
109+
else {
110+
$Result.PSObject.Properties | ForEach-Object { "$($_.Name): $($_.Value)" }
111+
}
71112
}
72-
"JSON" {
73-
$Result | ConvertTo-Json
113+
"JSON" {
114+
if ($OutputPath) {
115+
$Result | ConvertTo-Json | Set-Content -Path $OutputFile -Encoding UTF8
116+
}
117+
else {
118+
$Result | ConvertTo-Json
119+
}
74120
}
75-
default {
76-
Write-Output $Result | Format-Table -AutoSize
121+
"CSV" {
122+
if ($OutputPath) {
123+
$Result | Export-Csv -Path $OutputFile -NoTypeInformation -Append
124+
}
125+
else {
126+
$Result | Export-Csv -NoTypeInformation -Append
127+
}
77128
}
78-
}
79-
if ($LogToFile) {
80-
Add-Content -Path $LogToFile -Value "$($Result.UserName): $($Result.SID) $($Result.FullName)"
129+
"HTML" {
130+
if ($OutputPath) {
131+
$Result | ConvertTo-Html | Set-Content -Path $OutputFile -Encoding UTF8
132+
}
133+
else {
134+
$Result | ConvertTo-Html
135+
}
136+
}
137+
"XML" {
138+
if ($OutputPath) {
139+
$Result | Export-Clixml -Path $OutputFile
140+
}
141+
else {
142+
$Result | Export-Clixml
143+
}
144+
}
145+
default { $Result | Format-Table -AutoSize }
81146
}
82147
}
83148
catch {
84-
Write-Error -Message "Failed to get SID for user '$User'. $_"
149+
Write-Error -Message "Failed to get SID for user '$User'! $_"
150+
}
151+
finally {
152+
Write-Verbose -Message "SID information: $Result"
85153
}
86154
}
87155
}
156+
END {
157+
if ($OutputPath) {
158+
Write-Host "SID information exported to $OutputPath" -ForegroundColor DarkCyan
159+
}
160+
Write-Output -InputObject $Result
161+
}
88162
}

0 commit comments

Comments
 (0)