PowerShell, SSAS, Role, Membership

上篇随笔使用PowerShell创建SSAS Role贴出了如何使用PowerShell批量创建Role,由于个人项目需求,创建Role的步骤和添加Membership分开了,该文贴出如何使用PowerShell批量向Role中添加Membership。

代码如下:

# ---------------------------------------------------------
# AUTHOR: Niko / hewuquan@outlook.com / 2016-10-18
# For XX BI Project - Add Memebers to Role
# Steps:
# Param: MemberList.csv (DealerNo, DealerName, RoleType, Account)
# RoleType: 1: Parts; 2: Service; 3: Sales; 4: GM
# Function: Grant Account Role permissions
# --------------------------------------------------------- # ---------------------------------------------------------
# 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 + "\MemberList.csv"
$log = $path + "\Log.txt"
$date = Get-Date
$iRow = 1
$logMsg $server = New-Object Microsoft.AnalysisServices.Server
$server.Connect("Put Your Server Instance Here")
$dbName = "Put Your SSAS Database Name Here"
$database = $server.Databases.Item($dbName) # ---------------------------------------------------------
# Start Function
# ---------------------------------------------------------
Function Start-Commands
{
Load-CSV
} # ---------------------------------------------------------
# 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
} # ---------------------------------------------------------
# Read Account From CSV and Add to Roles
# Read CSV => Add to Roles
# ---------------------------------------------------------
Function Load-CSV
{
$logMsg = "[INFO] Processing started"
Write-Log Import-Csv $newpath | ForEach-Object{
If ( ($_.DealerNo -eq "") -Or ($_.DealerName -eq "") -Or ($_.RoleType -eq "") -Or ($_.Account -eq "") )
{
$logMsg = "[ERROR] Please provide valid DealerNo, DealerName, RoleType and Account. Processing skipped for line $($iRow)"
Write-Log
Continue
} If ( $_.RoleType -eq 4 ) # 4: GM
{
$logMsg = "[INFO] Processing Line $($iRow), [$($_.DealerNo)], [$($_.DealerName)] ,[General Manager], [$($_.Account)]..."
Write-Log $roleName = $_.DealerNo + " " + $_.DealerName
Add-RoleMember $roleName $_.Account $iRow
}
elseIf ( $_.RoleType -eq 3 ) # 3: Sales
{
$logMsg = "[INFO] Processing Line $($iRow), [$($_.DealerNo)], [$($_.DealerName)] ,[Sales Manager], [$($_.Account)]..."
Write-Log $roleName = $_.DealerNo + " " + $_.DealerName
Add-RoleMember $roleName $_.Account $iRow
}
elseIf ( $_.RoleType -eq 2 ) # 2: Service
{
$logMsg = "[INFO] Processing Line $($iRow), [$($_.DealerNo)], [$($_.DealerName)] ,[Service Manager], [$($_.Account)]..."
Write-Log $roleName = $_.DealerNo + " " + $_.DealerName + " Service"
Add-RoleMember $roleName $_.Account $iRow
$roleName = $_.DealerNo + " " + $_.DealerName + " Parts"
Add-RoleMember $roleName $_.Account $iRow
}
elseIf ( $_.RoleType -eq 1 ) # 1: Parts
{
$logMsg = "[INFO] Processing Line $($iRow), [$($_.DealerNo)], [$($_.DealerName)] ,[Parts Manager], [$($_.Account)]..."
Write-Log $roleName = $_.DealerNo + " " + $_.DealerName + " Parts"
Add-RoleMember $roleName $_.Account $iRow
}
else
{
$logMsg = "[ERROR] Line $($iRow), RoleType: (1: Parts; 2: Service; 3: Sales; 4: GM). No Role for RoleType $($_.RoleType). Processing skipped for line $($iRow)"
Write-Log
} $iRow ++
}
} Function Add-RoleMember
{
param
(
[String] $RoleName,
[String] $Account,
[Int] $iRow
)
begin
{
}
process
{
$roleItem = $database.Roles.FindByName( $RoleName )
# Check Role
if ( !$roleItem)
{
$logMsg = "[ERROR] Role [$($roleName)] doesn't exists in current database. Processing skipped for line $($iRow)"
Write-Log
Return
}
# Check Membership
$IsExists = $false
$roleItem.Members | ForEach-Object{
if ( $_.Name.Contains($Account))
{
$logMsg = "[Info] Account [$($Account)] already exists in Role [$($RoleName)]. Processing skipped for line $($iRow)"
Write-Log
$IsExists = $true
}
}
If ( $IsExists) {Return}
# Add Account To Role
Try
{
$roleItem.Members.Add($Account)
$bi = $roleItem.Update()
$logMsg = "[INFO] Add Account [$($Account)] to Role [$($roleName)] Succeed."
Write-Log
}
Catch
{
$logMsg = "[INFO] Add Account [$($Account)] to Role [$($roleName)] Failed. $($_.Exception.Message)"
Write-Log
}
}
} Start-Commands
Write-Host -NoNewline "Press [Enter] to Exit..."
Read-Host

