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