Skip to content

Commit 3a451a6

Browse files
committed
Update Show-UserSID
domain and log to file features, other features and enhancements
1 parent 0ca054b commit 3a451a6

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
Function Show-UserSID {
22
<#
33
.SYNOPSIS
4-
Gets the Security Identifier (SID) for a specified user.
4+
Gets the Security Identifier (SID) for specified users.
55
66
.DESCRIPTION
7-
Retrieves the Security Identifier (SID) for a user by translating the account name.
7+
Retrieves the Security Identifier (SID) for users by translating their account names.
88
99
.PARAMETER UserName
10-
Specifies the user name for which to retrieve the SID.
10+
Specifies the user names for which to retrieve the SID, accepts an array of user names.
11+
.PARAMETER OutputFormat
12+
Specifies the format for displaying the output, options: 'Table' (default), 'List', 'JSON'.
13+
.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+
.PARAMETER IncludeFullName
18+
Indicates whether to include the user's full name in the output.
1119
1220
.EXAMPLE
13-
"your_user" | Show-UserSID
14-
Show-UserSID -UserName "user1", "user2" -OutputFormat List
21+
"user" | Get-UserSID
22+
Get-UserSID -UserName "user1", "user2" -OutputFormat JSON
1523
1624
.NOTES
17-
v0.0.4
25+
v0.0.5
1826
#>
1927
[CmdletBinding()]
2028
param (
@@ -23,18 +31,39 @@ Function Show-UserSID {
2331
[Alias("u")]
2432
[string[]]$UserName,
2533

26-
[Parameter(Position = 1, HelpMessage = "Specify the output format. Options: 'Table' (default), 'List', 'JSON'")]
34+
[Parameter(Mandatory = $false, Position = 1, HelpMessage = "Specify the output format")]
2735
[ValidateSet("Table", "List", "JSON")]
28-
[string]$OutputFormat = "Table"
36+
[Alias("o")]
37+
[string]$OutputFormat = "Table",
38+
39+
[Parameter(Mandatory = $false, Position = 2, HelpMessage = "Specify the domain to filter users")]
40+
[Alias("d")]
41+
[string]$Domain,
42+
43+
[Parameter(Mandatory = $false, HelpMessage = "Specify a file path to log verbose information.")]
44+
[Alias("l")]
45+
[string]$LogToFile,
46+
47+
[Parameter(Mandatory = $false, HelpMessage = "User's full name in output")]
48+
[Alias("if")]
49+
[switch]$IncludeFullName
2950
)
3051
PROCESS {
3152
foreach ($User in $UserName) {
3253
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'"
56+
continue
57+
}
3358
$NTAccount = New-Object System.Security.Principal.NTAccount($User)
3459
$UserSID = $NTAccount.Translate([System.Security.Principal.SecurityIdentifier]).Value
60+
$FullName = if ($IncludeFullName) {
61+
$NTAccount.Translate([System.Security.Principal.NTAccount]).Value
62+
}
3563
$Result = [PSCustomObject]@{
3664
UserName = $User
3765
SID = $UserSID
66+
FullName = if ($FullName) { $FullName } else { 'n/a' }
3867
}
3968
switch ($OutputFormat) {
4069
"List" {
@@ -47,6 +76,9 @@ Function Show-UserSID {
4776
Write-Output $Result | Format-Table -AutoSize
4877
}
4978
}
79+
if ($LogToFile) {
80+
Add-Content -Path $LogToFile -Value "$($Result.UserName): $($Result.SID) $($Result.FullName)"
81+
}
5082
}
5183
catch {
5284
Write-Error -Message "Failed to get SID for user '$User'. $_"

0 commit comments

Comments
 (0)