Unable to pipe Get-DbaUser into Remove-DbaUser when multiple users are selected on the same database #4971
Closed
Description
Before submitting a bug report:
- Ensure you are able to reproduce it on the latest released version (we release often)
- Verified this bug is not already reported in an issue
- Verified errors are not related to permissions
- Can reproduce in a clean PowerShell session (clean =
powershell -NoProfile
)
Steps to Reproduce
CREATE USER USER01 WITHOUT LOGIN;
CREATE USER USER02 WITHOUT LOGIN;
#Returns 2 users
get-dbadbuser -SqlInstance SQLSRV01 -Database DB01| Where-Object{ $_.Name -match "^USER(.*)" } | select Name
# Removes 1 user then errors
get-dbadbuser -SqlInstance SQLSRV01 -Database DB01| Where-Object{ $_.Name -match "^USER(.*)" } | Remove-DbaUser
#Workaround
get-dbadbuser -SqlInstance SQLSRV01 -Database DB01| Where-Object{ $_.Name -match "^USER(.*)" } | %{Remove-DbaUser -SqlInstance $_.SqlInstance -Database $_.Database -User $_.Name }
Expected Behavior
Both users should be removed
Actual Behavior
Single user is removed.
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
At line:36361 char:17
+ $users | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.SqlSe...dListEnumerator:SmoSortedListEnumerator) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
Environmental data
PowerShell:
Name Value
---- -----
PSVersion 5.1.14393.2636
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.2636
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
SQL Server:
Microsoft SQL Server 2014 (SP2-CU13) (KB4456287) - 12.0.5590.1 (X64) Aug 1 2018 01:23:36 Copyright (c) Microsoft Corporation Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 (Build 9600: ) (Hypervisor)