1
1
Function Show-UserSID {
2
2
<#
3
3
. SYNOPSIS
4
- Gets the Security Identifier (SID) for a specified user .
4
+ Gets the Security Identifier (SID) for specified users .
5
5
6
6
. 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 .
8
8
9
9
. 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.
11
19
12
20
. 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
15
23
16
24
. NOTES
17
- v0.0.4
25
+ v0.0.5
18
26
#>
19
27
[CmdletBinding ()]
20
28
param (
@@ -23,18 +31,39 @@ Function Show-UserSID {
23
31
[Alias (" u" )]
24
32
[string []]$UserName ,
25
33
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" )]
27
35
[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
29
50
)
30
51
PROCESS {
31
52
foreach ($User in $UserName ) {
32
53
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
+ }
33
58
$NTAccount = New-Object System.Security.Principal.NTAccount($User )
34
59
$UserSID = $NTAccount.Translate ([System.Security.Principal.SecurityIdentifier ]).Value
60
+ $FullName = if ($IncludeFullName ) {
61
+ $NTAccount.Translate ([System.Security.Principal.NTAccount ]).Value
62
+ }
35
63
$Result = [PSCustomObject ]@ {
36
64
UserName = $User
37
65
SID = $UserSID
66
+ FullName = if ($FullName ) { $FullName } else { ' n/a' }
38
67
}
39
68
switch ($OutputFormat ) {
40
69
" List" {
@@ -47,6 +76,9 @@ Function Show-UserSID {
47
76
Write-Output $Result | Format-Table - AutoSize
48
77
}
49
78
}
79
+ if ($LogToFile ) {
80
+ Add-Content - Path $LogToFile - Value " $ ( $Result.UserName ) : $ ( $Result.SID ) $ ( $Result.FullName ) "
81
+ }
50
82
}
51
83
catch {
52
84
Write-Error - Message " Failed to get SID for user '$User '. $_ "
0 commit comments