Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 对从CSV文件加载的内容使用ValidateSet_Validation_Powershell_Csv_Parameters - Fatal编程技术网

Validation 对从CSV文件加载的内容使用ValidateSet

Validation 对从CSV文件加载的内容使用ValidateSet,validation,powershell,csv,parameters,Validation,Powershell,Csv,Parameters,我非常喜欢ValidateSet的工作方式。当您在PowerShell ISE中键入Cmdlet时,它会将选项作为列表提出 我想知道是否可以从CSV文件中检索值(Import CSV),并在Param块中使用它们,以便在构造Cmdlet参数时,它们在PowerShell ISE的下拉框中可用?与$Type现在的工作方式相同,但随后使用导入文件中的值 Function New-Name { Param ( [parameter(Position=0, Mandatory=$true)]

我非常喜欢
ValidateSet
的工作方式。当您在PowerShell ISE中键入Cmdlet时,它会将选项作为列表提出

我想知道是否可以从CSV文件中检索值(
Import CSV
),并在
Param
块中使用它们,以便在构造Cmdlet参数时,它们在PowerShell ISE的下拉框中可用?与
$Type
现在的工作方式相同,但随后使用导入文件中的值

Function New-Name {
Param (
    [parameter(Position=0, Mandatory=$true)]
    [ValidateSet('Mailbox','Distribution','Folder','Role')]
    [String]$Type,
    [parameter(Position=1,Mandatory=$true)]
    [String]$Name
)
    Process { 'Foo' }
}

您可以从以下内容开始:

function New-Name {
    param (
        [parameter(Position=0, Mandatory=$true)]
        [String]$Name
    )

    dynamicparam {
        $attributes = new-object System.Management.Automation.ParameterAttribute
        $attributes.ParameterSetName = "__AllParameterSets"
        $attributes.Mandatory = $true
        $attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
        $attributeCollection.Add($attributes)
        $values = @('MailBox', 'Tralala', 'Trilili') # your Import-Csv here
        $ValidateSet = new-object System.Management.Automation.ValidateSetAttribute($values)
        $attributeCollection.Add($ValidateSet)

        $dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("Type", [string], $attributeCollection)
        $paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
        $paramDictionary.Add("Type", $dynParam1)
        return $paramDictionary 
    }

     process { 'Foo' }
}
学分到期时,这主要来自脚本编写人员的以下内容。 代码并不漂亮,但它可以满足您的需要


我更喜欢TabExpansion++模块,虽然这在技术上无法验证,但它有一些很好的功能

下面是一个msbuild重载命令的示例,用于为项目添加一些intellisense

Register-ArgumentCompleter -CommandName "msbuild" -ParameterName "target" -ScriptBlock {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $projectName = $fakeBoundParameter['project']
    $projectFile = Join-Path (Get-Location) $projectName
    $projectXml = [xml](Get-Content $projectFile)

    $targets = $projectXml.Project.Target | Where-Object { $_.Name.ToString().StartsWith($wordToComplete) }
    foreach($target in $projectXml.Project.Target)
    {
        New-CompletionResult -CompletionText "$($target.Name)"
    }
}

正是我想要的。非常感谢你,大卫。