Home > Blockchain >  Powershell Get-LocalGroupMember adds computer name to $._Name so it cannot be compared to txt file
Powershell Get-LocalGroupMember adds computer name to $._Name so it cannot be compared to txt file

Time:11-15

Essentially, my script is supposed to check if each user in the administrators group is listed inside of a text file, and if it is then ignore it and move on. If it isn't, it removes the user from the administrator group. However, Get-LocalGroupMember prepends the computer name to the username. This means that the username in the txt file (ex user1), does not match the $._Name variable from the Get-LocalGroupMember command (ex desktop/user1). Here is a copy of the code

$GroupName = "Administrators"
$Exclude = "Administrator","$env:UserName"
$AuthorizedAdmins = Get-Content C:\Users\$env:UserName\admins.txt

Get-LocalGroupMember $GroupName |
    ForEach-Object{
        if ($_.ObjectClass -eq 'User'){
        if ($AuthorizedAdmins -contains $_.Name -or $Exclude -contains $_.Name){
            Continue
        }
        else{
            Remove-LocalGroupMember -Group $GroupName -Member $_.Name -Confirm
        }
    }
}

I have tried several solutions. In the code, I created a new variable that removed the first $env:ComputerName 1 characters of the $._Name string. While this did work to remove the computername, powershell errors out. Here is the error code and changed script:

Get-LocalGroupMember : System error.
At users.ps1:6 char:1
  Get-LocalGroupMember $GroupName |
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : NotSpecified: (:) [Get-LocalGroupMember], ContinueException
      FullyQualifiedErrorId : An unspecified error occurred.,Microsoft.PowerShell.Commands.GetLocalGroupMemberCommand
$GroupName = "Administrators"
$Exclude = "Administrator","$env:UserName"
$AuthorizedAdmins = Get-Content C:\Users\$env:UserName\admins.txt
   
Get-LocalGroupMember $GroupName |
    ForEach-Object{
        $User = $_.Name
        $length = $env:ComputerName.Length 1
        $ShortUser = $User.Remove(0,$length)
        if ($_.ObjectClass -eq 'User'){ #ignore groups and select only users
            if ($AuthorizedAdmins -contains $ShortUser -or $Exclude -contains $ShortUser){
                Continue
            }
            else{
                Remove-LocalGroupMember -Group $GroupName -Member $_.Name -Confirm
            }
        }
    }

The admin.txt file is formatted as follows:

user1
user2
user3

I cannot figure out how to fix this, though it is probably someting simple. Any help would be appreciated.

CodePudding user response:

The real issue with your code is your use of continue in a ForEach-Object loop, see note from the docs. If you want to emulate continue in a pipeline processing function you should use return instead. So your code, with some improvements and simplifications would be:

$GroupName = "Administrators"
$exclude = @(
    "Administrator"
    $env:UserName
    Get-Content C:\Users\$env:UserName\admins.txt
)

Get-LocalGroupMember $GroupName | ForEach-Object{
    # if its not a user, skip this logic
    if ($_.ObjectClass -ne 'User') {
        return
    }
    # here we assume its a user
    if ($_.Name.Split('\')[-1] -in $exclude) {
        return
    }
    
    Remove-LocalGroupMember -Group $GroupName -Member $_.Name -Confirm
}
  • Related