简体   繁体   中英

Disable Positional Parameter Binding in Powershell

I want parameters ONLY through alias:

script.ps1 -d site1 -c eac34b2d

This shouldnt be allowed:

script.ps1 site1 eac34b2d

Param(
    [parameter(Mandatory=$true)]
    [alias("d")]
    $DocRoot,
    [alias("c")]
    $Commit
)

Basically you want to disable positional parameter binding. I wrote about a built-in way in Powershell 3.0: use [CmdletBinding(PositionalBinding=$false)] .

http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2011/09/19/how-to-disable-positional-parameter-binding-in-powershell.aspx

A workaround for v2 is available here:

http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/abbc587f-cd7e-4c8d-879f-355339d9d6b7/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM