Created
April 4, 2023 04:45
-
-
Save krymtkts/677efd219f12d3ca5aed1cdd69c6052f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory, | |
Position = 0, | |
ValueFromPipeline, | |
ValueFromPipelineByPropertyName)] | |
[string] | |
$WorkGroup, | |
[Parameter(Mandatory, | |
Position = 1, | |
ValueFromPipeline, | |
ValueFromPipelineByPropertyName)] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$QueryPath | |
) | |
$ErrorActionPreference = 'Stop' | |
$Query = $(Get-Content $QueryPath -Raw) | |
if ($Query.Contains('?')) { | |
$ParameterCount = ($Query -split '\?').Length - 1 | |
$Parameters = @() | |
for ($i = 0; $i -lt $ParameterCount; $i++) { | |
$Parameters += Read-Host "Enter parameter $($i++)" | |
} | |
$QueryExecutionId = Start-ATHQueryExecution -WorkGroup $WorkGroup -QueryString $Query -ExecutionParameter $Parameters | |
} | |
else { | |
$QueryExecutionId = Start-ATHQueryExecution -WorkGroup $WorkGroup -QueryString $(Get-Content $QueryPath -Raw) | |
} | |
Write-Host "wait for result of $QueryExecutionId" | |
do { | |
$Result = Get-ATHQueryExecution -QueryExecutionId $QueryExecutionId | |
if ($Result.Status.State -eq 'FAILED') { | |
throw "$QueryExecutionId is failed. $($Result.Status.StateChangeReason)" | |
} | |
Start-Sleep -Seconds 3 | |
} until ($Result.Status.State -eq 'SUCCEEDED') | |
$BucketName, $Key = ($Result.ResultConfiguration.OutputLocation -replace 's3://', '') -split '/', 2 | |
$LocalFile = "${PSScriptRoot}/$(($Key -split '/')[-1])" | |
Read-S3Object -BucketName $BucketName -Key $Key -File $LocalFile | Out-Null | |
Get-Content $LocalFile | ConvertFrom-Csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment