首页 > 解决方案 > 通过 PowerShell 编写 SSAS 表格角色脚本的问题

问题描述

我正在尝试创建一个将输出 SSAS 表格角色的 Powershell 脚本,但我遇到了一些困难。我在网上搜索了帮助,发现了一些似乎相当普遍但由于某种原因对我不起作用的代码。代码如下

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.AnalysisServices.Tabular”) > $NULL

$Server = New-Object Microsoft.AnalysisServices.Server

If ($? -eq $False)

{

      Throw “Cannot load assembly”

      EXIT

}

# SSAS server name variable
$SSASServerName = "."

# Try to connect to the SSAS server
$SSASServer = New-Object Microsoft.AnalysisServices.Server
$SSASServer.Connect($SSASServerName)

# Object to store the result
$Result = @()

# Get the SSAS databases and loop thru each of them
foreach ($DB in $SSASServer.Databases)
{
    # Get the SSAS database
    $SSASDatabase = $SSASServer.Databases.Item($DB.ID)

    # Get the roles available within the SSAS database and loop thru each of them
    foreach ($Role in $SSASDatabase.Roles)
    {
        # Get the members within the role and loop thru each one of them
        foreach ($UserName in $Role.Members)
        {
            # Create a new object that would store the database name, role name and member user name
            $ItemResult = New-Object System.Object
            $ItemResult | Add-Member -type NoteProperty -name DatabaseName -value $DB.Name
            $ItemResult | Add-Member -type NoteProperty -name RoleName -value $Role.Name
            $ItemResult | Add-Member -type NoteProperty -name UserName -value $UserName.Name

            # Put the item result and append it to the result object
            $Result +=$ItemResult
        }
    }
}

当我运行它时,它不会返回任何错误,但不会给我任何角色,并且服务器上的 SSAS 数据库中有很多角色。它似乎为 Roles 属性返回一个空集。

有人可以帮我指出我出错的方向吗?

在此先感谢迈克

标签: ssas-tabular

解决方案


推荐阅读