带有空格和制表符补全的PowerShell Param ValidateSet值 [英] PowerShell Param ValidateSet values with Spaces and Tab completion

查看:92
本文介绍了带有空格和制表符补全的PowerShell Param ValidateSet值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于发布有关PowerShell和制表符完成的另一个问题,我深表歉意。 StackOverflow系统找到了几个很好的问题,并给出了与此主题相关的答案,但是它们似乎都太麻烦了,无法在这个简单的 New-ADComputer 脚本中实现。

First, I do apologize for posting another question concerning PowerShell and tab completion. The StackOverflow system identified several excellent questions with answers concerning this very topic, but they all seemed too cumbersome to implement into this simple New-ADComputer script.

参数将放入 Splat ,以保持脚本可读性。以下代码选项卡在ISE中正确完成了,但必须用双引号引起来。

The params are going into a Splat to keep the script readable. The following code correctly tab completes in the ISE, but must be wrapped in double quotes.

PowerShell中是否有任何本机方法允许选项卡完成包括以下内容的参数集

Is there any native method in PowerShell to allow for tab completion of Parameter Sets that include spaces?

Param(
  [Parameter(Mandatory=$true)]
  [string]$Server,
  [Parameter(Mandatory=$true)]
  [ValidateSet('Env1','Env 2','Env 3')]
  [string]$Environment,
  [Parameter(Mandatory=$true)]
  [ValidateSet('Application','Database','File and Print','Web Server')]
  [string]$Type
)
$NewADitems = @{
  Name        = $server
  Path        = "OU=$Type,OU=$Environment,OU=Smaller DN string"
  Location    ='MySite'
  Description = "Test Description"
  ManagedBy   = "Huge Distingushed Name string"
  WhatIf      = $true
}
Write-Host @NewADitems

使用命令并收到错误:

PS C:\Scripts> .\ADComputer-ParamTest.ps1 -Server ThisTest -Environment Env 3 -Type File and Print
C:\Scripts\ADComputer-ParamTest.ps1 : Cannot validate argument on parameter
'Environment'. The argument "Env" does not belong to the set "Env1,Env 2,Env3"
specified by the ValidateSet attribute. Supply an argument that is in the
set and then try the command again.At line:1 char:58
+ .\ADComputer-ParamTest.ps1 -Server ThisTest -Environment Env 3 -Type File and Pr ...
+                                                          ~~~

编辑:更多信息。如果您在示例脚本中为参数 Environment 取消了单引号/双引号,则制表符补全将不适用于最终参数 Type 。用引号括起来的第二组将纠正此问题,但这是一种监视此行为的方法。

More information. If you leave off the single/double quotes in my example script for the parameter Environment, tab completion will not work for the final parameter Type. Enclosing the 2nd set in quotes will correct this but it's a way to keep watch for this behavior.

推荐答案

不,至少要向上Powershell 5.0 2015年4月预览版。选项卡补全按您的描述工作。它仍然需要集合周围的引号才能真正起作用而不会引发错误。不论价格多少,当您使用引号开始制表符补全时,它的确会添加匹配类型的结束引号。例如,按 f,Tab将完成文件和打印(不确定何时将其添加为功能)。

No, at least up to Powershell 5.0 April 2015 preview. Tab completion works as you describe. It will still need the quotes around the set to actually work without throwing the error. For what it's worth, it does add the closing quote of the matching type when you start the tab completion with a quote. For example, pressing "f then Tab will complete to "File and Print"(not sure when that was added as a feature).

我试图找到自动将引号作为ValidateSet的一部分,包括在参数集周围的附加双引号和其他转义引号的尝试。所有尝试均导致制表符补全无法以各种方式工作。

I tried finding ways to auto-include the quotes as part of the ValidateSet including additional double quotes around the parameter sets and other attempts at escaping quotes. All attempts resulted in tab completion not working in various ways.

一些尝试,以防万一有人可以尝试这种途径:

Some of the attempts, in case anyone might try that avenue:

[ValidateSet('Env1','"Env 2"','"Env 3"')]
[ValidateSet('Env1',"'Env 2'","'Env 3'")]
[ValidateSet('Env1','`"Env 2`"',"`'Env 3`'")]
[ValidateSet('Env1','\"Env 2\"',"\'Env 3\'")]

这篇关于带有空格和制表符补全的PowerShell Param ValidateSet值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