Azure / Azure/azure-cli

Calling az rest --method POST with JSON body file in PowerShell not working in DevOps Pipeline

Open
#19,948 1 comment 0 reactions 1 assignee Claimed by @jiasli View on GitHub
feature-request rest
Dominant language
Python
Stars
4.6k
Forks
3.5k
Avg merge
3d 2h
Merged PRs (30d)
60

Description

Not sure if this is just a question because of what am i doing wrong, or there is a bug here.

My code is grabbing results from Azure Log Analytics with a complicated query, and I'm using az rest. From within a PowerShell 7 window I'm able to make the call and retrieve results. However when I call the PowerShell script from within an Azure Pipeline I receive an error when it's reading in the body.

This is my query json file. It is reading in the AADServicePrincipalSignInLogs and looking for Apps that are signing in with ADAL.
```json
{
"query": "AADServicePrincipalSignInLogs\r\n |mv-expand ParsedFields=parse_json(AuthenticationProcessingDetails)\r\n |extend Key = ParsedFields.key\r\n |extend Value = ParsedFields.value\r\n | where Key contains \"AD App\"\r\n | where Value contains \"ADAL\"\r\n |extend details =split(Value, \" \")\r\n |extend Version = strcat(details[3],\" \",details[4])\r\n |project TimeGenerated, ['App Name'] = ServicePrincipalName, ['App Id'] = AppId, ['ADAL Version'] = Version\r\n |summarize ['Sign-in Count'] = dcount(TimeGenerated, 4) by ['App Name'], ['App Id'], ['ADAL Version'];",
"timespan": "P31D"
}
```
Below is the call using `az rest`. The $WorkspaceResourceId looks like `/subscriptions//resourcegroups//providers/microsoft.operationalinsights/workspaces/`

```ps1
az rest --method POST --url "https://management.azure.com$WorkspaceResourceId/api/query?api-version=2017-01-01-preview"--body `@adalquery.json
```
![image](https://user-images.githubusercontent.com/6736509/138115103-fd1a9e8d-749c-4b3e-a4d8-62aba0dfbd03.png)

The exact same code running through a pipeline
```text
2021-10-20T14:20:22.6899388Z [command]C:\Windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" --version"
2021-10-20T14:20:28.9711113Z WARNING: You have 2 updates available. Consider updating your CLI installation with 'az upgrade'
2021-10-20T14:20:28.9712365Z azure-cli 2.28.0 *
2021-10-20T14:20:28.9902230Z
2021-10-20T14:20:28.9903079Z
2021-10-20T14:20:28.9935086Z Please let us know how we are doing: https://aka.ms/azureclihats
2021-10-20T14:20:28.9935879Z core 2.28.0 *
2021-10-20T14:20:29.0000573Z and let us know if you're interested in trying out our newest features: https://aka.ms/CLIUXstudy
2021-10-20T14:20:29.0001477Z telemetry 1.0.6
2021-10-20T14:20:29.0110459Z
2021-10-20T14:20:29.0135748Z Extensions:
2021-10-20T14:20:29.0703479Z azure-devops 0.20.0
....
2021-10-20T14:20:43.7956799Z [command]C:\Windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" account set --subscription ae68c399-6a9a-467b-9192-61e294da12e1"
2021-10-20T14:20:44.1843402Z [command]"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\azureclitaskscript1634739622650.ps1'"
2021-10-20T14:20:44.8151625Z Getting Query...
2021-10-20T14:20:45.0358591Z https://management.azure.com/subscriptions/***/resourcegroups/***/providers/microsoft.operationalinsights/workspaces/***/api/query?api-version=2017-01-01-preview
2021-10-20T14:20:53.1872813Z ERROR: Bad Request({"error":{"message":"The request had some invalid properties","code":"BadArgumentError","correlationId":"9479b99a-698b-486c-9fdb-b6d79f97f6d9","details":[{"code":"InvalidJsonBody","message":"Unexpected token @ in JSON at position 0","target":null}],"innererror":{"code":"QueryValidationError","message":"Failed parsing the query"}}})
```

I guess this is because pipelines create a temporary Powershell file `D:\a\_temp\azureclitaskscript1634739622650.ps1`

My YAML file is below. As you can see it's using Windows.
```yml
pool:
vmImage: "windows-latest"

stages:
- stage: stage1
displayName: "stage1"
variables:
- name: workspaceresourceid
value: "***"
jobs:
- job: getadalapps
displayName: Get ADAL Applications
steps:
- checkout: self
persistCredentials: true
displayName: "Self Checkout"

- task: AzureCLI@2
name: "getadalapps"
displayName: "Azure CLI ./get-adalapplications.ps1"
inputs:
azureSubscription: "***"
scriptType: pscore
scriptPath: '$(System.DefaultWorkingDirectory)\Pipeline\get-adalapplications.ps1'
arguments: "-WorkspaceResourceId:'${{variables.workspaceresourceid}}'"
```

Has anyone had this problem, and got around it (without writing the script in BASH).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.