zoukankan      html  css  js  c++  java
  • 使用PowerShell为SSAS Role添加Membership

    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
  • 相关阅读:
    The Princess and the Pea,摘自iOS应用Snow White and more stories
    Android笔记之自定义的RadioGroup、RadioButton,以及View实例状态的保存与恢复
    Android笔记之获取debug.keystore和release.keystore的MD5/SHA1值
    Android笔记之WebView加载网页的进度回调
    Android Studio停留在“Indexing paused due to batch update”的解决方案
    【Python初级】StringIO和BytesIO读写操作的小思考
    【Python初级】由判定回文数想到的,关于深浅复制,以及字符串反转的问题
    【Python初级】由生成杨辉三角代码所思考的一些问题
    【面试总结-编程】多行两列数据,实现同key的value求和并输出
    【算法与数据结构实战】模拟竖式加法,自定义位数
  • 原文地址:https://www.cnblogs.com/Niko12230/p/5984955.html
Copyright © 2011-2022 走看看