2016-06-10 20 views
0

我在PowerShell 3中使用TabExpansion2,當我選項卡來完成一個參數時,它會調出我想要的字符串,但是用我不想要的語法包裝。格式化選項卡參數完成powershell

例如,當我打標籤-binName後:

Use-Bin -binName @{Name=5.0} 

我需要的是:

Use-Bin -binName 5.0 

我使用這個腳本:https://www.powershellgallery.com/packages/PowerShellCookbook/1.3.6/Content/TabExpansion.ps1

這些調整選項:

$options["CustomArgumentCompleters"] = @{ 
      "binName" = {Get-ChildItem -Path $global:TH_BinDir | Select-Object Name} 
      "dbName" = {Get-ChildItem -Path $global:TH_DBDir\RT5.7\ | Select-Object Name} 
      "patchSubDir" ={Get-ChildItem -Path $global:TH_BinDir\Patches\ | Select-Object Name} 
      "hmiSubDir" = {Get-ChildItem -Path $global:TH_HMIDir | Select-Object Name} 
      "moduleScript" = {Get-ChildItem -Path $global:TH_ModPaths | Select-Object Name} 
      "items" = {"bins", "databases", "modules"}   
     } 

謝謝!

+0

在[這個問題]底部的答案(http://stackoverflow.com/questions/30633098/powershell-param-validateset-values-with-spaces-and-tab-completion)有幫助嗎? – user4317867

回答

0

我不熟悉tabexpansion,但你的問題是你正在返回name屬性的對象。你只想返回字符串。

$options["CustomArgumentCompleters"] = @{ 
    "binName" = {Get-ChildItem -Path $global:TH_BinDir | Select-Object -ExpandProperty Name} 
    "dbName" = {Get-ChildItem -Path $global:TH_DBDir\RT5.7\ | Select-Object -ExpandProperty Name} 
    "patchSubDir" ={Get-ChildItem -Path $global:TH_BinDir\Patches\ | Select-Object -ExpandProperty Name} 
    "hmiSubDir" = {Get-ChildItem -Path $global:TH_HMIDir | Select-Object -ExpandProperty Name} 
    "moduleScript" = {Get-ChildItem -Path $global:TH_ModPaths | Select-Object -ExpandProperty Name} 
    "items" = {"bins", "databases", "modules"} 
} 

由於您使用3.0,這將是更簡潔,並完成相同的事情。

$options["CustomArgumentCompleters"] = @{ 
    "binName" = {(Get-ChildItem -Path $global:TH_BinDir).Name} 
    "dbName" = {(Get-ChildItem -Path $global:TH_DBDir\RT5.7\).Name} 
    "patchSubDir" ={(Get-ChildItem -Path $global:TH_BinDir\Patches\).Name} 
    "hmiSubDir" = {(Get-ChildItem -Path $global:TH_HMIDir).Name} 
    "moduleScript" = {(Get-ChildItem -Path $global:TH_ModPaths).Name} 
    "items" = {"bins", "databases", "modules"}   
} 

兩種解決方案都通過擴展單個屬性name的字符串來工作。

+0

謝謝。這很好。 –