PowerShell, SSAS, Role, DatabasePermission, Cube, Dimension, CubePermission, CubeDimensionCube

在SSAS中创建Role是一个比较繁琐的操作,一般都会涉及到如下操作:创建Role、选择Database Permission、添加Memeberships、选择Cube的Access权限、Cell Data、选择Dimension的Access权限以及DimensionData中Attribute的Allowed/Denied Set.

个人结合工作中的实际情况,通过Poweshell完成了Role的创建,代码如下:

.CSV格式

# ---------------------------------------------------------
# AUTHOR: Niko / hewuquan@outlook.com / 2016-10-18
# For XX BI Project - Create Roles In SSAS
# Steps: Start-Commands => Create-Roles => Insert-Role
# Param: RoleList.csv (DealerNo, DealerName, CompanyID)
# Function: Create 3 Roles for each dealer
# --------------------------------------------------------- # ---------------------------------------------------------
# Load Assemblys And Modules
# ---------------------------------------------------------
If (![System.reflection.Assembly]::LoadWithPartialName(“Microsoft.AnalysisServices”))
{
Write-Host "[ERROR] Microsoft.AnalysisServices Assembly couldn't be loaded. Script will stop!"
Exit 1
}
If (![System.reflection.Assembly]::LoadWithPartialName(“System.IO”))
{
Write-Host "[ERROR] System.IO Assembly couldn't be loaded. Script will stop!"
Exit 1
} # ---------------------------------------------------------
# Static Variables
# ---------------------------------------------------------
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$newpath = $path + "\RoleList.csv"
$log = $path + "\Log.txt"
$date = Get-Date
$iRow = 1
$logMsg $server = New-Object Microsoft.AnalysisServices.Server
$server.Connect("Put You Server Instance Here")
$dbName = "Put You SSAS Database Name Here"
$database = $server.Databases.Item($dbName) # ---------------------------------------------------------
# Start Function
# ---------------------------------------------------------
Function Start-Commands
{
Create-Roles
} # ---------------------------------------------------------
# Function to Log
# ---------------------------------------------------------
Function Write-Log
{
$logMsg = "{0}: {1}" -f $date.ToString("yyyy-MM-dd hh:mm:ss"),$logMsg
Write-Host $logMsg
$logMsg | Out-File $log -Append
} # ---------------------------------------------------------
# Detail: Create Role In SSAS
# Check Role If Exists => Crate Role => Grant Database Access
# => Grant Cube Access Pemission
# => Grant Dimension Permission
# ---------------------------------------------------------
Function Insert-Role
{
param
(
[String] $RoleName,
[Int] $iRow,
[Int] $CompanyID,
[String] $RoleType #All, Parts, Service
)
begin
{
Write-Host "`r`n"
$logMsg = "[INFO] Processing Role $($RoleName)..."
Write-Log
}
process
{
# Check If Exists
If ( $database.Roles.FindByName($RoleName) )
{
#Write-Host $RoleName, $iRow, $CompanyID
$logMsg = "[WARN] Role $($roleName) is already exists in database. Processing skipped for line $($iRow)"
Write-Log
Return
}
# Create Role
$RoletoCreate = New-Object Microsoft.AnalysisServices.Role($RoleName)
Try
{
$database.Roles.Add($RoletoCreate)
$RoletoCreate.Update()
$logMsg = "[INFO] Creat Role ($($RoleName)) Succeed."
Write-Log
}
Catch
{
$logMsg = "[ERROR] Create Role ($($RoleName)) Failed. $($_.Exception.Message)"
Write-Log
Return
}
# Grant Database Access Permission
Try
{
$dbPermission = $database.DatabasePermissions.Add($RoletoCreate.ID)
$dbPermission.ReadDefinition = [Microsoft.AnalysisServices.ReadDefinitionAccess]::Allowed
$dbPermission.Update()
$logMsg = "[INFO] Grant Role ($($RoleName)) Database Access Permission Succeed."
Write-Log
}
Catch
{
$logMsg = "[ERROR] Grant Role ($($RoleName)) Database Access Permission Failed. $($_.Exception.Message)"
Write-Log
}
# Grant Cube Access Pemission
Foreach($cubeItem in $database.Cubes)
{
Try
{
$cubePermission = $cubeItem.CubePermissions.Add( $RoletoCreate.ID )
$cubePermission.Read = [Microsoft.AnalysisServices.ReadAccess]::Allowed # Grant Dimension Permission
# Parts & Service: (Company, Dealer); Clocking (Company); Sales: (Dealer)
if ( $cubeItem.Name -eq "Parts" -Or $cubeItem.Name -eq "Service")
{
$dimItem = $database.Dimensions.FindByName("Company")
$attItem = $dimItem.Attributes.FindByName("BICompany")
$cubeDimPermission = $cubePermission.DimensionPermissions.Add( $dimItem.ID )
$cubeDimPermission.Read = [Microsoft.AnalysisServices.ReadAccess]::Allowed
$attPermission = $cubeDimPermission.AttributePermissions.Add( $attItem.ID )
$attPermission.AllowedSet = "{[Company].[BICompany].&[$($CompanyID)]}" $dimItem = $database.Dimensions.FindByName("Dealer")
$attItem = $dimItem.Attributes.FindByName("BICompany")
$cubeDimPermission = $cubePermission.DimensionPermissions.Add( $dimItem.ID )
$cubeDimPermission.Read = [Microsoft.AnalysisServices.ReadAccess]::Allowed
$attPermission = $cubeDimPermission.AttributePermissions.Add( $attItem.ID )
$attPermission.AllowedSet = "{[Dealer].[BICompany].&[$($CompanyID)]}"
}
if ( $cubeItem.Name -eq "Clocking")
{
$dimItem = $database.Dimensions.FindByName("Company")
$attItem = $dimItem.Attributes.FindByName("BICompany")
$cubeDimPermission = $cubePermission.DimensionPermissions.Add( $dimItem.ID )
$cubeDimPermission.Read = [Microsoft.AnalysisServices.ReadAccess]::Allowed
$attPermission = $cubeDimPermission.AttributePermissions.Add( $attItem.ID )
$attPermission.AllowedSet = "{[Company].[BICompany].&[$($CompanyID)]}"
}
if ( $cubeItem.Name -eq "Sales")
{
$dimItem = $database.Dimensions.FindByName("Dealer")
$attItem = $dimItem.Attributes.FindByName("BICompany")
$cubeDimPermission = $cubePermission.DimensionPermissions.Add( $dimItem.ID )
$cubeDimPermission.Read = [Microsoft.AnalysisServices.ReadAccess]::Allowed
$attPermission = $cubeDimPermission.AttributePermissions.Add( $attItem.ID )
$attPermission.AllowedSet = "{[Dealer].[BICompany].&[$($CompanyID)]}"
} #1,All: All Cubes; 2,Parts: Parts Cube; 3,Service: Service Cube
If ( $RoleType -eq "All" -Or ( $RoleType -eq "Parts" -and $cubeItem.Name -eq "Parts" ) -Or ( $RoleType -eq "Service" -and $cubeItem.Name -eq "Service" ) )
{
$cubePermission.Read = [Microsoft.AnalysisServices.ReadAccess]::Allowed
}
else
{
$cubePermission.Read = [Microsoft.AnalysisServices.ReadAccess]::None
}
$cubePermission.Update() $logMsg = "[INFO] Grant Cube ($($cubeItem.Name)) Read Permission To ($($RoleName)) Succeed."
Write-Log
}
Catch
{
$logMsg = "[ERROR] Grant Cube ($($cubeItem.Name)) Read Permission To ($($RoleName)) Failed. $($_.Exception.Message)"
Write-Log
Return
}
}
# Grant Dimension Permission
}
} # ---------------------------------------------------------
# Read CSV to Create SSAS Roles
# Read CSV => Create 3 Role for each row
# ---------------------------------------------------------
Function Create-Roles
{
$logMsg = "[INFO] Processing started"
Write-Log Import-Csv $newpath | ForEach-Object{
If ( ($_.DealerNo -eq "") -Or ($_.DealerName -eq "") -Or ($_.CompanyID -eq "") )
{
$logMsg = "[ERROR] Please provide valid DealerNo, DealerName and CompanyID. Processing skipped for line $($iRow)"
Write-Log
}
Else
{
$roleName = $_.DealerNo + " " + $_.DealerName # Role 1
$roleNameParts = $roleName + " Parts" # Role Parts
$roleNameService = $roleName + " Services" # Role Service Insert-Role $roleName $iRow $_.CompanyID "All"
Insert-Role $roleNameParts $iRow $_.CompanyID "Parts"
Insert-Role $roleNameService $iRow $_.CompanyID "Service"
}
$iRow ++
} $server.Disconnect() Write-Host "`r`n"
$logMsg = "[INFO] Processing stopped"
Write-Log
} Start-Commands
Write-Host -NoNewline "Press [Enter] to Exit..."
Read-Host