使用PowerShell为SSAS Role添加Membership的更多相关文章

  1. 使用PowerShell创建SSAS Role

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

  2. 使用powershell为物理网卡添加多个IP地址

    因特殊要求,需要给某物理网卡添加多个IP地址: powershell中有个netsh的命令,添加IPv4地址的方法: add address [name=]<字符串>       [[ad ...

  3. Wordpress 为用户或角色 role 添加 capabilities(权限)

    首先查看角色具有哪些权限: $admin_role_set = get_role( 'administrator' )->capabilities; $author_role_set = get ...

  4. powershell小脚本--批量添加用户属性----导出登录时间

    需求1:某公司所有员工少了MAIL属性,需要批量添加.例如,用户chenyy  添加邮件属性chenyy@xxxx.com 先导出(只导出名字)备用: Get-ADUser -Filter * -Pr ...

  5. Windows 系统PowerShell或cmd设置添加静态路由方式

    电脑上添加静态路由,PowerShell或cmd设置路由 方法/步骤1.首先以管理员身份在“运行”窗口输入cmd或PowerShell(按WIN+R打开运行窗口),然后回车进入命令行,输入 route ...

  6. SSAS动态添加分区(一)

    一.动态分区的好处就不说了,随着时间的推移,不可能一个度量值组都放在一个分区中,处理速度非常慢,如何动态添加分区,如何动态处理分区,成为了很多新手BI工程师一个头痛的问题,废话不多说,分享一下我的经验 ...

  7. SSAS动态添加分区 (转载)

    一.动态分区的好处就不说了,随着时间的推移,不可能一个度量值组都放在一个分区中,处理速度非常慢,如何动态添加分区,如何动态处理分区,成为了很多新手BI工程师一个头痛的问题,废话不多说,分享一下我的经验 ...

  8. Powershell查看SSAS Cube占用磁盘空间

    以下是用powershell查看Cube占用磁盘空间大小的方式.可以编译成函数也可以直接把参数改成需要的服务器名称. Param($ServerName="SERVERNAME") ...

  9. Powershell About Active Directory Group Membership of a domain user

    使用Get-User命令去寻找group membership of a domain user $((Get-ADUser Wendy -Properties *).MemberOf -split ...

随机推荐

  1. python--类与类之间的关系,(魔术方法)特殊成员方法

    1.依赖关系 类与类之间存在这几种关系:1.依赖关系 2.关联关系 3.组合关系 4.聚合关系 5.继承关系 6.实现关系 我们来设置下面两个场景 玩电脑和打僵尸 class Person: def ...

  2. GO学习笔记 - 包内首字母大写的名称是被导出的,才能被其它包代码调用!

    在GO语言的任意包内,如果名称的首字母是大写的,意味着这个名称被导出,在其它包中可以使用“包名.名称”方式来调用,如果名称首字母不是大写,那么只能在这个包内部使用!这个概念还真是和以往接触的编程语言的 ...

  3. mongodb 备份脚本

    ###############备份脚本#!/bin/bash basepath="/data/backup/dump$(date +%Y%m%d%H%M%S)" if [ ! -d ...

  4. 从【BZOJ4173】谈做题技巧

    题目描述 ----------------------------------------------------------------------------------------------- ...

  5. 前端必学内容:webpack3快速入门 1-23节内容参考

    前端必学内容:webpack(模块打包器) webpack3 学习内容,点击即可到达 (1).webpack快速入门——如何安装webpack及注意事项 (2).webpack快速入门——webpac ...

  6. [转] YUM 源优先级插件:Yum Priorities

    Linux 发行版比较多,同时还有很多个人或组织维护了某些特定用途的安装/升级源.Yum Priorities 插件可以用来强制保护源.它通过给各个源设定不同的优先级,使得系统管理员可以将某些源(比如 ...

  7. mysql导入大批量数据出现MySQL server has gone away的解决方法

    因工作需要,需要导入一个200M左右的sql到user库 执行命令 mysql> use user Database changed mysql> source /tmp/user.sql ...

  8. maven 打包 war 包含 WEB-INF/lib 目录

    <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...

  9. sparksql连接mysql

    1.方法1:分别将两张表中的数据加载为DataFrame /* * 方法1:分别将两张表中的数据加载为DataFrame * */ /* Map<String,String> option ...

  10. CODEVS-1215迷宫

    迷宫 原题:传送门 题目描述 Description 在N*N的迷宫内,“#”为墙,“.”为路,“s”为起点,“e”为终点,一共4个方向可以走.从左上角((0,0)“s”)位置处走到右下角((n-1, ...