Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashSet in Powershell: Collection was of a fixed size

Tags:

c#

powershell

I have a PowerShell function as follows:

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 by executing 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 a way to allow the Remove operation?

like image 350
vdenotaris Avatar asked Mar 19 '15 15:03

vdenotaris


1 Answers

When you pass collection by pipeline, it is enumerated and each individual element is passed. If you want to pass collection as single element, you should pack collection to another collection. Unary , create array with single 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()
    }
}
like image 183
user4003407 Avatar answered Sep 17 '22 03:09

user4003407