Skip to content

Commit

Permalink
Granting db rights idempotent, so always run
Browse files Browse the repository at this point in the history
  • Loading branch information
deitch committed Feb 22, 2018
1 parent 67d2340 commit 99a5576
Showing 1 changed file with 28 additions and 37 deletions.
65 changes: 28 additions & 37 deletions ReportingServicesTools/Functions/Admin/Set-RsDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ function Set-RsDatabase
.PARAMETER IsExistingDatabase
Specify this switch if the database to use already exists, and prevent creation of the database.
.PARAMETER HasDatabaseUserRights
Specify this switch if the runtime database user already has access rights to the database, and prevent granting the user rights.
.PARAMETER DatabaseCredentialType
Indicate what type of runtime credentials to use when connecting to the database: Windows, SQL, or Service Account.
Expand Down Expand Up @@ -97,9 +94,6 @@ function Set-RsDatabase
[switch]
$IsExistingDatabase,

[switch]
$HasDatabaseUserRights,

[Parameter(Mandatory = $true)]
[Alias('Authentication')]
[Microsoft.ReportingServicesTools.SqlServerAuthenticationType]
Expand Down Expand Up @@ -168,7 +162,7 @@ function Set-RsDatabase
}

# must have credentials passed
if ($AdminDatabaseCredentialType -like 'sql')
if ($isSQLAdminAccount)
{
if ($AdminDatabaseCredential -eq $null)
{
Expand Down Expand Up @@ -222,43 +216,40 @@ function Set-RsDatabase
#endregion Create Database if necessary

#region Configuring Database rights
if (-not $HasDatabaseUserRights)
# Step 3 - Generate database rights script
Write-Verbose "Generating database rights script..."
$isWindowsAccount = ($DatabaseCredentialType -like "Windows") -or ($DatabaseCredentialType -like "ServiceAccount")
$result = $rsWmiObject.GenerateDatabaseRightsScript($username, $Name, $IsRemoteDatabaseServer, $isWindowsAccount)
if ($result.HRESULT -ne 0)
{
# Step 3 - Generate database rights script
Write-Verbose "Generating database rights script..."
$isWindowsAccount = ($DatabaseCredentialType -like "Windows") -or ($DatabaseCredentialType -like "ServiceAccount")
$result = $rsWmiObject.GenerateDatabaseRightsScript($username, $Name, $IsRemoteDatabaseServer, $isWindowsAccount)
if ($result.HRESULT -ne 0)
{
Write-Verbose "Generating database rights script... Failed!"
throw "Failed to generate the database rights script from the report server using WMI. Errorcode: $($result.HRESULT)"
}
else
{
$SQLscript = $result.Script
Write-Verbose "Generating database rights script... Complete!"
}
Write-Verbose "Generating database rights script... Failed!"
throw "Failed to generate the database rights script from the report server using WMI. Errorcode: $($result.HRESULT)"
}
else
{
$SQLscript = $result.Script
Write-Verbose "Generating database rights script... Complete!"
}

# Step 4 - Run Database rights script
Write-Verbose "Executing database rights script..."
try
# Step 4 - Run Database rights script
Write-Verbose "Executing database rights script..."
try
{
if ($isSQLAdminAccount)
{
if ($isSQLAdminAccount)
{
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -ErrorAction Stop -Username $adminUsername -Password $adminPassword
}
else
{
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -ErrorAction Stop
}
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -ErrorAction Stop -Username $adminUsername -Password $adminPassword
}
catch
else
{
Write-Verbose "Executing database rights script... Failed!"
throw
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -ErrorAction Stop
}
Write-Verbose "Executing database rights script... Complete!"
}
catch
{
Write-Verbose "Executing database rights script... Failed!"
throw
}
Write-Verbose "Executing database rights script... Complete!"
#endregion Configuring Database rights

#region Update Reporting Services database configuration
Expand Down

0 comments on commit 99a5576

Please sign in to comment.