When I wrote and can use the following PowerShell function under PowerShell v2 just fine. However, when I try to run it under PowerShell v1 it fails.
function Get-UserChoice
{
param(
[string] $Caption,
[string] $Message = '',
$Choices,
[int] $DefaultChoice = -1
);
# Collection to hold ChoiceDescription objects
$ChoiceCollection = New-Object System.Collections.ObjectModel.Collection``1[System.Management.Automation.Host.ChoiceDescription];
# Create ChoiceDescription object for each choice
foreach ($c in $Choices)
{
# If help text doesn't exist, set it to a blank string
if (!($c.Help)) { $c.Help = '' }
$ChoiceCollection.Add((New-Object System.Management.Automation.Host.ChoiceDescription($c.Label, $c.Help)));
}
return $Host.UI.PromptForChoice($Caption, $Message, $ChoiceCollection, $DefaultChoice);
}
PowerShell v1 error
New-Object : Cannot find type [System.Collections.ObjectModel.Collection`1[System.Management.Automation.Host.ChoiceDescription]]: make sure the assembly containing this type is loaded.
At C:\PS> ChoiceTest.ps1:11 char:35
+ $ChoiceCollection = New-Object <<<< System.Collections.ObjectModel.Collection``1[System.Management.Automation.Host.ChoiceDescription]; You cannot call a method on a null-valued expression. At C:\MailboxMoves\ChoiceTest.ps1:19 char:30
+ $ChoiceCollection.Add( <<<< (New-Object System.Management.Automation.Host.ChoiceDescription($c.Label, $c.Help))); You cannot call a method on a null-valued expression.
I think the error being given is a red herring as I can run the following commands under PowerShell v1, and they both indicate that the Classes are available for use:
# Completes successfully and the Collection can be used
New-Object System.Collections.ObjectModel.Collection``1[System.String]
# Can't find a parameterless constructor
New-Object System.Management.Automation.Host.ChoiceDescription
What am I missing?