Skip to content

Commit

Permalink
Merge pull request #203 from ClaudioESSilva/VariousFixes
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
jtarquino authored Dec 3, 2018
2 parents 53436fd + d34ea61 commit dcc87f7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,51 @@ function Set-RsDatabaseCredentials
<#
.SYNOPSIS
This script configures the credentials used to connect to the database used by SQL Server Reporting Services.
.DESCRIPTION
This script configures the credentials used to connect to the database used by SQL Server Reporting Services. You must be an admin in RS and SQL Server in order to perform this operation successfully.
.PARAMETER DatabaseCredentialType
Indicate what type of credentials to use when connecting to the database.
.PARAMETER DatabaseCredential
Specify the credentials to use when connecting to the SQL Server.
Note: This parameter will be ignored whenever DatabaseCredentialType is set to ServiceAccount!
.PARAMETER IsRemoteDatabaseServer
Specify this parameter when the database server is on a different machine than the machine Reporting Services is on.
.PARAMETER ReportServerInstance
Specify the name of the SQL Server Reporting Services Instance.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER ReportServerVersion
Specify the version of the SQL Server Reporting Services Instance.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER ComputerName
The Report Server to target.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER Credential
Specify the credentials to use when connecting to the Report Server.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER QueryTimeout
Specify how many seconds the query will be running before exit by timeout. Default value is 30.
.EXAMPLE
Set-RsDatabaseCredentials -DatabaseCredentialType Windows -DatabaseCredential $myCredentials
Description
-----------
This command will configure Reporting Services to connect to the database using Windows credentials ($myCredentials).
.EXAMPLE
Set-RsDatabaseCredentials -DatabaseCredentialType SQL -DatabaseCredential $sqlCredentials
Description
-----------
This command will configure Reporting Services to connect to the database using SQL credentials ($sqlCredentials).
.EXAMPLE
Set-RsDatabaseCredentials -DatabaseCredentialType ServiceAccount
Description
Expand All @@ -66,26 +69,29 @@ function Set-RsDatabaseCredentials

[switch]
$IsRemoteDatabaseServer,

[Alias('SqlServerInstance')]
[string]
$ReportServerInstance,

[Alias('SqlServerVersion')]
[Microsoft.ReportingServicesTools.SqlServerVersion]
$ReportServerVersion,

[string]
$ComputerName,

[System.Management.Automation.PSCredential]
$Credential
$Credential,

[int]
$QueryTimeout = 30
)

if ($PSCmdlet.ShouldProcess((Get-ShouldProcessTargetWmi -BoundParameters $PSBoundParameters), "Configure to use $DatabaseCredentialType authentication"))
{
$rsWmiObject = New-RsConfigurationSettingObjectHelper -BoundParameters $PSBoundParameters

#region Validating authentication and normalizing credentials
$username = ''
$password = $null
Expand All @@ -94,7 +100,7 @@ function Set-RsDatabaseCredentials
$username = $rsWmiObject.WindowsServiceIdentityActual
$password = ''
}

else
{
if ($DatabaseCredential -eq $null)
Expand All @@ -105,10 +111,10 @@ function Set-RsDatabaseCredentials
$password = $DatabaseCredential.GetNetworkCredential().Password
}
#endregion Validating authentication and normalizing credentials

$databaseName = $rsWmiObject.DatabaseName
$databaseServerName = $rsWmiObject.DatabaseServerName

#region Configuring Database rights
# Step 1 - Generate database rights script
Write-Verbose "Generating database rights script..."
Expand All @@ -124,12 +130,12 @@ function Set-RsDatabaseCredentials
$SQLscript = $result.Script
Write-Verbose "Generating database rights script... Complete!"
}

# Step 2 - Run Database rights script
Write-Verbose "Executing database rights script..."
try
{
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLscript -ErrorAction Stop
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLscript -QueryTimeout $QueryTimeout -ErrorAction Stop
}
catch
{
Expand All @@ -138,7 +144,7 @@ function Set-RsDatabaseCredentials
}
Write-Verbose "Executing database rights script... Complete!"
#endregion Configuring Database rights

#region Update Reporting Services database configuration
# Step 3 - Update Reporting Services to connect to new database now
Write-Verbose "Updating Reporting Services to connect to new database..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function New-RsRestFolder
This script creates a new folder in the Report Server
.PARAMETER RsFolder
Specify the location where the folder should be created
Specify the location where the folder should be created
.PARAMETER FolderName
Specify the name of the the new folder
Expand Down Expand Up @@ -91,11 +91,11 @@ function New-RsRestFolder