使用PowerShell创建SSAS Role的更多相关文章

  1. 使用PowerShell为SSAS Role添加Membership

    PowerShell, SSAS, Role, Membership 上篇随笔使用PowerShell创建SSAS Role贴出了如何使用PowerShell批量创建Role,由于个人项目需求,创建R ...

  2. Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机

    <Windows Azure Platform 系列文章目录> 本文介绍的是国外的Azure Global.如果是国内由世纪互联运维的Azure China,请参考这篇文档: Azure ...

  3. [Redis]如何通过Powershell创建Redis服务

    目前Redis在中国上线了,不过只是预览版而且不能通过Portal进行操作,不过可以通过Powershell创建,具体如下: 下载最新的Powershell SDK:http://www.window ...

  4. SharePoint 2013 使用PowerShell创建State Service

    今天,搞SPD配置的sp2010wf迁移到sp2013环境上去,发布解决方案都很正常,给列表添加wf的时候报错“该表单无法显示,可能是由于 Microsoft SharePoint Server St ...

  5. SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection

    通过PowerShell创建SharePoint Site Collection,代码如下: Add-PSSnapin microsoft.sharepoint.powershell function ...

  6. 采用powershell创建project网站集(摘抄自https://www.cnblogs.com/jindahao/p/5855668.html)

    采用powershell创建project网站集,具体步骤如下 1.输入License Enable-ProjectServerLicense –Key "23CB6-N4X8Q-WWD7M ...

  7. Azure VMSS ---- PowerShell创建自定义镜像的VMSS集群

    前面一篇文章介绍了如何用PowerShell创建标准镜像的VMSS集群.http://www.cnblogs.com/hengwei/p/7391178.html 本文将介绍,如何用PowerShel ...

  8. Azure VMSS ---- PowerShell创建标准镜像的VMSS集群

    VMSS的创建可以采用Portal.Powershell.Azure CLI或者Template. 但目前Portal创建有很多限制,本文将介绍如何用PowerShell来创建VMSS的集群. 具体的 ...

  9. 在Azure上通过Powershell创建多Interface的Cisco CSR路由器

    前面通过Json的Template在Azure上创建了Cisco的CSR路由器.但那个Json的template只支持1块网卡.如果需要多网卡的Cisco CSR路由器,可以改上篇文章中提到的Json ...

随机推荐

  1. winform textbox控件keydown、keypress、keyup简单介绍

    1.执行先后顺序: keydown-->keypress-->keyup 2.按键相关操作: 1)keydown和keyup参数类型KeyEventArgs(提供了KeyCode)实现形式 ...

  2. linux - 【LAMP环境配置安装注意安装步骤】 9

    (一)安装gcc glibc-devel glibc-headers ==>依赖项 kernel-headers ==>依赖项 libgomp gcc-c++ libstdc++-deve ...

  3. python 缺失值处理(Imputation)

    一.缺失值的处理方法 由于各种各样的原因,真实世界中的许多数据集都包含缺失数据,这些数据经常被编码成空格.nans或者是其他的占位符.但是这样的数据集并不能被scikit - learn算法兼容,因为 ...

  4. flask接收前台的form数据

    转自 http://www.cnblogs.com/wanghaonull/p/6340096.html 我主要是想了解 request.form.get('username') 这一部分

  5. Nginx+SpringBoot搭建负载均衡

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  6. 兼容各浏览器的js回车事件

    HTML代码: <input type="text" onkeydown="EnterPress(event)" /> JS代码: function ...

  7. JavaScript的几种(原型)继承

    定义Foo,Bar 其中,Bar继承Foo a是Bar的实例,包含有Foo和Bar的函数和属性: function Foo(name) { this.name = name; } Foo.protot ...

  8. 聊聊Postgres中的IPC之SI Message Queue

    在 PostgreSQL中,每一个进程都有属于自己的共享缓存(shared cache).例如,同一个系统表在不同的进程中都有对应的Cache来缓存它的元组(对于RelCache来说缓存的是一个Rel ...

  9. 浅谈javascript和python语言的深拷贝

    深拷贝: 之前在开发中我遇到一个很大的bug,经过我多次调试之后我发现原本应该有保存数据的地方数据全部被清空,仔细一看发现原来是被人为删除,明明操作的是一个副本,为什么原本也会跟着一起被删除呢?经过了 ...

  10. [转载+整理]Nginx Location匹配规则

    目录 规则语法 location 分类 匹配顺序: 扩展 location / {}和 location =/ {}的区别 测试 规则语法 语法 匹配规则 空 普通匹配(遵循最大前缀匹配规则, 优先度 ...