我有一个PowerShell功能如下:Function GetAllIdentityProvidersFromDatabase {param ([string] $SQLConnectionSting)$AllIdPIdentifiers = New-Object System.Collections.Generic.HashSet[string]$SQLConnect = ...

我有一个PowerShell功能如下:
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()
}
}
然后,在另一个脚本中:
$AllIdPIdentifiers = New-Object 'System.Collections.Generic.HashSet[string]'
$AllIdPIdentifiers = GetAllIdentityProvidersFromDatabase $SQLConnectionString
$AllIdPIdentifiers.Remove("GodspeedYou")
Write-Host $AllIdPIdentifiers.Count
通过执行它,我有这个错误:
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
有没有办法允许删除操作?
解决方法:
当您通过管道传递集合时,它将被枚举并传递每个单独的元素.如果要将集合作为单个元素传递,则应将集合打包到另一个集合.一元,用单个元素创建数组.
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()
}
}
沃梦达教程
本文标题为:c# – Powershell中的HashSet:Collection是固定大小的


基础教程推荐
猜你喜欢
- C# – NetUseAdd来自Windows Server 2008和IIS7上的NetApi32.dll 2023-09-20
- C# Winform实现石头剪刀布游戏 2023-01-11
- Unity shader实现多光源漫反射以及阴影 2023-03-04
- 京东联盟C#接口测试示例分享 2022-12-02
- c#读取XML多级子节点 2022-11-05
- c#中利用Tu Share获取股票交易信息 2023-03-03
- C#集合查询Linq在项目中使用详解 2023-06-09
- 使用c#从分隔文本文件中插入SQL Server表中的批量数据 2023-11-24
- C#中类与接口的区别讲解 2023-06-04
- C#通过GET/POST方式发送Http请求 2023-04-28