0

I have the following code:

#look up the app function name for this resource group
switch -Regex (az resource list -g $RESOURCE_NAME -o table) {
    '^(\w+[.-]*\w+).*\bserverFarms\b' {
      $Matches[1]; break
    }
  }
$FUNCTION_APP = $Matches[1]
echo $Matches.Count
echo $Matches[0]
echo $Matches[1]

The az resource list command returns the follow output:

> az resource list -g $AZ_RESOURCE_GROUP_NAME -o table
Name                   ResourceGroup    Location    Type                               
----------------------  ---------------  ----------  --------------------------------- 
jf-aa-ext-app-12345      jfFuncs         eastus      microsoft.insights/components
omstorage                jfFuncs         eastus      Microsoft.Storage/storageAccounts
jf-aa-ext-app-12345      jfFuncs         eastus      Microsoft.Web/serverFarms
jf-aa-ext-app-12345     jfFuncs         eastus      Microsoft.Web/sites

Trying to extract "jf-aa-ext-app-12345" from the line that has "serverFarms". But it's only returning "jf-aa" as a match.

10
  • Does ^(\S*).*\bserverFarms\b give the results you need? Commented Jun 3, 2021 at 18:28
  • In the question you asked and got that code multiple people recommended using the Azure module to get this info rather than parsing cli output. Why don't you use the module? Commented Jun 3, 2021 at 18:30
  • @TheMadTechnician because this code ... ultimately will need to run in a docker container where i may not have the option to install azure module ... i know it will have the cli... Commented Jun 3, 2021 at 18:33
  • I found the regex i need. '^([\w+.-]*).*\bserverFarms\b' Commented Jun 3, 2021 at 18:33
  • 2
    az has an --output json option that would be easier to parse with ConvertFrom-Json - no regex required. From there, you can just where-object your way to victory... Commented Jun 3, 2021 at 19:04

1 Answer 1

2

EDIT

Adding this edit at the top because it is clearly the best answer, I was not aware the Azure CLI could output json, thanks mclayton.

# Some alternatives using "-o json"

az resource list -g $RESOURCE_NAME -o json | ConvertFrom-Json |
Where-Object {$_.Type -match 'serverFarms'} | Select -Expand Name

# OR

$resGroup = az resource list -g $RESOURCE_NAME -o json | ConvertFrom-Json
$resGroup.where({$_.Type -match 'serverFarms'}).Name

I propose this solution to your problem, of course, as in yesterday's comments Ash recommended using the Az Module which is definetly recommended. An alternative to parse this output which will be reusable for future cases, is to convert it to an object which can be easily manipulated (this is what TheMadTechnician proposed which in my opinion is much better than using a particular regex to get the result you want).

Note: I'm splitting on '\r\n' because I'm using a string copy pasted from your question. In your case, simply pipping the result of az resource... to Select-String should work fine.

$output = @'
Name                   ResourceGroup    Location    Type                               
----------------------  ---------------  ----------  --------------------------------- 
jf-aa-ext-app-12345      jfFuncs         eastus      microsoft.insights/components
omstorage                jfFuncs         eastus      Microsoft.Storage/storageAccounts
jf-aa-ext-app-12345      jfFuncs         eastus      Microsoft.Web/serverFarms
jf-aa-ext-app-12345      jfFuncs         eastus      Microsoft.Web/sites
'@ -split '\r\n' | Select-String -Pattern '^[^-]'

$parsedOutput = $output -replace '\s{2,}',',' | ConvertFrom-Csv

Output:

PS /> $parsedOutput

Name                ResourceGroup Location Type                             
----                ------------- -------- ----                             
jf-aa-ext-app-12345 jfFuncs       eastus   microsoft.insights/components    
omstorage           jfFuncs       eastus   Microsoft.Storage/storageAccounts
jf-aa-ext-app-12345 jfFuncs       eastus   Microsoft.Web/serverFarms        
jf-aa-ext-app-12345 jfFuncs       eastus   Microsoft.Web/sites              

PS /> $parsedOutput | Get-Member -MemberType NoteProperty

   TypeName: System.Management.Automation.PSCustomObject

Name          MemberType   Definition                               
----          ----------   ----------                               
Location      NoteProperty string Location=eastus                   
Name          NoteProperty string Name=jf-aa-ext-app-12345          
ResourceGroup NoteProperty string ResourceGroup=jfFuncs             
Type          NoteProperty string Type=microsoft.insights/components

The result you want:

PS /> $parsedOutput.where({$_.Type -match 'serverFarms'}).Name
jf-aa-ext-app-12345
Sign up to request clarification or add additional context in comments.

4 Comments

i appreciate this. but ultimately, i have to make this work in a dockerized environment where I may not have the option to just install the azure modules. this is why i've been continuing with the tools i know will be available. but thank you
@dot yup, which is why i'm showing you how you can parse the output using what you already have.
I ended up just using mclaytons suggestion to output in JSON format. If you can add that here to your answer, i'll accept! Thank so much though the help!
@dot done, I added it at the top because it is clearly the best one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.