0

I'm trying to get the usage details for an azure subscription in JSON format through powershell and rest API(https://consumption.azure.com/v3/enrollments/{enrollmentNumber}/usagedetailsbycustomdate?startTime=2017-01-01&endTime=2017-01-10)

My code is working when I use actual values of date as parameter. But when I use variables to get the current and previous day's date it doesn't work. It is throwing an error saying the value is less than 2014-05-01

 $endDate = Get-Date
 $startDate = $endDate.AddDays(-1)`enter code here`
 $out = $startDate.ToString('yyyy-MM-dd')
 $out1 = $endDate.ToString('yyyy-MM-dd')
 $header = @{"authorization"="bearer $key"}
 $uri = "https://consumption.azure.com/v3/enrollments/$enrollment/usagedetailsbycustomdate?startTime=$out&endTime=$out1"

I'm looking to get the last two days of usage details through this code. Any help or advise is appreciated

  • can you post the rest uri as formatted via the code? Any extra formatting that differs from the manual call? – Mike Oryszak May 30 '19 at 14:52
  • $response = Invoke-WebRequest $uri -Headers $header -ErrorAction Stop – Guhan Palanisamy May 30 '19 at 15:10
  • looking specifically for the output of $uri to compare it against the string above. – Mike Oryszak May 30 '19 at 15:11
  • The rest uri is not formatted further. The weird thing is if format the date in the same way and pass it to a different for billing period (https://consumption.azure.com/v3/enrollments/{enrollmentNumber}/billingPeriods/{billingPeriod}/usagedetails) it works. Do i have to format the date in a different way ? – Guhan Palanisamy May 30 '19 at 15:13
  • ```'{0}{1}{2}' -f $uri,$enrolments,"usagedetailsbycustomdate?startTime=",$out1,$out2``` – lloyd May 30 '19 at 15:14
  • If i give the variables with in double quotes in URI I get the following output "Invoke-WebRequest : {"error":{"code":"400","message":"Parameter: startTime. Value is less than 2014-05-01"}}" if not I'm not getting any error but the webrequest is not being invoked – Guhan Palanisamy May 30 '19 at 15:16
  • Sounds like the date format is good then, that was what I was trying to validate. Are sure you have usage data for the date period provided? – Mike Oryszak May 30 '19 at 15:23
  • Yes I do Mike. Lloyd would you care to elaborate more on your answer. Thanks to you both – Guhan Palanisamy May 30 '19 at 15:38

0 Answers0