我正在使用PowerShell脚本,我一直在使用枚举类型时遇到困难。我对SharePoint Online执行REST调用以获取有关特定组的信息。在PowerShell中访问枚举名称
$group = Invoke-SPORestMethod -Url "https://tenant.sharepoint.com/sites/site/_api/web/RoleAssignments/GetByPrincipalId(8)?`$expand=RoleDefinitionBindings"
$group.RoleDefinitionBindings.results[0].RoleTypeKind
它返回一个int,RoleTypeKind,即在[Microsoft.SharePoint.Client.RoleType]
一个枚举。我无法访问assciated枚举值的名称属性。目前,我正在做这样的,但它似乎可怕的错误:
function getGroupPermissionKind([int]$roleType){
#https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.roletype.aspx
[Enum]::GetValues([Microsoft.SharePoint.Client.RoleType]) | foreach {
$Name = $_
$Value = ([Microsoft.SharePoint.Client.RoleType]::$_).value__
if ($Value -eq $roleType){
return $Name
}
}
}
明知$group.RoleDefinitionBindings.results[0].RoleTypeKind
返回枚举的正确INT,我怎么能更直接地访问枚举的名字而不是看似janky实施我想到了?
我不希望做一个反向查找,但我觉得我的实现是可怕的。必须有一种原生的方式来做到这一点,我并没有把握。 –
呃...我刚刚贴了一张?当然,如果你已经有一个'RoleType'对象而不是数字值,你应该可以通过调用它的'ToString()'方法来获得名字,如[这个答案]中所建议的(http://stackoverflow.com /一个/1630171分之38568919)。 –