PowerShell Param ValidateSet Options with Spaces and Tabs

First, I apologize for submitting another question regarding PowerShell and the completion of the tab. The StackOverflow system revealed some great questions with answers to this topic, but they all seemed too cumbersome to implement this simple New-ADComputerscript.

Parameters are collected in Splatorder to save the script for reading. The following correct code tab has been added to ISE, but it should be enclosed in double quotes.

Is there any native method in PowerShell to allow tabs to execute 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

Used command and error received:

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 ...
+                                                          ~~~

: . / script Environment, Type. , .

+4
2

, , Powershell 5.0 2015 . . , , . , , . , "f" Tab " " ( , ).

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\'")]
+2

2013 . , , , , TabExpansion2, Powershell . :

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, 'Internet Explorer'. Get-EventLog, , $LogName ValidateSet, intellisense .


:

+1

All Articles