Powershell HashSet: Collection Was Fixed Size

I have a PowerShell function:

Function GetAllIdentityProvidersFromDatabase { param ( [string] $SQLConnectionSting ) $AllIdPIdentifiers = New-Object 'System.Collections.Generic.HashSet[string]' $SQLConnect = new-object system.data.sqlclient.sqlconnection $SQLConnectionSting try { $SQLQuery = $("SELECT [IdPIdentifier] FROM [dbo].[IdPs]") $SQLConnect.Open() $command = New-object system.data.sqlclient.SqlCommand $command.connection = $SQLConnect $command.CommandText = $SQLQuery $Reader = $command.ExecuteReader() while ($Reader.Read()) { $value = $Reader.GetValue($1) $AllIdPIdentifiers.Add($value) | Out-Null } $AllIdPIdentifiers } catch { Write-Host "SQL Select error: " $Error[0].ToString() -ForegroundColor Red } finally { $SQLConnect.Close() } } 

Then in another script:

 $AllIdPIdentifiers = New-Object 'System.Collections.Generic.HashSet[string]' $AllIdPIdentifiers = GetAllIdentityProvidersFromDatabase $SQLConnectionString $AllIdPIdentifiers.Remove("GodspeedYou") Write-Host $AllIdPIdentifiers.Count 

And, having executed it, I have this error:

 Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size." At C:\PowerShell\EduGain\FederationMetadataExtractor.ps1:151 char:1 + $AllIdPIdentifiers.Remove("GodspeedYou") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NotSupportedException 

Is there any way to enable the Remove operation?

+5
source share
1 answer

When you pass a collection along the pipeline, it is enumerated and each individual item is passed. If you want to transfer the collection as a single item, you should collect the collection into another collection. Unary , create an array with one element.

 Function GetAllIdentityProvidersFromDatabase { param ( [string] $SQLConnectionSting ) $AllIdPIdentifiers = New-Object 'System.Collections.Generic.HashSet[string]' $SQLConnect = new-object system.data.sqlclient.sqlconnection $SQLConnectionSting try { $SQLQuery = $("SELECT [IdPIdentifier] FROM [dbo].[IdPs]") $SQLConnect.Open() $command = New-object system.data.sqlclient.SqlCommand $command.connection = $SQLConnect $command.CommandText = $SQLQuery $Reader = $command.ExecuteReader() while ($Reader.Read()) { $value = $Reader.GetValue($1) $AllIdPIdentifiers.Add($value) | Out-Null } ,$AllIdPIdentifiers } catch { Write-Host "SQL Select error: " $Error[0].ToString() -ForegroundColor Red } finally { $SQLConnect.Close() } } 
+4
source

Source: https://habr.com/ru/post/1215695/


All Articles