if ($Credential -ne $null)
{
$response = Invoke-WebRequest -Uri $foldersUri -Method Post -WebSession $WebSession -Body $payloadJson -ContentType "application/json" -Credential $Credential -Verbose:$false
$response = Invoke-WebRequest -Uri $foldersUri -Method Post -WebSession $WebSession -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -Credential $Credential -Verbose:$false
}
else
{
$response = Invoke-WebRequest -Uri $foldersUri -Method Post -WebSession $WebSession -Body $payloadJson -ContentType "application/json" -UseDefaultCredentials -Verbose:$false
$response = Invoke-WebRequest -Uri $foldersUri -Method Post -WebSession $WebSession -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -UseDefaultCredentials -Verbose:$false
}

Write-Verbose "Folder $TargetFolderPath was created successfully!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function Set-RsRestItemDataSource
{
throw "DataModelDataSource.AuthType must be specified: $ds!"
}
elseif (($ds.DataModelDataSource.AuthType -LIKE 'Windows' -or
elseif (($ds.DataModelDataSource.AuthType -LIKE 'Windows' -or
$ds.DataModelDataSource.AuthType -LIKE 'UsernamePassword' -or
$ds.DataModelDataSource.AuthType -LIKE 'Impersonate') -and
($ds.DataModelDataSource.Username -eq $null -or
Expand All @@ -171,9 +171,9 @@ function Set-RsRestItemDataSource
elseif ($ds.DataSourceSubType -eq $null)
{
# DataSourceType, ConnectionString and CredentialRetrieval must always be specified!
if ($ds.DataSourceType -eq $null -or
if ($ds.DataSourceType -eq $null -or
$ds.ConnectionString -eq $null -or
$ds.CredentialRetrieval -eq $null -or
$ds.CredentialRetrieval -eq $null -or
!($ds.CredentialRetrieval -LIKE 'Integrated' -or
$ds.CredentialRetrieval -LIKE 'Store' -or
$ds.CredentialRetrieval -LIKE 'Prompt' -or
Expand All @@ -183,7 +183,7 @@ function Set-RsRestItemDataSource
}
elseif ($ds.DataModelDataSource -ne $null)
{
# since this is an embedded data source for Paginated Report/Shared data set,
# since this is an embedded data source for Paginated Report/Shared data set,
# you should not set any value to DataModelDataSource
throw "You cannot specify DataModelDataSource for this datasource: $ds!"
}
Expand All @@ -208,8 +208,8 @@ function Set-RsRestItemDataSource

$dataSourcesUri = [String]::Format($dataSourcesUri, $RsItemType + "s", $RsItem)

# Converting $DataSources into array as PowerBIReport(...)/DataSources expects data sources
# to be in an array in the request body. If $DataSources is already an array, this operation
# Converting $DataSources into array as PowerBIReport(...)/DataSources expects data sources
# to be in an array in the request body. If $DataSources is already an array, this operation
# combines $DataSources array with an empty array, so result is still an array.
$dataSourcesArray = @($DataSources)

Expand All @@ -235,11 +235,11 @@ function Set-RsRestItemDataSource
Write-Verbose "Updating data sources for $($RsItem)..."
if ($Credential -ne $null)
{
Invoke-WebRequest -Uri $dataSourcesUri -Method $method -Body $payloadJson -ContentType "application/json" -WebSession $WebSession -Credential $Credential -Verbose:$false | Out-Null
Invoke-WebRequest -Uri $dataSourcesUri -Method $method -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -WebSession $WebSession -Credential $Credential -Verbose:$false | Out-Null
}
else
{
Invoke-WebRequest -Uri $dataSourcesUri -Method $method -Body $payloadJson -ContentType "application/json" -WebSession $WebSession -UseDefaultCredentials -Verbose:$false | Out-Null
Invoke-WebRequest -Uri $dataSourcesUri -Method $method -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -WebSession $WebSession -UseDefaultCredentials -Verbose:$false | Out-Null
}
Write-Verbose "Data sources were updated successfully!"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ function Write-RsRestCatalogItem
.EXAMPLE
Write-RsRestCatalogItem -Path 'c:\reports\monthlyreport.rdl' -RsFolder '/monthlyreports'
Description
-----------
Uploads the report 'monthlyreport.rdl' to folder '/monthlyreports' using v2.0 REST Endpoint to Report Server located at http://localhost/reports/.
.EXAMPLE
Write-RsRestCatalogItem -Path 'c:\reports\monthlyreport.rdl' -RsFolder '/monthlyreports' -RestApiVersion 'v1.0'
Description
-----------
Uploads the report 'monthlyreport.rdl' to folder '/monthlyreports' to v1.0 REST Endpoint to Report Server located at http://localhost/reports/.
.EXAMPLE
Write-RsRestCatalogItem -WebSession $mySession -Path 'c:\reports\monthlyreport.rdl' -RsFolder '/monthlyreports'
Description
-----------
Uploads the report 'monthlyreport.rdl' to folder '/monthlyreports' to v2.0 REST Endpoint to Report Server located at the specified WebSession object.
.EXAMPLE
Write-RsRestCatalogItem -ReportPortalUri 'http://myserver/reports' -Path 'c:\reports\monthlyreport.rdl' -RsFolder '/monthlyreports'
Description
-----------
Uploads the report 'monthlyreport.rdl' to folder '/monthlyreports' using v2.0 REST Endpoint to Report Server located at http://myserver/reports.
Expand Down Expand Up @@ -146,13 +146,13 @@ function Write-RsRestCatalogItem
$itemPath = "$RsFolder/$itemName"
}

Write-Verbose "Reading file content..."
Write-Verbose "Reading file $item content..."
if ($itemType -eq 'DataSource')
{
[xml] $dataSourceXml = Get-Content -Path $EntirePath
if ($item.Extension -eq '.rsds')
{
if ($dataSourceXml -eq $null -or
if ($dataSourceXml -eq $null -or
$dataSourceXml.DataSourceDefinition -eq $null -or
$dataSourceXml.DataSourceDefinition.Extension -eq $null -or
$dataSourceXml.DataSourceDefinition.ConnectString -eq $null)
Expand All @@ -167,7 +167,7 @@ function Write-RsRestCatalogItem
}
elseif ($item.Extension -eq '.rds')
{
if ($dataSourceXml -eq $null -or
if ($dataSourceXml -eq $null -or
$dataSourceXml.RptDataSource -eq $null -or
$dataSourceXml.RptDataSource.Name -eq $null -or
$dataSourceXml.RptDataSource.ConnectionProperties -eq $null -or
Expand All @@ -178,7 +178,7 @@ function Write-RsRestCatalogItem
}

$itemName = $dataSourceXml.RptDataSource.Name
$itemPath = $itemPath.Substring(0, $itemPath.LastIndexOf('/') + 1) + $itemName
$itemPath = $itemPath.Substring(0, $itemPath.LastIndexOf('/') + 1) + $itemName
$enabled = $true
$connectionProperties = $dataSourceXml.RptDataSource.ConnectionProperties
$connectionString = $connectionProperties.ConnectString
Expand Down Expand Up @@ -245,11 +245,11 @@ function Write-RsRestCatalogItem

if ($Credential -ne $null)
{
Invoke-WebRequest -Uri $catalogItemsUri -Method Post -WebSession $WebSession -Body $payloadJson -ContentType "application/json" -Credential $Credential -Verbose:$false | Out-Null
Invoke-WebRequest -Uri $catalogItemsUri -Method Post -WebSession $WebSession -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -Credential $Credential -Verbose:$false | Out-Null
}
else
{
Invoke-WebRequest -Uri $catalogItemsUri -Method Post -WebSession $WebSession -Body $payloadJson -ContentType "application/json" -UseDefaultCredentials -Verbose:$false | Out-Null
Invoke-WebRequest -Uri $catalogItemsUri -Method Post -WebSession $WebSession -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -UseDefaultCredentials -Verbose:$false | Out-Null
}

Write-Verbose "$EntirePath was uploaded to $RsFolder successfully!"
Expand Down Expand Up @@ -279,22 +279,23 @@ function Write-RsRestCatalogItem
$uri = [String]::Format($catalogItemsUpdateUri, $itemId)
if ($Credential -ne $null)
{
Invoke-WebRequest -Uri $uri -Method Put -WebSession $WebSession -Body $payloadJson -ContentType "application/json" -Credential $Credential -Verbose:$false | Out-Null
Invoke-WebRequest -Uri $uri -Method Put -WebSession $WebSession -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -Credential $Credential -Verbose:$false | Out-Null
}
else
{
Invoke-WebRequest -Uri $uri -Method Put -WebSession $WebSession -Body $payloadJson -ContentType "application/json" -UseDefaultCredentials -Verbose:$false | Out-Null
Invoke-WebRequest -Uri $uri -Method Put -WebSession $WebSession -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -UseDefaultCredentials -Verbose:$false | Out-Null
}
Write-Verbose "$EntirePath was uploaded to $RsFolder successfully!"
}
catch
{
throw (New-Object System.Exception("Failed to create catalog item: $($_.Exception.Message)", $_.Exception))
Write-Error (New-Object System.Exception("Failed to create catalog item: $($_.Exception.Message)", $_.Exception))
}
return
}

throw (New-Object System.Exception("Failed to create catalog item: $($_.Exception.Message)", $_.Exception))
else
{
Wrote-Error (New-Object System.Exception("Failed to create catalog item: $($_.Exception.Message)", $_.Exception))
}
}
}
}
Expand Down
Loading

0 comments on commit dcc87f7

Please sign in to comment.