'Deleting admin local group member script doesnt work

I created the below script to delete the Specified Members from Administrators Group but it crashes when it can't find the member. Specificaly it shows my code and in the end adds : An unspecifed error occured At line:21 char:5 "An unspecifed error occured" | Write-Error ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException"

Clear-Host
$ErrorActionPreference = 'Stop'
$VerbosePreference = 'Continue'

#User to search for
$MEMBERS = @("Test1","Test5","Test2","Test76","Test3")
foreach ($MEMBER in $MEMBERS) {

#Declare LocalUser Object
$ObjLocalGroupMember = $null

try {
    Write-Verbose "Searching for $($MEMBER) in LocalGroup DataBase"
    $ObjLocalGroupMember = Get-LocalGroupMember -Group "Administrators" -Member $MEMBER
    Write-Verbose "User $($MEMBER) was found"
}
catch [Microsoft.PowerShell.Commands.MemberNotFoundException] {
    "User $($MEMBER) was not found" | Write-Error
}
catch  {
    "An unspecifed error occured" | Write-Error
    Exit
}

if ($ObjLocalGroupMember) {
    Write-Verbose "Deleting User $($MEMBER)" 
    
$password = ConvertTo-SecureString -String "password" -AsPlainText -Force
$op = Get-LocalGroupMember -Group "Administrators"| Where-Object {$_.Name -eq $MEMBER}
if ($op)
 {
  Remove-LocalGroupMember ($op) | Out-Null
  }
 }
}

I am still very new in this so the solution could be very simple but your help will be very much apreciated.



Solution 1:[1]

The main issue is your use of Write-Error in combination with $ErrorActionPreference = 'Stop', since the preference is Stop, the non-terminating error generated by Write-Error will be treated as a terminating error, hence the script stops at first occurrence of an error.

The other main issue is your use of exit instead of continue in your catch block, but also to explain why it's reaching that catch statement, you're trying to catch the error type Microsoft.PowerShell.Commands.MemberNotFoundException however, when Get-LocalGroupMember can't find a member, the exception type is Microsoft.PowerShell.Commands.PrincipalNotFoundException:

try {
    Get-LocalGroupMember -Group Administrators -Member doesnotexist
}
catch {
    $_.Exception.GetType().FullName
}

# Results in: `Microsoft.PowerShell.Commands.PrincipalNotFoundException`

As for your code, you could simplify it a bit, there are some not needed redundancies:

Clear-Host
$ErrorActionPreference = 'Stop'
$VerbosePreference = 'Continue'

#User to search for
$members = "Test1", "Test5", "Test2", "Test76", "Test3"
foreach($member in $members) {
    try {
        Write-Verbose "Searching for $member in LocalGroup DataBase"
        $ObjLocalGroupMember = Get-LocalGroupMember -Group Administrators -Member $member
        Write-Verbose "User $member was found"
        Write-Verbose "Deleting User $member" 
        Remove-LocalGroupMember $ObjLocalGroupMember
    }
    catch [Microsoft.PowerShell.Commands.PrincipalNotFoundException] {
        Write-Warning "User $member was not found"
        continue # Go to next user
    }
    catch  {
        # Adding `$_` so at least you know what happened
        Write-Warning "An unspecifed error occured: $_"
        continue
    }

    # Unclear what is this doing here, never being used and
    # should also be outside the loop in any case:
    # $password = ConvertTo-SecureString -String "password" -AsPlainText -Force
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1