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. 73. 矩阵置零 leetcode JAVA

    题目: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ...

  2. datatime模块

    https://www.cnblogs.com/cindy-cindy/p/6720196.html

  3. [Flex] 组件Tree系列 —— 运用variableRowHeight和wordWrap设置可变行高

    mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:运用variableRowHeig ...

  4. 【git】——简单用法

    git 更新远程代码到本地 git fetch origin master git merge origin/master 冲突: Your local changes to the followin ...

  5. [转] 检查更新时出错:无法启动更新检查(错误代码为 4: 0x80070005 — system level)

    Google浏览器Chrome更新到时候提示错误:检查更新时出错:无法启动更新检查(错误代码为 4: 0x80070005 -- system level),很有可能是Chrome更新服务被禁用了,我 ...

  6. php prepare

    详见 https://stackoverflow.com/questions/134099/are-pdo-prepared-statements-sufficient-to-prevent-sql- ...

  7. leetcode-887-三维形体投影面积

    题目描述: 在 N * N 的网格中,我们放置了一些与 x,y,z 三轴对齐的 1 * 1 * 1 立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. ...

  8. 能够放在文档的 <head> 中的各种配置元素

    一份关于任何可以写入到你的文档中 <head> 部分的清单 最小推荐 <meta charset="utf-8"> <meta http-equiv= ...

  9. docker阿里云镜像加速器使用

    加速器使用:加快镜像下载速度 访问www.aliyun.com: 登录之后点击”控制台“,选择“产品与服务“: 选择“容器镜像服务“: 设定密码后选择“镜像加速器”:  这里会有一个加速器地址:  在 ...

  10. Oracle数据库学习(一):虚拟机下Oracle Linux的安装与配置

    这篇博文主要以图片的形式讲述Oracle Linux在虚拟机下的安装与配置 一.前期虚拟机安装ISO文件的配置 1.创建新的虚拟机 2.选择“自定义(高级)”选项,下一步,默认“虚拟机硬件兼容性”或选 ...