Skip to content

Commit

Permalink
Added verbose output
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-Geisler committed Dec 22, 2024
1 parent d52e39c commit 9232853
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
13 changes: 11 additions & 2 deletions powerrti/Public/Connect-RTIAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ function Connect-RtiAccount {
The TenantId of the Azure Active Directory tenant you want to connect to
and in which your Fabric Capacity is.
.NOTES
Revsion History:
- 2024-12-22 - FGE: Added Verbose Output
.EXAMPLE
Connect-RTIAccount `
-TenantID '12345678-1234-1234-1234-123456789012'
Expand All @@ -32,16 +38,19 @@ begin {
}

process {
Write-Verbose "Connect to Azure Account"
Connect-AzAccount `
-TenantId $TenantId | `
Out-Null

# Get authentication token
Write-Verbose "Get authentication token"
$RTISession.FabricToken = (Get-AzAccessToken `
-ResourceUrl $RTISession.BaseFabricUrl).Token
Write-Verbose "Token: $($RTISession.FabricToken)"

# Setup headers for API calls
Write-Verbose "Setup headers for API calls"
$RTISession.HeaderParams = @{'Authorization'="Bearer {0}" -f $RTISession.FabricToken}
Write-Verbose "HeaderParams: $($RTISession.HeaderParams)"
}

end {
Expand Down
36 changes: 27 additions & 9 deletions powerrti/Public/Invoke-RtiKQLCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function Invoke-RtiKQLCommand {
This example will create a table named 'MyTable' with a column named 'MyColumn' in
the KQLDatabase 'MyKQLDatabase'.
.NOTES
Revsion History:
- 2024-12-22 - FGE: Added Verbose Output
#>

[CmdletBinding()]
Expand All @@ -51,60 +57,72 @@ function Invoke-RtiKQLCommand {

begin {

# Check if session is established - if not throw error
Write-Verbose "Check if session is established - if not throw error"
if ($null -eq $RTISession.headerParams) {
throw "No session established to Fabric Real-Time Intelligence. Please run Connect-RTISession"
}

Write-Verbose "Check if KQLDatabaseName and KQLDatabaseId are used together"
if ($PSBoundParameters.ContainsKey("KQLDatabaseName") -and $PSBoundParameters.ContainsKey("KQLDatabaseId")) {
throw "Parameters KQLDatabaseName and KQLDatabaseId cannot be used together"
}

# FGE: Get Kusto Database
Write-Verbose "Get Kusto Database"
if ($PSBoundParameters.ContainsKey("KQLDatabaseName")) {
Write-Verbose "Getting Kusto Database by Name: $KQLDatabaseName"
$kustDB = Get-RtiKQLDatabase `
-WorkspaceId $WorkspaceId `
-KQLDatabaseName $KQLDatabaseName
}

if ($PSBoundParameters.ContainsKey("KQLDatabaseId")) {
Write-Warning "Getting Kusto Database by Id"
Write-Verbose "Getting Kusto Database by Id: $KQLDatabaseId"
$kustDB = Get-RtiKQLDatabase `
-WorkspaceId $WorkspaceId `
-KQLDatabaseId $KQLDatabaseId
}

Write-Verbose "Check if Kusto Database was found"
if ($null -eq $kustDB) {
throw "Kusto Database not found"
}
}

process {

# Authenticate against Kusto
Write-Verbose "Authenticate against Kusto"
$kustoToken = (Get-AzAccessToken `
-ResourceUrl $RTISession.KustoURL).Token

$headerParams = @{'Authorization'="Bearer {0}" -f $kustoToken}

# FGE: Generate the query API URL
Write-Verbose "Generate the query API URL"
$queryAPI = "$($kustDB.queryServiceUri)/v1/rest/mgmt"

$KQLCommand = $KQLCommand | Out-String

# FGE: It is crucial to have the .execute database script <| in the beginning, otherwise
# the Kusto API will not execute the script.
Write-Verbose "It is crucial to have the .execute database script <| in the beginning, otherwise the Kusto API will not execute the script."
if (-not ($KQLCommand -match "\.execute database script <\|")) {
$KQLCommand = ".execute database script <| $KQLCommand"
}

# Create body of the request
Write-Verbose "The KQL-Command is: $KQLCommand"

Write-Verbose "Create body of the request"
$body = @{
'csl' = $KQLCommand;
'db'= $kustDB.displayName
} | ConvertTo-Json -Depth 1

# Call Kusto API to run entities creation script
Write-Verbose "Calling Query API"
Write-Verbose "-----------------"
Write-Verbose "Sending the following values to the Query API:"
Write-Verbose "Headers: $($Rtisession.headerParams | Format-List | Out-String)"
Write-Verbose "Method: POST"
Write-Verbose "URI: $queryAPI"
Write-Verbose "Body of request: $body"
Write-Verbose "ContentType: application/json"

Invoke-RestMethod `
-Headers $headerParams `
-Method POST `
Expand Down

0 comments on commit 9232853

Please sign in to comment.