You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell不同参数集共用参数名时[ValidateSet()]及Get-Help语法问题

Fixing Parameter Set Syntax Display in Get-Help for PowerShell Advanced Functions

It looks like your parameter sets aren't being properly distinguished by PowerShell's help system, which is why you're seeing a merged syntax instead of separate blocks for Default and TestAccountsOnly. Here's how to fix this:

Core Issues & Solutions

  1. Parameter Sets Need Unique Identifiers: PowerShell can't tell your sets apart if all parameters are shared between them. You need at least one parameter exclusive to each set (like a switch for the test set) to help both the runtime and help system differentiate them.
  2. Split Shared Parameters by Set for Different Validation: To apply different [ValidateSet()] rules to the same parameter name across sets, you need to define the parameter twice—once per set—each with its own validation constraints.

Example Fixed Function

Here's how to adjust your function to meet these requirements:

function New-SecondaryAccount {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param(
        # Common parameter across both sets
        [Parameter(Mandatory = $true, ParameterSetName = 'Default')]
        [Parameter(Mandatory = $true, ParameterSetName = 'TestAccountsOnly')]
        [string]$Name,

        # AccountType for Default set with production valid values
        [Parameter(Mandatory = $true, ParameterSetName = 'Default')]
        [ValidateSet('User', 'Admin', 'Service')]
        [string]$AccountType,

        # AccountType for TestAccountsOnly set with test-specific valid values
        [Parameter(Mandatory = $true, ParameterSetName = 'TestAccountsOnly')]
        [ValidateSet('TestUser', 'TestAdmin', 'TestService')]
        [string]$AccountType,

        # Common parameters across both sets
        [Parameter(Mandatory = $true, ParameterSetName = 'Default')]
        [Parameter(Mandatory = $true, ParameterSetName = 'TestAccountsOnly')]
        [string]$Password,

        [Parameter(Mandatory = $true, ParameterSetName = 'Default')]
        [Parameter(Mandatory = $true, ParameterSetName = 'TestAccountsOnly')]
        [string]$Description,

        [Parameter(Mandatory = $true, ParameterSetName = 'Default')]
        [Parameter(Mandatory = $true, ParameterSetName = 'TestAccountsOnly')]
        [string]$OwnerEmployeeID,

        [Parameter(Mandatory = $false, ParameterSetName = 'Default')]
        [Parameter(Mandatory = $false, ParameterSetName = 'TestAccountsOnly')]
        [string]$AdditionalDescription,

        # Unique switch to identify TestAccountsOnly set
        [Parameter(Mandatory = $false, ParameterSetName = 'TestAccountsOnly')]
        [switch]$TestOnly
    )

    # Example logic to handle parameter sets
    if ($PSCmdlet.ParameterSetName -eq 'TestAccountsOnly') {
        Write-Verbose "Creating test account: $Name ($AccountType)"
    } else {
        Write-Verbose "Creating production account: $Name ($AccountType)"
    }

    # Rest of your function logic here
}

Key Changes Explained

  • Unique Parameter for Test Set: The -TestOnly switch is only present in the TestAccountsOnly set. This gives PowerShell a clear way to distinguish between the two sets, which helps the help system display separate syntax blocks.
  • Split AccountType Parameter: We define AccountType twice—once for each set—each with its own [ValidateSet()]. Even though the parameter name is the same, PowerShell treats them as separate entries tied to their respective sets.
  • Explicit Parameter Set Assignment: Every parameter is explicitly assigned to the sets it belongs to, so there's no ambiguity.

Verify the Fix

After updating your function, run:

Get-Help New-SecondaryAccount

You should now see two distinct SYNTAX blocks: one for Default (with production AccountType values) and one for TestAccountsOnly (with test-specific values and the -TestOnly switch). For full details, use:

Get-Help New-SecondaryAccount -Full

This approach ensures both the runtime and help system correctly recognize and display your parameter sets with their unique validation rules.

内容的提问来源于stack exchange,提问作者trebleCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:11:15