OK, the code should just need a small update to include a single call to Get-ADGroup, then. Try this:
foreach ($csvEntry in $groups) { try { $group = Get-ADGroup -Identity $csvEntry.SamAccountName -ErrorAction Stop $GID = [int]$group.SID.Value.Substring([int]$group.SID.Value.Lastindexof("-")+1)+1000 $group | Set-ADGroup -Replace @{gidNumber=$GID} -Verbose -ErrorAction Stop } catch { # handle error however you like } }