4

Apologies if this appears to be a gimme-teh-codez question, but i'm asking this because i'm a n00b when it comes to Powershell.

How do i use Powershell to add a particular domain user or group to a specific reporting role in SSRS2008 (say the Content Manager or Browser role)? Is there a simple one or two line script to achieve it?

Thanks!

(I have previously posted this question on SO here).

4

I came across this question whilst trying to find the same myself.
In the end, I wrote this function, which works with SSRS 2008 (as originally requested), all the way up to the current version of SSRS (2016).

Whist it's not a two-line answer, it does define a function that, once created, provides a single-line method... so in my mind, that counts (especially as you can't do this with just two lines).

Hope someone finds it useful!

function Add-SSRSUserRole
(   
    [string]$reportServerUrl,[string]$userGroup,[string]$requiredRole,[string]$folder,[bool]$inheritFromParent
)
{
    #Ensure we stop on errors
    $ErrorActionPreference = "Stop";
    #Connect to the SSRS webservice 
    $ssrs = New-WebServiceProxy -Uri "$reportServerUrl" -UseDefaultCredential;
    $namespace = $ssrs.GetType().Namespace;
    $changesMade = $false;

    #Look for a matching policy     
    $policies = $ssrs.GetPolicies($folder, [ref]$inheritFromParent);
    if ($policies.GroupUserName -contains $userGroup)
    {
        Write-Host "User/Group already exists. Using existing policy.";
        $policy = $policies | where {$_.GroupUserName -eq $userGroup} | Select -First 1 ;
    }
    else
    {
        #A policy for the User/Group needs to be created
        Write-Host "User/Group was not found. Creating new policy.";
        $policy = New-Object -TypeName ($namespace + '.Policy');
        $policy.GroupUserName = $userGroup;
        $policy.Roles = @();
        $policies += $policy;
        $changesMade = $true;
    }

    #Now we have the policy, look for a matching role
    $roles = $policy.Roles;
    if (($roles.Name -contains $requiredRole) -eq $false)
    {
        #A role for the policy needs to added
        Write-Host "Policy doesn't contain specified role. Adding.";
        $role = New-Object -TypeName ($namespace + '.Role');
        $role.Name = $requiredRole;
        $policy.Roles += $role;
        $changesMade = $true;
    }
    else 
    {
        Write-Host "Policy already contains specified role. No changes required.";
    }

    #If changes were made...
    if ($changesMade)
    {
        #...save them to SSRS
        Write-Host "Saving changes to SSRS.";
        $ssrs.SetPolicies($folder, $policies);
    }
    Write-Host "Complete.";
}

[string]$url = "http://localhost/ReportServer/ReportService2006.asmx?wsdl";
Add-SSRSUserRole $url "Everyone" "Browser" "/MyReportFolder" $true;
Add-SSRSUserRole $url "Domain\User" "Browser" "/MyReportFolder" $true;
| improve this answer | |
  • 2
    It doesn't matter that it isn't two lines, it's great to have an answer after 7 years :) – slugster May 9 '17 at 13:22
  • Just to note, this script worked fine once I changed the url to /ReportServer/ReportService2010.asmx?wsdl – Pondidum May 29 '18 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.