无法在Powershell中找到类型[System.Management.Automation.IValidateSetValuesGenerator]错误

问题描述 投票:1回答:1

我遇到了一个问题,即使我检查了很多资源,我也无法解决这个问题。我可能错过了一个非常重要的步骤,因为我不是Windows开发人员,而且我对PowerShell知之甚少。

这是我的PS版本。

Name                           Value                                                                                                      
----                           -----                                                                                                      
PSVersion                      5.1.14409.1018                                                                                             
PSEdition                      Desktop                                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                    
BuildVersion                   10.0.14409.1018                                                                                            
CLRVersion                     4.0.30319.42000                                                                                            
WSManStackVersion              3.0                                                                                                        
PSRemotingProtocolVersion      2.3                                                                                                        
SerializationVersion           1.1.0.1  

我想为我的函数的一个标志添加动态填充的值数组(如bash中的COMPREPLY)。

我根据在线查看的来源编写了以下示例。

class GetProfiles : System.Management.Automation.IValidateSetValuesGenerator
{
    [String[]] GetValidValues()
    {
        $Paths = "D:\Profiles"
        $Profiles = ForEach ($Path in $Paths)
        {
            if (Test-Path $Path)
            {
                (Get-ChildItem $Path).BaseName
            }
        }
        return [string[]] $Profiles
    }
}

function global:Activate()
{
    [CmdletBinding()]
    Param
    (
        [ValidateSet([GetProfiles])]
        [string]$Profile="",
    )

    Write-Host $Profile
}

但我的代码产生了这个错误。

At D:\projects\activate.ps1:40 char:20 + ... SoundNames : System.Management.Automation.IValidateSetValuesGenerator + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Unable to find type [System.Management.Automation.IValidateSetValuesGenerator]. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : TypeNotFound

我有以下的dll,我试着用Add-Type -Path将它添加到脚本的顶部,不起作用:/。

C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

有任何想法吗?你介意把我放在正确的方向吗?

谢谢!

powershell assemblies
1个回答
0
投票

仅适用于ps6 +。这一页 ...

IValidateSetValuesGenerator接口(System.Management.Automation)| Microsoft Docs - https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.ivalidatesetvaluesgenerator?view=pscore-6.0.0

......说得很清楚。 [grin] plus,当你选择Windows Powershell时,它会显示这个......

请求的页面不适用于Windows PowerShell SDK 1.1.0。您已被重定向到此页面可用的最新产品版本。

© www.soinside.com 2019 - 2024. All rights reserved.