Skip objectnotfound and execute the rest

I have a simple scripts that call a .csv file. The script looks for each line and when it founds in will insert in Active Directory for me (adds user to a group), but when the user # doesn’t exist, it doesn’t add but it won’t continue doing the rest. I get “objectnotfound”.

How do tell it to skip it if it can’t find and add, but continue doing the rest of them? I have a script now, but it stops when it can’t find (errors), but it won’t do the rest that exist.

It helps to show your code.

If it’s a halting error and you really don’t care about the error you can typically add -ErrorAction SilentlyContinue to the command that is halting to have it continue execution or have a try/catch block around the erroring command

Neemobeer,
Here’s the code…thanks

$Snumber=Import-csv c:\temp\Snumber.csv
Foreach($EmployeeID in $Snumber)
{
Add-ADGroupMember -Identity “AD group” -Members $Snumber.EmployeeID
}

.csv
row 1 - s# <<< can’t find, doesn’t exist
row 2 - s# <<< exist

I had used $ErrorActionPreference = “SilentlyContinue”, but I doesn’t work.
Thanks.

If your CSV doesn’t have a header you’ll need to provide the header in your Import-CSV command

Actually the first row in my csv is EmployeeID.

Well I assure you I am no mind reader, and speculate the rest of the forum users aren’t either. Your sample shows no header. How can we help with inaccurate information? Also, please edit your code per these instructions.

No header need actually. I poked around and found this $ErrorActionPreference = ‘Continue’ and got it to do what I needed it to do. Thanks again for trying to help.

$ErrorActionPreference = ‘Continue’ <<<<< Added
$Snumber=Import-csv c:\temp\Snumber.csv
Foreach($EmployeeID in $Snumber)
{
Add-ADGroupMember -Identity “LA_Ops-Safety-Div” -Members $Snumber.EmployeeID
}

You acctually don’t need a loop!! :point_up_2:t3: Actually a loop bothers more than it helps because you do the same action again and again for each line in your CSV.

You’re using $Snumber.EmployeeID inside your loop while your loop variable is actually $EmployeeID

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

1 Like

How can re-write to not do a loop :slight_smile:
Would it be Foreach ($User in $Snumber) ?

You do not need a loop. :man_shrugging:t3:

$Snumber = Import-Csv -Path 'c:\temp\Snumber.csv'
Add-ADGroupMember -Identity 'LA_Ops-Safety-Div' -Members $Snumber.EmployeeID

If I remove the loop “Foreach…” it doesn’t work.

Also pro spoke to soon.
This is what I’m getting…

Add-ADGroupMember : Cannot find an object with identity: ‘s110011’ under: ‘DC=ct,DC=dot,DC=ca,DC=gov’.
At line:5 char:1

  • Add-ADGroupMember -Identity “LA_Ops-Safety-Div” -Members $Snumber.Emp …
  •   + CategoryInfo          : ObjectNotFound: (s110011:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
      + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
    
    

Add-ADGroupMember : Cannot find an object with identity: ‘s110011’ under: ‘DC=ct,DC=dot,DC=ca,DC=gov’.
At line:5 char:1

  • Add-ADGroupMember -Identity “LA_Ops-Safety-Div” -Members $Snumber.Emp …
  •   + CategoryInfo          : ObjectNotFound: (s110011:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
      + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

Thought it worked, turns out my random data I chose was actual data found and it ran. Made up a actual false data and it actually doesn’t work.

This is what I am seeing…

Add-ADGroupMember : Cannot find an object with identity: ‘s110011’ under: ‘DC=ct,DC=dot,DC=ca,DC=gov’.
At line:5 char:1

  • Add-ADGroupMember -Identity “LA_Ops-Safety-Div” -Members $Snumber.Emp …
  •   + CategoryInfo          : ObjectNotFound: (s110011:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
      + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
    
    

Add-ADGroupMember : Cannot find an object with identity: ‘s110011’ under: ‘DC=ct,DC=dot,DC=ca,DC=gov’.
At line:5 char:1

  • Add-ADGroupMember -Identity “LA_Ops-Safety-Div” -Members $Snumber.Emp …
  •   + CategoryInfo          : ObjectNotFound: (s110011:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
      + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

Please format your code and error messages always as code …

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

You may re-read the help for the cmdlet you’re about to use :+1:t3:

To provide the members you want to add to the group you have to use one of the following ADPrincipals

  • Distinguished name
  • GUID (objectGUID)
  • Security identifier (objectSid)
  • SAM account name (sAMAccountName)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.