【发布时间】:2019-08-16 07:50:09
【问题描述】:
我遇到了一个问题,即使我检查了很多资源,我也无法解决。由于我不是 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