简体   繁体   中英

HashSet in Powershell: Collection was of a fixed size

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?

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM