Powershell 中的 HashSet:集合具有固定大小 [英] HashSet in Powershell: Collection was of a fixed size

查看:159
本文介绍了Powershell 中的 HashSet:集合具有固定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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

有没有办法允许Remove操作?

推荐答案

当您通过管道传递集合时,它会被枚举并传递每个单独的元素.如果要将集合作为单个元素传递,则应将集合打包到另一个集合.一元 , 用单个元素创建数组.

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

这篇关于Powershell 中的 HashSet:集合具有固定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