PowerShell参数使用空格和制表符完成验证设置值
首先,我很抱歉发布了另一个关于PowerShell和制表符完成的问题。StackOverflow系统确定了几个关于这个主题的优秀问题和答案,但它们似乎都太麻烦,无法在这个简单的PowerShell参数使用空格和制表符完成验证设置值,powershell,active-directory,Powershell,Active Directory,首先,我很抱歉发布了另一个关于PowerShell和制表符完成的问题。StackOverflow系统确定了几个关于这个主题的优秀问题和答案,但它们似乎都太麻烦,无法在这个简单的新ADComputer脚本中实现 参数将进入Splat,以保持脚本可读性。以下代码正确选项卡在ISE中完成,但必须用双引号括起来 PowerShell中是否有任何本机方法允许包含空格的参数集的制表符完成 Param( [参数(必需=$true)] [字符串]$Server, [参数(必需=$true)] [验证集('Env
新ADComputer
脚本中实现
参数将进入Splat
,以保持脚本可读性。以下代码正确选项卡在ISE中完成,但必须用双引号括起来
PowerShell中是否有任何本机方法允许包含空格的参数集的制表符完成
Param(
[参数(必需=$true)]
[字符串]$Server,
[参数(必需=$true)]
[验证集('Env1'、'env2'、'env3')]
[string]$Environment,
[参数(必需=$true)]
[ValidateSet('Application'、'Database'、'File and Print'、'Web Server')]
[字符串]$Type
)
$NewADitems=@{
Name=$server
Path=“OU=$Type,OU=$Environment,OU=较小的DN字符串”
地点:='MySite'
Description=“测试描述”
ManagedBy=“巨大的区分名称字符串”
WhatIf=$true
}
写入主机@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 ...
+ ~~~
编辑:更多信息。如果您在我的示例脚本中为参数
环境
省去了单引号/双引号,则制表符完成将不适用于最终参数类型
。用引号括起第二组将纠正这一问题,但这是一种监视此行为的方法。否,至少在2015年4月5.0预览版之前。选项卡完成功能如您所述。它仍然需要集合周围的引号才能在不抛出错误的情况下实际工作。值得一提的是,当您使用引号开始制表符完成时,它确实添加了匹配类型的结束引号。例如,按“f then Tab”将完成“File and Print”(不确定该功能是何时添加的)
我试图找到方法自动包含引号作为ValidateSet的一部分,包括参数集周围的附加双引号和其他转义引号的尝试。所有尝试都导致制表符完成无法以各种方式工作
一些尝试,以防有人尝试这一途径:
[ValidateSet('Env1','"Env 2"','"Env 3"')]
[ValidateSet('Env1',"'Env 2'","'Env 3'")]
[ValidateSet('Env1','`"Env 2`"',"`'Env 3`'")]
[ValidateSet('Env1','\"Env 2\"',"\'Env 3\'")]
自2013年以来,这一直作为错误输入。根据中列出的解决方法,您可以更新Powershell用于自动完成的
TabExpansion2
函数。为此,只需运行以下代码:
function TabExpansion2
{
[CmdletBinding(DefaultParameterSetName = 'ScriptInputSet')]
Param(
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 0)]
[string] $inputScript,
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 1)]
[int] $cursorColumn,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 0)]
[System.Management.Automation.Language.Ast] $ast,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 1)]
[System.Management.Automation.Language.Token[]] $tokens,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 2)]
[System.Management.Automation.Language.IScriptPosition] $positionOfCursor,
[Parameter(ParameterSetName = 'ScriptInputSet', Position = 2)]
[Parameter(ParameterSetName = 'AstInputSet', Position = 3)]
[Hashtable] $options = $null
)
End
{
if ($psCmdlet.ParameterSetName -eq 'ScriptInputSet')
{
$completion = [System.Management.Automation.CommandCompletion]::CompleteInput(
$inputScript,
$cursorColumn,
$options)
}
else
{
$completion = [System.Management.Automation.CommandCompletion]::CompleteInput(
$ast,
$tokens,
$positionOfCursor,
$options)
}
$count = $completion.CompletionMatches.Count
for ($i = 0; $i -lt $count; $i++)
{
$result = $completion.CompletionMatches[$i]
if ($result.CompletionText -match '\s')
{
$completion.CompletionMatches[$i] = New-Object System.Management.Automation.CompletionResult(
"'$($result.CompletionText)'",
$result.ListItemText,
$result.ResultType,
$result.ToolTip
)
}
}
return $completion
}
}
值得注意的是,字符串插入对于像
Get EventLog-LogName
这样的本机cmdlet可以正常工作,它将正确地封装'internetexplorer'
。尽管如果您为Get EventLog
,您会看到$LogName
实际上没有使用ValidateSet
,因此必须提供智能感知通过另一种机制
其他实例: