-3

Example: Get all the details of App service with Pricing Tier and App type

Below given is power shell script to export Web app details but i am unable to fetch Pricing tier and App type of App service.

#Provide the subscription Id where the Webapps ,function apps resides
$subscriptionId = "XXXXXXXXXXXXXXXXXXXXXXX"
$currentTime=$(get-date).ToString("yyyyMMddHHmmss");    
$outputFilePath=".\AzureWebAppsReport-"+$currentTime+".csv"  

Set-AzureRmContext $subscriptionId
$result=@()   

# Get all the webapps  
$webapps =Get-AzureRMWebApp 
$AzSubscription = Get-AzureRmSubscription -SubscriptionId $subscriptionId
$rmresources =  Get-AzureRmResource | ?{ $_.Sku -NE $null}

# Loop through the webapps  
foreach($webapp in $webapps)  
{  
 $info = "" | Select Name,State,LOCATION,ResourceGroup,SUBSCRIPTION,AppServicePlan,PricingTier

foreach($rmResource in $rmresources) { 
        if($webapp.ResourceGroup -eq $rmResource.ResourceGroupName) {
            $info.PricingTier = $rmResource.Sku
            } 
        } 

        $info.Name = $webapp.Name 
        $info.State = $webapp.State 
        $info.LOCATION = $webapp.LOCATION
        $info.ResourceGroup = $webapp.ResourceGroup
        $info.SUBSCRIPTION = $AzSubscription.Name
        $info.AppServicePlan=$webapp.ServerFarmId

    #Add the object with above properties to the Array  
    $result+=$info 
}
$result | ft Name,State,LOCATION,ResourceGroup,SUBSCRIPTION,AppServicePlan,PricingTier

#Export the result Array to CSV file  
$result | Export-CSV $outputFilePath -NoTypeInformation 

enter image description here

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
RahulTripathi07
  • 85
  • 2
  • 10

1 Answers1

1

You could try my sample below, it works fine on my side.

$AzSubscription = Get-AzSubscription -SubscriptionId "<subscription-id>"
$result=@()  
$webapps = Get-AzWebApp
foreach($webapp in $webapps){
    $Tier = (Get-AzResource -ResourceId $webapp.ServerFarmId).Sku.Tier
    $obj = [PSCustomObject]@{
        Name = $webapp.Name
        State = $webapp.State
        Location = $webapp.Location
        PricingTier = $Tier
        AppType = $webapp.Kind
        ResourceGroup = $webapp.ResourceGroup
        Subscription = $AzSubscription.Name
    }
    $result += $obj
}
$result | Export-Csv -Path "C:\Users\joyw\Desktop\webapps.csv" -NoTypeInformation

The .csv file will be like below:

enter image description here

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Getting app type blank – RahulTripathi07 Apr 03 '20 at 09:49
  • @RahulTripathi07 Well, maybe there are differences in `Get-AzureRmWebApp` between the old `AzureRm` and the new `Az` module, actually I test it with `Az`, I notice you are using `AzureRm`, so I convert my script to that and give you. I recommend you to use the new `Az` module, becasue the `AzureRm` has been deprecated and will never be updated, just uninstall it and install `Az` module, follow this [link](https://learn.microsoft.com/en-us/powershell/azure/migrate-from-azurerm-to-az?view=azps-2.8.0), then use my updated script. – Joy Wang Apr 03 '20 at 09:53
  • 2
    @RahulTripathi07 Hi, could you tell me why not accept the answer? It solved your issue. – Joy Wang Apr 17 '20 at 09:24