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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With