|
| 1 | + |
| 2 | +# ---------------------------------------------------------------------------------- |
| 3 | +# |
| 4 | +# Copyright Microsoft Corporation |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ---------------------------------------------------------------------------------- |
| 15 | + |
| 16 | +<# |
| 17 | +.Synopsis |
| 18 | +Query the usage data for scope defined. |
| 19 | +.Description |
| 20 | +Query the usage data for scope defined. |
| 21 | +#> |
| 22 | +function Invoke-AzCostManagementQuery { |
| 23 | +[OutputType([Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.IQueryResult])] |
| 24 | +[CmdletBinding(DefaultParameterSetName='UsageExpanded', PositionalBinding=$false, SupportsShouldProcess, ConfirmImpact='Medium')] |
| 25 | +param( |
| 26 | + [Parameter(ParameterSetName='UsageExpanded', Mandatory, HelpMessage="This includes 'subscriptions/{subscriptionId}/' for subscription scope, 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}' for resourceGroup scope, 'providers/Microsoft.Billing/billingAccounts/{billingAccountId}' for Billing Account scope and 'providers/Microsoft.Billing/billingAccounts/{billingAccountId}/departments/{departmentId}' for Department scope, 'providers/Microsoft.Billing/billingAccounts/{billingAccountId}/enrollmentAccounts/{enrollmentAccountId}' for EnrollmentAccount scope, 'providers/Microsoft.Management/managementGroups/{managementGroupId} for Management Group scope, 'providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingProfiles/{billingProfileId}' for billingProfile scope, 'providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingProfiles/{billingProfileId}/invoiceSections/{invoiceSectionId}' for invoiceSection scope, and 'providers/Microsoft.Billing/billingAccounts/{billingAccountId}/customers/{customerId}' specific for partners.")] |
| 27 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Path')] |
| 28 | + [System.String] |
| 29 | + # The scope associated with query and export operations. |
| 30 | + # This includes '/subscriptions/{subscriptionId}/' for subscription scope, '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}' for resourceGroup scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}' for Billing Account scope and '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/departments/{departmentId}' for Department scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/enrollmentAccounts/{enrollmentAccountId}' for EnrollmentAccount scope, '/providers/Microsoft.Management/managementGroups/{managementGroupId} for Management Group scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingProfiles/{billingProfileId}' for billingProfile scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingProfiles/{billingProfileId}/invoiceSections/{invoiceSectionId}' for invoiceSection scope, and '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/customers/{customerId}' specific for partners. |
| 31 | + ${Scope}, |
| 32 | + |
| 33 | + [Parameter(ParameterSetName='UsageExpanded1', Mandatory, HelpMessage="This can be '{externalSubscriptionId}' for linked account or '{externalBillingAccountId}' for consolidated account used with dimension/query operations.")] |
| 34 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Path')] |
| 35 | + [System.String] |
| 36 | + # This can be '{externalSubscriptionId}' for linked account or '{externalBillingAccountId}' for consolidated account used with dimension/query operations. |
| 37 | + ${ExternalCloudProviderId}, |
| 38 | + |
| 39 | + [Parameter(ParameterSetName='UsageExpanded1', Mandatory, HelpMessage="The external cloud provider type associated with dimension/query operations.")] |
| 40 | + [ArgumentCompleter([Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.ExternalCloudProviderType])] |
| 41 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Path')] |
| 42 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.ExternalCloudProviderType] |
| 43 | + # The external cloud provider type associated with dimension/query operations. |
| 44 | + # This includes 'externalSubscriptions' for linked account and 'externalBillingAccounts' for consolidated account. |
| 45 | + ${ExternalCloudProviderType}, |
| 46 | + |
| 47 | + [Parameter(ParameterSetName='UsageExpanded', Mandatory, HelpMessage="The time frame for pulling data for the query.")] |
| 48 | + [Parameter(ParameterSetName='UsageExpanded1', Mandatory, HelpMessage="The time frame for pulling data for the query.")] |
| 49 | + [ArgumentCompleter([Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.TimeframeType])] |
| 50 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 51 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.TimeframeType] |
| 52 | + # The time frame for pulling data for the query. |
| 53 | + # If custom, then a specific time period must be provided. |
| 54 | + ${Timeframe}, |
| 55 | + |
| 56 | + [Parameter(ParameterSetName='UsageExpanded', Mandatory, HelpMessage="The type of the query.")] |
| 57 | + [Parameter(ParameterSetName='UsageExpanded1', Mandatory, HelpMessage="The type of the query.")] |
| 58 | + [ArgumentCompleter([Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.ExportType])] |
| 59 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 60 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.ExportType] |
| 61 | + # The type of the query. |
| 62 | + ${Type}, |
| 63 | + |
| 64 | + [Parameter(ParameterSetName='UsageExpanded', HelpMessage="Array of column names to be included in the query.")] |
| 65 | + [Parameter(ParameterSetName='UsageExpanded1', HelpMessage="Array of column names to be included in the query.")] |
| 66 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 67 | + [System.String[]] |
| 68 | + # Array of column names to be included in the query. |
| 69 | + # Any valid query column name is allowed. |
| 70 | + # If not provided, then query includes all columns. |
| 71 | + ${ConfigurationColumn}, |
| 72 | + |
| 73 | + [Parameter(ParameterSetName='UsageExpanded', HelpMessage="Dictionary of aggregation expression to use in the query.")] |
| 74 | + [Parameter(ParameterSetName='UsageExpanded1', HelpMessage="Dictionary of aggregation expression to use in the query.")] |
| 75 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 76 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Runtime.Info(PossibleTypes=([Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.IQueryDatasetAggregation]))] |
| 77 | + [System.Collections.Hashtable] |
| 78 | + # Dictionary of aggregation expression to use in the query. |
| 79 | + # The key of each item in the dictionary is the alias for the aggregated column. |
| 80 | + # Query can have up to 2 aggregation clauses. |
| 81 | + ${DatasetAggregation}, |
| 82 | + |
| 83 | + [Parameter(ParameterSetName='UsageExpanded', HelpMessage="Has filter expression to use in the query.")] |
| 84 | + [Parameter(ParameterSetName='UsageExpanded1', HelpMessage="Has filter expression to use in the query.")] |
| 85 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 86 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.IQueryFilter] |
| 87 | + # Has filter expression to use in the query. |
| 88 | + # To construct, see NOTES section for DATASETFILTER properties and create a hash table. |
| 89 | + ${DatasetFilter}, |
| 90 | + |
| 91 | + [Parameter(ParameterSetName='UsageExpanded', HelpMessage="The granularity of rows in the query.")] |
| 92 | + [Parameter(ParameterSetName='UsageExpanded1', HelpMessage="The granularity of rows in the query.")] |
| 93 | + [ArgumentCompleter([Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.GranularityType])] |
| 94 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 95 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Support.GranularityType] |
| 96 | + # The granularity of rows in the query. |
| 97 | + ${DatasetGranularity}, |
| 98 | + |
| 99 | + [Parameter(ParameterSetName='UsageExpanded', HelpMessage='Array of group by expression to use in the query.')] |
| 100 | + [Parameter(ParameterSetName='UsageExpanded1', HelpMessage="Array of group by expression to use in the query.")] |
| 101 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 102 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.IQueryGrouping[]] |
| 103 | + # Array of group by expression to use in the query. |
| 104 | + # Query can have up to 2 group by clauses. |
| 105 | + # To construct, see NOTES section for DATASETGROUPING properties and create a hash table. |
| 106 | + ${DatasetGrouping}, |
| 107 | + |
| 108 | + [Parameter(ParameterSetName='UsageExpanded', HelpMessage="The start date to pull data from.")] |
| 109 | + [Parameter(ParameterSetName='UsageExpanded1', HelpMessage="The start date to pull data from.")] |
| 110 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 111 | + [System.DateTime] |
| 112 | + # The start date to pull data from. |
| 113 | + ${TimePeriodFrom}, |
| 114 | + |
| 115 | + [Parameter(ParameterSetName='UsageExpanded', HelpMessage="The end date to pull data to.")] |
| 116 | + [Parameter(ParameterSetName='UsageExpanded1', HelpMessage="The end date to pull data to.")] |
| 117 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Body')] |
| 118 | + [System.DateTime] |
| 119 | + # The end date to pull data to. |
| 120 | + ${TimePeriodTo}, |
| 121 | + |
| 122 | + [Parameter()] |
| 123 | + [Alias('AzureRMContext', 'AzureCredential')] |
| 124 | + [ValidateNotNull()] |
| 125 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Azure')] |
| 126 | + [System.Management.Automation.PSObject] |
| 127 | + # The credentials, account, tenant, and subscription used for communication with Azure. |
| 128 | + ${DefaultProfile}, |
| 129 | + |
| 130 | + [Parameter(DontShow)] |
| 131 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Runtime')] |
| 132 | + [System.Management.Automation.SwitchParameter] |
| 133 | + # Wait for .NET debugger to attach |
| 134 | + ${Break}, |
| 135 | + |
| 136 | + [Parameter(DontShow)] |
| 137 | + [ValidateNotNull()] |
| 138 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Runtime')] |
| 139 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Runtime.SendAsyncStep[]] |
| 140 | + # SendAsync Pipeline Steps to be appended to the front of the pipeline |
| 141 | + ${HttpPipelineAppend}, |
| 142 | + |
| 143 | + [Parameter(DontShow)] |
| 144 | + [ValidateNotNull()] |
| 145 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Runtime')] |
| 146 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Runtime.SendAsyncStep[]] |
| 147 | + # SendAsync Pipeline Steps to be prepended to the front of the pipeline |
| 148 | + ${HttpPipelinePrepend}, |
| 149 | + |
| 150 | + [Parameter(DontShow)] |
| 151 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Runtime')] |
| 152 | + [System.Uri] |
| 153 | + # The URI for the proxy server to use |
| 154 | + ${Proxy}, |
| 155 | + |
| 156 | + [Parameter(DontShow)] |
| 157 | + [ValidateNotNull()] |
| 158 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Runtime')] |
| 159 | + [System.Management.Automation.PSCredential] |
| 160 | + # Credentials for a proxy server to use for the remote call |
| 161 | + ${ProxyCredential}, |
| 162 | + |
| 163 | + [Parameter(DontShow)] |
| 164 | + [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Category('Runtime')] |
| 165 | + [System.Management.Automation.SwitchParameter] |
| 166 | + # Use the default credentials for the proxy |
| 167 | + ${ProxyUseDefaultCredentials} |
| 168 | +) |
| 169 | + |
| 170 | + process { |
| 171 | + $ApiVersion = '2019-11-01' |
| 172 | + $URL = '' |
| 173 | + if ($PSBoundParameters.ContainsKey('ExternalCloudProviderType')) { |
| 174 | + $URL = [System.Text.RegularExpressions.Regex]::Replace( |
| 175 | + "providers/Microsoft.CostManagement/$ExternalCloudProviderType/$ExternalCloudProviderId/query?api-version=$ApiVersion" ,"\\?&*$|&*$|(\\?)&+|(&)&+","$1$2") |
| 176 | + } else { |
| 177 | + $URL = [System.Text.RegularExpressions.Regex]::Replace( |
| 178 | + "$Scope/providers/Microsoft.CostManagement/query?api-version=$ApiVersion", "\\?&*$|&*$|(\\?)&+|(&)&+","$1$2") |
| 179 | + } |
| 180 | + |
| 181 | + $Request = [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.QueryDefinition]::New() |
| 182 | + if ($PSBoundParameters.ContainsKey('ConfigurationColumn')) { |
| 183 | + $Request.ConfigurationColumn = $ConfigurationColumn |
| 184 | + } |
| 185 | + if ($PSBoundParameters.ContainsKey('DatasetAggregation')) { |
| 186 | + $Request.DatasetAggregation = $DatasetAggregation |
| 187 | + } |
| 188 | + if ($PSBoundParameters.ContainsKey('DatasetFilter')) { |
| 189 | + $Request.DatasetFilter = $DatasetFilter |
| 190 | + } |
| 191 | + if ($PSBoundParameters.ContainsKey('DatasetGranularity')) { |
| 192 | + $Request.DatasetGranularity = $DatasetGranularity |
| 193 | + } |
| 194 | + if ($PSBoundParameters.ContainsKey('DatasetGrouping')) { |
| 195 | + $Request.DatasetGrouping = $DatasetGrouping |
| 196 | + } |
| 197 | + if ($PSBoundParameters.ContainsKey('TimePeriodFrom')) { |
| 198 | + $Request.TimePeriodFrom = $TimePeriodFrom |
| 199 | + } |
| 200 | + if ($PSBoundParameters.ContainsKey('TimePeriodTo')) { |
| 201 | + $Request.TimePeriodTo = $TimePeriodTo |
| 202 | + } |
| 203 | + if ($PSBoundParameters.ContainsKey('Timeframe')) { |
| 204 | + $Request.Timeframe = $Timeframe |
| 205 | + } |
| 206 | + if ($PSBoundParameters.ContainsKey('Type')) { |
| 207 | + $Request.Type = $Type |
| 208 | + } |
| 209 | + $Result = [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.QueryResult]::New() |
| 210 | + $SkipToken = $null |
| 211 | + $RowList = New-Object System.Collections.Generic.List[System.Collections.Generic.List[string]] |
| 212 | + while ($true) { |
| 213 | + $Response = Invoke-AzCostManagementUsageQueryInternal -URL $URL -SkipToken $SkipToken -Payload $Request.ToJsonString() -Scope $Scope |
| 214 | + $Result.Column = $Response.Column |
| 215 | + foreach ($Row in $Response.Row) { |
| 216 | + $RowList.Add($Row) |
| 217 | + } |
| 218 | + if ($null -ne $Response.NextLink -and '' -ne $Response.nextlink) |
| 219 | + { |
| 220 | + $SkipToken = $Response.nextLink |
| 221 | + } else { |
| 222 | + break |
| 223 | + } |
| 224 | + } |
| 225 | + $Result.Row = $RowList |
| 226 | + return $Result |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +function Invoke-AzCostManagementUsageQueryInternal { |
| 231 | + [OutputType([Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.IQueryResult])] |
| 232 | + param( |
| 233 | + [System.String] |
| 234 | + ${URL}, |
| 235 | + [System.String] |
| 236 | + ${SkipToken}, |
| 237 | + [System.String] |
| 238 | + ${Payload}, |
| 239 | + [System.String] |
| 240 | + ${Scope} |
| 241 | + ) |
| 242 | + process { |
| 243 | + if ($null -ne $SkipToken -and $SkipToken -ne '') { |
| 244 | + $URL = "$URL&$SkipToken" |
| 245 | + } |
| 246 | + $ResponseContent = (Invoke-AzRest -Path $URL -Payload $Payload -Method POST).Content | ConvertFrom-Json |
| 247 | + |
| 248 | + $Result = [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.QueryResult]::New() |
| 249 | + if ($null -ne $ResponseContent.Properties.NextLink) |
| 250 | + { |
| 251 | + $Result.NextLink = $ResponseContent.Properties.NextLink.split('&')[1] |
| 252 | + } |
| 253 | + |
| 254 | + $ColumnList = New-Object System.Collections.Generic.List[Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.IQueryColumn] |
| 255 | + foreach ($Column in $ResponseContent.Properties.Columns) { |
| 256 | + Write-Host $Column.ToString() |
| 257 | + $QueryColumn = [Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20200601.QueryColumn]::New() |
| 258 | + $QueryColumn.Name = $Column.Name |
| 259 | + $QueryColumn.Type = $Column.Type |
| 260 | + $ColumnList.Add($QueryColumn) |
| 261 | + } |
| 262 | + $Result.Column = $ColumnList |
| 263 | + |
| 264 | + $RowList = New-Object System.Collections.Generic.List[System.Collections.Generic.List[string]] |
| 265 | + foreach ($Row in $ResponseContent.Properties.Rows) { |
| 266 | + $QueryRow = New-Object System.Collections.Generic.List[string] |
| 267 | + foreach ($Item in $Row) { |
| 268 | + $QueryRow.Add($Item.ToString()) |
| 269 | + } |
| 270 | + $RowList.Add($QueryRow) |
| 271 | + } |
| 272 | + $Result.Row = $RowList |
| 273 | + |
| 274 | + return $Result |
| 275 | + } |
| 276 | +} |
0 commit comments