@@ -47,36 +47,43 @@ function Test-IsMemberOfProtectedUsers {
4747 $User
4848 )
4949
50- Import-Module ActiveDirectory
51-
52- # Use the currently logged in user if none is specified
53- # Get the user from Active Directory
54- if (-not ($User )) {
55- # These two are different types. Fixed by referencing $CheckUser.SID later, but should fix here by using one type.
56- $CurrentUser = ([System.Security.Principal.WindowsIdentity ]::GetCurrent().Name).Split(' \' )[-1 ]
57- $CheckUser = Get-ADUser $CurrentUser - Properties primaryGroupID
58- } else {
59- $CheckUser = Get-ADUser $User - Properties primaryGroupID
50+ begin {
51+ Import-Module ActiveDirectory
6052 }
6153
62- # Get the Protected Users group by SID instead of by its name to ensure compatibility with any locale or language.
63- $DomainSID = (Get-ADDomain ).DomainSID.Value
64- $ProtectedUsersSID = " $DomainSID -525"
54+ process {
55+ # Use the currently logged in user if none is specified
56+ # Get the user from Active Directory
57+ if (-not ($User )) {
58+ # These two are different types. Fixed by referencing $CheckUser.SID later, but should fix here by using one type.
59+ $CurrentUser = ([System.Security.Principal.WindowsIdentity ]::GetCurrent().Name).Split(' \' )[-1 ]
60+ $CheckUser = Get-ADUser $CurrentUser - Properties primaryGroupID
61+ } else {
62+ $CheckUser = Get-ADUser $User - Properties primaryGroupID
63+ }
64+
65+ # Get the Protected Users group by SID instead of by its name to ensure compatibility with any locale or language.
66+ $DomainSID = (Get-ADDomain ).DomainSID.Value
67+ $ProtectedUsersSID = " $DomainSID -525"
6568
66- # Get members of the Protected Users group for the current domain. Recuse in case groups are nested in it.
67- $ProtectedUsers = Get-ADGroupMember - Identity $ProtectedUsersSID - Recursive | Select-Object - Unique
69+ # Get members of the Protected Users group for the current domain. Recuse in case groups are nested in it.
70+ $ProtectedUsers = Get-ADGroupMember - Identity $ProtectedUsersSID - Recursive | Select-Object - Unique
6871
69- # Check if the current user is in the 'Protected Users' group
70- if ($ProtectedUsers.SID.Value -contains $CheckUser.SID ) {
71- Write-Verbose " $ ( $CheckUser.Name ) ($ ( $CheckUser.DistinguishedName ) ) is a member of the Protected Users group."
72- $true
73- } else {
74- # Check if the user's PGID (primary group ID) is set to the Protected Users group RID (525).
75- if ( $CheckUser.primaryGroupID -eq ' 525' ) {
72+ # Check if the current user is in the 'Protected Users' group
73+ if ($ProtectedUsers.SID.Value -contains $CheckUser.SID ) {
74+ Write-Verbose " $ ( $CheckUser.Name ) ($ ( $CheckUser.DistinguishedName ) ) is a member of the Protected Users group."
7675 $true
7776 } else {
78- Write-Verbose " $ ( $CheckUser.Name ) ($ ( $CheckUser.DistinguishedName ) ) is not a member of the Protected Users group."
79- $false
77+ # Check if the user's PGID (primary group ID) is set to the Protected Users group RID (525).
78+ if ( $CheckUser.primaryGroupID -eq ' 525' ) {
79+ $true
80+ } else {
81+ Write-Verbose " $ ( $CheckUser.Name ) ($ ( $CheckUser.DistinguishedName ) ) is not a member of the Protected Users group."
82+ $false
83+ }
8084 }
8185 }
86+
87+ end { }
88+
8289}
0 commit comments