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.