0

原始问题 - 下面更新 - 标记答案中的最终代码

我希望或多或少是一个简单的问题,但我的大脑被炸了。我正在尝试编写一个模块来设置注册表项权限,名为“Set-RegistryPermissions”,并且在命名我的开关和创建可接受的参数集时遇到了一点问题。我想出了以下内容,但我到了最后一行并被难住了。

    # -Recurse                  Sets Permissions for supplied key and subkeys (entire tree)

    # -Inherit                  Sets Inheritance for supplied key
    # -SubkeyInherit            Sets Inheritance for only subkeys (entire subkey tree)

    # -Inherit -Recurse         Sets Inheritance for supplied key and subkeys (entire tree)
    # -SubkeyInherit -Recurse   Sets Permissions for supplied key and Inheritance for subkeys (entire subkey tree)

我越看越困惑。也许我可以结合 -Inherit 和 -SubkeyInherit 而不是 -Inherit -Recurse 或者重新开始并拥有类似 SetSuppliedKey、Recurse、Set... 啊哈我又困惑了。请问有什么建议吗?

- - 更新 - -

针对@Scepticalist 的评论,我想出了以下内容,这也允许我添加“-Permissions -Subkeys”。

我确实认为我可以将 '-Permissions -Recurse' 更改为 '-Permissions -All' 但这听起来更难理解,或者我可以将 '-Inherit -All' 更改为 '-Inherit -Recurse' 并废弃 -All 开关,但是这可能会使最后一个选项令人困惑,例如您尝试递归所有权限以及继承。

也许我只是想多了,或者试图在一个命令中做太多事情。如果命令运行两次,设置权限然后设置继承可能会更容易。

如果您发现任何问题或认为它太复杂,请告诉我您的想法。谢谢你。

# -Permissions (Parameter)
# -Recurse (Switch)
# -Inherit (Switch)
# -Subkeys (Switch)
# -All (Switch)

# -Permissions              Sets Permissions for supplied key
# -Permissions -Subkeys     Sets Permissions for only subkeys (entire subkey tree)
# -Permissions -Recurse     Sets Permissions for supplied key and subkeys (entire tree)

# -Inherit                  Sets Inheritance for supplied key
# -Inherit -Subkeys         Sets Inheritance for only subkeys (entire subkey tree)
# -Inherit -All             Sets Inheritance for supplied key and subkeys (entire tree)

# -Permissions -Inherit -Subkeys    Sets Permissions for supplied key and Inheritance for subkeys (entire subkey tree)
# -Permissions -Inherit -All        Sets Permissions for supplied key and Inheritance for entire tree

这些将是唯一有效的组合,无效组合的示例是 -Permissions -Subkeys -Recurse 或 -Permissions -Subkeys -All

[编辑]

阅读它,我想我可能会将 'Subkeys' 更改为 'InheritSubkeys' 并废弃 'All' 开关,使最后 4 行变为读取,

# -InheritSubkeys           Sets Inheritance for only subkeys (entire subkey tree)
# -Inherit -InheritSubkeys  Sets Inheritance for supplied key and subkeys (entire tree)

# -Permissions -InheritSubkeys            Sets Permissions for supplied key and Inheritance for subkeys (entire subkey tree)
# -Permissions -Inherit -InheritSubkeys   Sets Permissions for supplied key and Inheritance for entire tree
4

1 回答 1

0

我计算出我的名字,并从这里的答案中得到了一点帮助,一个参数集中的多个强制参数,我终于让一切都按照我想要的方式工作了。

Get-Help 显示正确,选项卡补全工作正常,并且在其他人存在时键入破折号后仅显示有效参数。

我仍然不完全理解这一切是如何运作的,但它正在运作,这就是我现在所关心的。

所以我最终的命名和参数集是:

# -User          (Mandatory)
# -RegKey        (Mandatory)
# -Rights        (Allow | Deny)
# -Permissions   (Validateset)
# -Recurse       (Switch)
# -SubkeysOnly   (Switch)
# -Inherit       (SingleKey | All | Subkeys)

# -Permissions -Rights                    Sets Permissions for supplied key
# -Permissions -Rights -Recurse           Sets Permissions for supplied key and subkeys (entire tree)
# -Permissions -Rights -SubkeysOnly       Sets Permissions for only subkeys (entire subkey tree)

