Powershell,开关命名ParameterSets
Powershell, Switch naming ParameterSets
原始问题 - 下面更新 - 标记答案中的最终代码
我希望或多或少是一个简单的问题,但我的大脑被炸了。我正在尝试编写一个模块来设置注册表项权限,名为 '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
我算出了自己的名字,并在此处答案的帮助下,,终于让一切都按我想要的方式工作了。
Get-Help 正确显示,Tab 自动补全功能正常,并且在输入破折号后仅显示有效参数(如果存在其他参数)。
我仍然不完全理解这一切是如何工作的,但它正在工作,这就是我现在关心的。
所以我最终的命名和参数集是:
# -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 中应该只有一个反引号(`)。我不得不加一秒钟才能让它显示在这里。
Get-Help 的结果
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 可能会使它有点难以阅读,但它是正确的。
原始问题 - 下面更新 - 标记答案中的最终代码
我希望或多或少是一个简单的问题,但我的大脑被炸了。我正在尝试编写一个模块来设置注册表项权限,名为 '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
我算出了自己的名字,并在此处答案的帮助下,
Get-Help 正确显示,Tab 自动补全功能正常,并且在输入破折号后仅显示有效参数(如果存在其他参数)。
我仍然不完全理解这一切是如何工作的,但它正在工作,这就是我现在关心的。
所以我最终的命名和参数集是:
# -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 中应该只有一个反引号(`)。我不得不加一秒钟才能让它显示在这里。
Get-Help 的结果
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 可能会使它有点难以阅读,但它是正确的。