我正在尝试编写一个可扩展的实用程序,而不是硬编码各种类型的执行策略。使用Get-Command我可以获得有关Set-ExecutionPolicy的一些信息,但我无法获得参数类型的成员Microsoft.Powershell.ExecutionPolicy
(get-command set-executionpolicy).parameters.executionpolicy
就我而言,这就是我的意思。我尝试过使用get-typedata,但在将Microsoft.Powershell.ExecutionPolicy传递给它时它什么也没有返回。
所以问题是:我如何枚举所有执行策略类型?
答案 0 :(得分:2)
您有两种选择:
#1-与枚举对象属性进行交互:
[Microsoft.PowerShell.ExecutionPolicy] | Get-Member -Static -MemberType Property
TypeName: Microsoft.PowerShell.ExecutionPolicy
Name MemberType Definition
---- ---------- ----------
AllSigned Property static Microsoft.PowerShell.ExecutionPolicy AllSigned {get;}
Bypass Property static Microsoft.PowerShell.ExecutionPolicy Bypass {get;}
Default Property static Microsoft.PowerShell.ExecutionPolicy Default {get;}
RemoteSigned Property static Microsoft.PowerShell.ExecutionPolicy RemoteSigned {get;}
Restricted Property static Microsoft.PowerShell.ExecutionPolicy Restricted {get;}
Undefined Property static Microsoft.PowerShell.ExecutionPolicy Undefined {get;}
Unrestricted Property static Microsoft.PowerShell.ExecutionPolicy Unrestricted {get;}
#2在v3中引入,与enum本身进行交互(这是@PetSerAl评论的更短版本):
# Values()
[Microsoft.PowerShell.ExecutionPolicy].GetEnumNames()
Unrestricted
RemoteSigned
AllSigned
Restricted
Restricted
Bypass
Undefined
v3之前的:
# Names()
[Enum]::GetValues('Microsoft.PowerShell.ExecutionPolicy')