I'm trying to call an API within an Azure Automation Account by using a Powershell Runbook and I'm getting A 405 error when I attempt a GET method and a 499 eroror when I attempt a POST.
$uri = "https://xxxxxxxx-xxxxxxxx-prod-api.azurewebsites.net/api/PortfolioExport?PortfolioBatchDate=9/9/9999"
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-7181039d1ed4"
$clientId = "xxxxxxxxxxxx-xxxx-xxxx-xxxx-5268b21e2920"
$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx~~U7n06yxQlc49"
$resource = "https://xxxxx.com/xxxxx.riskanalysis.app"
$tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$body = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
resource = $resource
audience = $resource # Specify the audience parameter here
}
$response = Invoke-RestMethod -Method Post -Uri $tokenEndpoint -Body $body
$accessToken = $response.access_token
$headers = @{
Authorization = "Bearer $accessToken"
}
Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
This is the error that I receive when attempting to run the above code...
Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed.
At line:26 char:2
+ Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
When attempting to use POST, Here is the message
Invoke-RestMethod : The remote server returned an error: (499).
At line:26 char:2
- Invoke-RestMethod -Method POST -Uri $uri -Headers $headers
-
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
We have a Logic app that fails to run due to the timeout time which is why I'm trying to go the automation account route. This is the logic app JSON, which runs correctly but fails to complete. I tried using this code as reference for calling the API within the Runbook so maybe I didn't do it correctly.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"HTTP": {
"inputs": {
"authentication": {
"audience": "https://xxxxx.com/xxxxx.riskanalysis.app",
"clientId": "xxxxxxxxxxxx-xxxx-xxxx-xxxx-5268b21e2920",
"secret": "xxxxxxxxxxxxxxxxxxxxxxxx~~U7n06yxQlc49",
"tenant": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx-7181039d1ed4",
"type": "ActiveDirectoryOAuth"
},
"method": "POST",
"uri": "https://xxxxxxxx-xxxxxxxx-prod-api.azurewebsites.net/api/PortfolioExport?PortfolioBatchDate=9/9/9999"
},
"limit": {
"timeout": "PT15M"
},
"runAfter": {},
"type": "Http"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"evaluatedRecurrence": {
"frequency": "Month",
"interval": 1,
"startTime": "2022-06-01T06:00:00Z",
"timeZone": "Central Standard Time"
},
"recurrence": {
"frequency": "Month",
"interval": 1,
"startTime": "2022-06-01T06:00:00Z",
"timeZone": "Central Standard Time"
},
"type": "Recurrence"
}
}
},
"parameters": {}
}
First part of the runbook that calls an sql stored procedure:
# Instantiate the connection to the SQL Database
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Data Source=xxxxxxxxxxx.database.windows.net;Initial Catalog=xxxxxxx;Integrated Security=False;User ID=xxxxxxxxxxxxxxxx;Password=xxxxxxxxxxxx;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"
$sqlConnection.Open()
Write-Output "Azure SQL database connection opened"
# Define the SQL command to run
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandTimeout = 0
$sqlCommand.Connection = $sqlConnection
Write-Output "Issuing command to run stored procedure"
# Execute the SQL command
$sqlCommand.CommandText= 'DECLARE @ReportDate datetime; SET @ReportDate = GETDATE(); EXEC Reporting.PortfolioBuild2 @ReportDate'
$result = $sqlCommand.ExecuteNonQuery()
Write-Output "Stored procedure execution completed"
# Close the SQL connection
$sqlConnection.Close()
Write-Output "Run completed"
Write-Output "Run started"
# Instantiate the connection to the SQL Database
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Data Source=xxxxxxxxxxx.database.windows.net;Initial Catalog=xxxxxxx;Integrated Security=False;User ID=xxxxxxxxxxxxxxxx;Password=xxxxxxxxxxxx;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"
$sqlConnection.Open()
Write-Output "Azure SQL database connection opened"
# Define the SQL command to run
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandTimeout = 0
$sqlCommand.Connection = $sqlConnection
Write-Output "Issuing command to run stored procedure"
# Execute the SQL command
$sqlCommand.CommandText= 'DECLARE @ReportDate datetime; SET @ReportDate = GETDATE(); EXEC Reporting.PortfolioBuild2 @ReportDate'
$result = $sqlCommand.ExecuteNonQuery()
Write-Output "Stored procedure execution completed"
# Close the SQL connection
$sqlConnection.Close()
Write-Output "Run completed"
Write-Output "Run started"
# Instantiate the connection to the SQL Database
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Data Source=xxxxxxxxxxx.database.windows.net;Initial Catalog=xxxxxxx;Integrated Security=False;User ID=xxxxxxxxxxxxxxxx;Password=xxxxxxxxxxxx;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"
$sqlConnection.Open()
Write-Output "Azure SQL database connection opened"
# Define the SQL command to run
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandTimeout = 0
$sqlCommand.Connection = $sqlConnection
Write-Output "Issuing command to run stored procedure"
# Execute the SQL command
$sqlCommand.CommandText= 'DECLARE @ReportDate datetime; SET @ReportDate = GETDATE(); EXEC Reporting.PortfolioBuild2 @ReportDate'
$result = $sqlCommand.ExecuteNonQuery()
Write-Output "Stored procedure execution completed"
# Close the SQL connection
$sqlConnection.Close()
Write-Output "Run completed"
Write-Output "Run started"
# Instantiate the connection to the SQL Database
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Data Source=xxxxxxxxxxx.database.windows.net;Initial Catalog=xxxxxxx;Integrated Security=False;User ID=xxxxxxxxxxxxxxxx;Password=xxxxxxxxxxxx;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"
$sqlConnection.Open()
Write-Output "Azure SQL database connection opened"
# Define the SQL command to run
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandTimeout = 0
$sqlCommand.Connection = $sqlConnection
Write-Output "Issuing command to run stored procedure"
# Execute the SQL command
$sqlCommand.CommandText= 'DECLARE @ReportDate datetime; SET @ReportDate = GETDATE(); EXEC Reporting.PortfolioBuild2 @ReportDate'
$result = $sqlCommand.ExecuteNonQuery()
Write-Output "Stored procedure execution completed"
# Close the SQL connection
$sqlConnection.Close()
Write-Output "Run completed"
Logic app code:
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"HTTP": {
"inputs": {
"authentication": {
"audience": "https://xxxxxxxxxxxxxx/xxxxxxxxxxxx.riskanalysis.app",
"clientId": "xxxxxxxxxxxxxxa-a2a5-5268b21e2920",
"secret": "xxxxxxxxxxxxxxweIkcS3qkq7~~U7n06yxQlc49",
"tenant": "xxxxxxxxxxxxxxx-a6a9-7181039d1ed4",
"type": "ActiveDirectoryOAuth"
},
"method": "POST",
"uri": "https://xxxxxxxxxxxxxxxxxxxxx-api.azurewebsites.net/api/PortfolioExport?PortfolioBatchDate=9/9/9999"
},
"limit": {
"timeout": "PT15M"
},
"runAfter": {},
"type": "Http"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"evaluatedRecurrence": {
"frequency": "Month",
"interval": 1,
"startTime": "2022-06-01T06:00:00Z",
"timeZone": "Central Standard Time"
},
"recurrence": {
"frequency": "Month",
"interval": 1,
"startTime": "2022-06-01T06:00:00Z",
"timeZone": "Central Standard Time"
},
"type": "Recurrence"
}
}
},
"parameters": {}
}