在 PowerShell 中访问枚举名称

Eri*_*der 1 rest powershell enums office365 sharepoint-online

我正在使用 PowerShell 编写脚本,并且在使用枚举类型时遇到了困难。我对 SharePoint Online 执行 REST 调用以获取有关特定组的信息。

$group = Invoke-SPORestMethod -Url "https://tenant.sharepoint.com/sites/site/_api/web/RoleAssignments/GetByPrincipalId(8)?`$expand=RoleDefinitionBindings"
$group.RoleDefinitionBindings.results[0].RoleTypeKind
Run Code Online (Sandbox Code Playgroud)

它返回一个 int,RoleTypeKind,它是 [Microsoft.SharePoint.Client.RoleType]. 我无法访问关联枚举值的 name 属性。我目前正在这样做,但似乎非常错误:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

知道$group.RoleDefinitionBindings.results[0].RoleTypeKind返回枚举的正确 int 后,我​​如何更直接地访问枚举的名称,而不是使用我提出的看似笨拙的实现?

Paw*_*Dyl 5

据我所知,你可以只使用铸造:

[System.AttributeTargets]4096
Run Code Online (Sandbox Code Playgroud)

这导致

Delegate
Run Code Online (Sandbox Code Playgroud)

如果您需要纯字符串,请ToString()按如下方式调用

([System.AttributeTargets]4096).ToString()
Run Code Online (Sandbox Code Playgroud)