# -Inherit SingleKey                      Sets Inheritance for supplied key
# -Inherit Subkeys                        Sets Inheritance for only subkeys (entire subkey tree)
# -Inherit All                            Sets Inheritance for supplied key and subkeys (entire tree)

# -Permissions -Rights -Inherit Subkeys   Sets Permissions for supplied key and Inheritance for subkeys (entire subkey tree)
# -Permissions -Rights -Inherit All       Sets Permissions for supplied key and Inheritance for entire tree 

function Set-RegistryPermissions
{
    param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$false)]
    [ValidateNotNullOrEmpty()]
    [string]$User,

    [Parameter(Mandatory=$true, ValueFromPipeline=$false)]
    [ValidateNotNullOrEmpty()]
    [string]$RegKey,

    [Parameter(ParameterSetName = 'PermissionsInherit', Mandatory = $true)]
    [Parameter(ParameterSetName = 'SubKeysOnly', Mandatory = $true)]
    [Parameter(ParameterSetName = 'Recurse', Mandatory = $true)]
    [Parameter(ParameterSetName = 'PermissionsRights', Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [Validateset("ChangePermissions","CreateSubKey","Delete","EnumerateSubKeys","ExecuteKey","FullControl", ``
                 "Notify","QueryValues","ReadKey","ReadPermissions","SetValue","TakeOwnership","WriteKey")]
    [string[]]$Permissions,

    [Parameter(ParameterSetName = 'Recurse', Mandatory = $true)]
    [Switch]$Recurse,

    [Parameter(ParameterSetName = 'SubKeysOnly', Mandatory = $true)]
    [Switch]$SubKeysOnly,

    [Parameter(ParameterSetName = 'Inherit', Mandatory = $true)]
    [Parameter(ParameterSetName = 'PermissionsInherit', Mandatory = $true, ValueFromPipeline=$false)]
    [ValidateNotNullOrEmpty()]
    [Validateset("SingleKey","All","Subkeys")]
    [string]$Inherit,

    [Parameter(ParameterSetName = 'SubKeysOnly', Mandatory = $true)]
    [Parameter(ParameterSetName = 'Recurse', Mandatory = $true)]
    [Parameter(ParameterSetName = 'PermissionsInherit', Mandatory = $true)]
    [Parameter(ParameterSetName = 'PermissionsRights', Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [Validateset("Allow","Deny")]
    [string]$Rights
    )
}

Get-Help Set-RegistryPermissions

如果要复制和测试,则权限 ValidateSet 中应该只有一个反引号(`)。我必须加一秒钟才能在此处显示。

获取帮助的结果

SYNTAX
Set-RegistryPermissions -User <string> -RegKey <string> -Permissions {ChangePermissions | CreateSubKey | Delete | EnumerateSubKeys | ExecuteKey | FullControl | Notify | QueryValues | ReadKey | ReadPermissions | SetValue | TakeOwnership | WriteKey} 
-Rights {Allow | Deny}  [<CommonParameters>]

Set-RegistryPermissions -User <string> -RegKey <string> -Permissions {ChangePermissions | CreateSubKey | Delete | EnumerateSubKeys | ExecuteKey | FullControl | Notify | QueryValues | ReadKey | ReadPermissions | SetValue | TakeOwnership | WriteKey} 
-Recurse -Rights {Allow | Deny}  [<CommonParameters>]

Set-RegistryPermissions -User <string> -RegKey <string> -Permissions {ChangePermissions | CreateSubKey | Delete | EnumerateSubKeys | ExecuteKey | FullControl | Notify | QueryValues | ReadKey | ReadPermissions | SetValue | TakeOwnership | WriteKey} 
-SubKeysOnly -Rights {Allow | Deny}  [<CommonParameters>]

Set-RegistryPermissions -User <string> -RegKey <string> -Permissions {ChangePermissions | CreateSubKey | Delete | EnumerateSubKeys | ExecuteKey | FullControl | Notify | QueryValues | ReadKey | ReadPermissions | SetValue | TakeOwnership | WriteKey} 
-Inherit {SingleKey | All | Subkeys} -Rights {Allow | Deny}  [<CommonParameters>]

Set-RegistryPermissions -User <string> -RegKey <string> -Inherit {SingleKey | All | Subkeys}  [<CommonParameters>]

权限的 ValidateSet 可能会使它有点难以阅读,但它是正确的。

于 2019-11-04T09:39:40.400 回答