Skip to content

Commit e7e9a47

Browse files
committed
Update Show-UserSID
pipeline, custom object, process block, etc.
1 parent 0d4f350 commit e7e9a47

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,37 @@ Function Show-UserSID {
44
Gets the Security Identifier (SID) for a specified user.
55
66
.DESCRIPTION
7-
This function retrieves the Security Identifier (SID) for a given user by translating the user's account name.
8-
It utilizes the New-Object cmdlet to create an NTAccount object and then translates it to a SecurityIdentifier object to obtain the SID.
7+
Retrieves the Security Identifier (SID) for a user by translating the account name.
98
109
.PARAMETER UserName
11-
Specifies the name of the user for whom to retrieve the SID.
10+
Specifies the user name for which to retrieve the SID.
1211
1312
.EXAMPLE
1413
Show-UserSID -UserName "your_user"
1514
1615
.NOTES
17-
v0.0.1
16+
v0.0.2
1817
#>
1918
[CmdletBinding()]
2019
param (
21-
[Parameter(Mandatory, Position = 0, HelpMessage = "Specify the user name")]
20+
[Parameter(Mandatory, Position = 0, ValueFromPipeline = $true, HelpMessage = "Specify the user name")]
2221
[ValidateNotNullOrEmpty()]
22+
[Alias("u")]
2323
[string]$UserName
2424
)
25-
try {
26-
$UserSID = (New-Object Security.Principal.NTAccount($UserName)).Translate([Security.Principal.SecurityIdentifier]).Value
27-
return $UserSID
28-
}
29-
catch {
30-
Write-Error -Message "Error getting SID for user '$UserName'. $_"
31-
return $null
25+
process {
26+
try {
27+
$NTAccount = New-Object System.Security.Principal.NTAccount($UserName)
28+
$UserSID = $NTAccount.Translate([System.Security.Principal.SecurityIdentifier]).Value
29+
$Result = [PSCustomObject]@{
30+
UserName = $UserName
31+
SID = $UserSID
32+
}
33+
Write-Output -InputObject $Result
34+
}
35+
catch {
36+
Write-Error -Message "Failed to get SID for user '$UserName'. $_"
37+
return $null
38+
}
3239
}
3340
}

0 commit comments

Comments
 (0)