Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
PowerShell cmdlet参数值选项卡完成_Powershell_Powershell 3.0 - Fatal编程技术网

PowerShell cmdlet参数值选项卡完成

PowerShell cmdlet参数值选项卡完成,powershell,powershell-3.0,Powershell,Powershell 3.0,如何在PowerShell 3.0中为PowerShell函数或cmdlet(如Get Service和Get Process)实现参数选项卡完成 我意识到它适用于已知列表,但我想根据需要生成列表 Adam Driscoll支持cmdlet,但不幸的是,他没有详细说明 Trevor Sullivan了解函数,但据我所知,他的代码仅在定义函数时生成列表。检查github上的选项卡ExpansionPlusPlus模块,该模块由一位前PowerShell团队魔术师编写 经典地说,我使用正则表达式 比

如何在PowerShell 3.0中为PowerShell函数或cmdlet(如Get Service和Get Process)实现参数选项卡完成

我意识到它适用于已知列表,但我想根据需要生成列表

Adam Driscoll支持cmdlet,但不幸的是,他没有详细说明


Trevor Sullivan了解函数,但据我所知,他的代码仅在定义函数时生成列表。

检查github上的选项卡ExpansionPlusPlus模块,该模块由一位前PowerShell团队魔术师编写


经典地说,我使用正则表达式

比如说,

function TabExpansion {

    param($line, $lastWord) 

    if ( $line -match '(-(\w+))\s+([^-]*$)' )
    {
    ### Resolve Command name & parameter name
        $_param = $matches[2] + '*'
        $_opt = $Matches[3].Split(" ,")[-1] + '*'
        $_base = $Matches[3].Substring(0,$Matches[3].Length-$Matches[3].Split(" ,")[-1].length)

        $_cmdlet = [regex]::Split($line, '[|;=]')[-1]

        if ($_cmdlet -match '\{([^\{\}]*)$')
        {
            $_cmdlet = $matches[1]
        }

        if ($_cmdlet -match '\(([^()]*)$')
        {
            $_cmdlet = $matches[1]
        }

        $_cmdlet = $_cmdlet.Trim().Split()[0]

        $_cmdlet = @(Get-Command -type 'Cmdlet,Alias,Function,Filter,ExternalScript' $_cmdlet)[0]

        while ($_cmdlet.CommandType -eq 'alias')
        {
            $_cmdlet = @(Get-Command -type 'Cmdlet,Alias,Function,Filter,ExternalScript' $_cmdlet.Definition)[0]
        }

    ### Currently target is Get-Alias & "-Name" parameter

        if ( "Get-Alias" -eq $_cmdlet.Name -and "Name" -like $_param )
        {
           Get-Alias -Name $_opt | % { $_.Name } | sort | % { $_base + ($_ -replace '\s','` ') }
           break;
        }
    }
}
参考文献


Posh git将GitTabExpansion.ps1中的TabExpansion重命名为TabExpansion备份。
当完成与git命令不匹配时,posh git重新定义的TabExpansion调用原始TabExpansion(TabExpansionBackup)。
因此,您所要做的就是重新定义TabExpansionBackup

(cat.\GitTabExpansion.ps1|选择-最后18个)
=================================================GitTabExpansion.ps1==============================

if (Test-Path Function:\TabExpansion) {
    Rename-Item Function:\TabExpansion TabExpansionBackup
}

function TabExpansion($line, $lastWord) {
    $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()

    switch -regex ($lastBlock) {
        # Execute git tab completion for all git-related commands
        "^$(Get-AliasPattern git) (.*)" { GitTabExpansion $lastBlock }
        "^$(Get-AliasPattern tgit) (.*)" { GitTabExpansion $lastBlock }

        # Fall back on existing tab expansion
        default { if (Test-Path Function:\TabExpansionBackup) { TabExpansionBackup $line $lastWord } }
    }
}
=================================================================================================

重新定义TabExpansionBackup(原始TabExpansion)


我对此困惑了一会儿,因为我也想做同样的事情。我做了一些我非常满意的事情

您可以从DynamicRAM添加ValidateSet属性。下面是一个示例,说明如何从xml文件动态生成ValidateSet。请参见以下代码中的“ValidateStatAttribute”:

function Foo() {
    [CmdletBinding()]
    Param ()
    DynamicParam {
        #
        # The "modules" param
        #
        $modulesAttributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]

        # [parameter(mandatory=...,
        #     ...
        # )]
        $modulesParameterAttribute = new-object System.Management.Automation.ParameterAttribute
        $modulesParameterAttribute.Mandatory = $true
        $modulesParameterAttribute.HelpMessage = "Enter one or more module names, separated by commas"
        $modulesAttributeCollection.Add($modulesParameterAttribute)    

        # [ValidateSet[(...)]
        $moduleNames = @()
        foreach($moduleXmlInfo in Select-Xml -Path "C:\Path\to\my\xmlFile.xml" -XPath "//enlistment[@name=""wp""]/module") {
            $moduleNames += $moduleXmlInfo.Node.Attributes["name"].Value
        }
        $modulesValidateSetAttribute = New-Object -type System.Management.Automation.ValidateSetAttribute($moduleNames)
        $modulesAttributeCollection.Add($modulesValidateSetAttribute)

        # Remaining boilerplate
        $modulesRuntimeDefinedParam = new-object -Type System.Management.Automation.RuntimeDefinedParameter("modules", [String[]], $modulesAttributeCollection)

        $paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
        $paramDictionary.Add("modules", $modulesRuntimeDefinedParam)
        return $paramDictionary
    }
    process {
        # Do stuff
    }
}
有了这个,我就可以打字了

Foo -modules M<press tab>
Foo-模块M

如果模块在XML文件中,它将完成“MarcusModule”。此外,我可以编辑XML文件,选项卡完成行为将立即改变;您不必重新导入函数。

您是否阅读了此处:不,我没有找到。非常信息化您也可以看看它是powershell v.2中的“动态智能”,但我在3.0中也使用了它,非常精细。我看到posh git已经在我的环境中定义了这个函数。是否有方法扩展/细分任何现有定义?
Foo -modules M<press tab>