Functions/Get-Subscription.ps1

function Get-Subscription {
    try {
        $allSubscriptions = Get-AzureRmSubscription -TenantId $currentTenantId;

        if (!$?) {
            Out-Error $Global:Error[0];
        }

        if ($subscriptions) {
            $totalSubscriptionsFound = $subscriptions.Count;
            
        }
        else {
            $totalSubscriptionsFound = $allSubscriptions.Count;
        }

        Add-Log "No of subscriptions to process for tenant $currentTenantId : $totalSubscriptionsFound" "subscriptions";
        $totalSubscriptionsProcessed = 0;

        foreach ($subscription in $allSubscriptions) {
            if (!$subscriptions -or $subscriptions.contains($subscription.Id) -or $subscriptions.contains($subscription.Name)) {
                Set-AzureRmContext -Subscription $subscription.Id | Out-Null
                $subscriptionId = $subscription.Id;
                $subscriptionName = $subscription.Name;
                Write-Progress -Activity "Scanning Subscriptions" -Id 4 -CurrentOperation "Processing subscription: $subscriptionName" -Status "Subscriptions in current tenant: $totalSubscriptionsProcessed of $totalSubscriptionsFound" -PercentComplete (($totalSubscriptionsProcessed / $totalSubscriptionsFound) * 100)
            
                $resourceGroups = Get-AzureRmResourceGroup;

                if (!$?) {
                    Out-Error $Global:Error[0];
    
                }

                $totalResourceGroupsFound = $resourceGroups.Count;
                Add-Log "No of resource groups found in subscription $subscriptionName ($subscriptionId): $totalResourceGroupsFound" "resourcegroups";

                $totalResourceGroupsProcessed = 0;

                foreach ($resourceGroup in $resourceGroups) {
                    $resourceGroupName = $resourceGroup.ResourceGroupName;
                    Write-Progress -Activity "Scanning Resource Groups" -Id 5 -CurrentOperation "Processing resource group: $resourceGroupName" -Status "Resource groups in current subscription: $totalResourceGroupsProcessed of $totalResourceGroupsFound" -PercentComplete (($totalResourceGroupsProcessed / $totalResourceGroupsFound) * 100)

                    if (!$sections -or $sections.Contains("as")) {
                        Get-AppService;
                        Add-Log "Processed App service certificates in resource group $resourceGroupName" "resourcegroups";
                    }
                    if (!$sections -or $sections.Contains("kv")) {
                        Get-KeyVault;
                        Add-Log "Processed Key vaults (keys, secrets and certificates) in resource group $resourceGroupName" "resourcegroups";
                    }
                    if (!$sections -or $sections.Contains("ad")) {
                        Join-AppSettingActiveDirectory;
                        Add-Log "Cross referenced web apps against AD applications in resource group $resourceGroupName" "resourcegroups";
                    }
                    $totalResourceGroupsProcessed++;
                    Add-Log "processed resource group in subscription $subscriptionName ($subscriptionId): $resourceGroupName" "resourcegroups";
                }

                Add-Log "No of resource groups processed in subscription $subscriptionName ($subscriptionId): $totalResourceGroupsProcessed" "resourcegroups";
                $totalSubscriptionsProcessed++;
                Add-Log "processed subscription in tenant $currentTenantId : $subscriptionName ($subscriptionId)" "subscriptions";
            }
        }
        Add-Log "No of subscriptions processed in tenant $currentTenantId : $totalSubscriptionsProcessed" "subscriptions";
    }
    catch {
        Out-Error $Global:Error[0];
    }
    
}