1

We have more than 1000 Azure subscriptions and some subscriptions have 1000+ resources. We are running powershell script from automation account to collect using graph explorer module to collect information about all resource in each subscription. There is a default limit where powershell can only collect data from 1000 subscriptions and also 100 reources and to overcome this limit we have put togather following script but it is giving us an error. I believe the issue is within for loop somwhere.


Import-Module Az.Accounts
Import-Module Az.Automation
Import-Module Az.Storage
Import-Module Az.ResourceGraph

$resourceGroup = "rg-xxxxx"
$storageAccount = "stxxxxxxxxxx"
$subscriptionid = "xxxx-xxxx-xxxx"
$storageAccountContainer = "azure"

$connectionName = "AzureRunAsConnection" # Run using Run As account
try
{
    # Get the connection "AzureRunAsConnection "

    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName

    "Logging in to Azure..."
    $connectionResult =  Connect-AzAccount -Tenant $servicePrincipalConnection.TenantID `
                             -ApplicationId $servicePrincipalConnection.ApplicationID   `
                             -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint `
                             -ServicePrincipal
    "Logged in."

}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

$date = get-date -format dd-MM-yyyy

$query = Search-AzGraph -Query 'Resources'


$subscriptions = Get-AzSubscription
$SubscriptionIds = $subscriptions.Id
$counter = [PSCustomObject] @{ Value = 0 }
$batchSize = 1000
$response = @()
$data = @()

$subscriptionsBatch = $subscriptionIds | Group -Property { [math]::Floor($counter.Value++ / $batchSize) }

foreach ($batch in $subscriptionsBatch){
$skipToken = $null;
$queryResult = $null;

do {
    if ($null -eq $skipToken){
        $queryResult = Search-Azgraph -Query $query -first 1000 -subscription $batch.Group;
        $data = $data + $queryResult;
    }
    else{
        $queryResult = Search-AzGraph -Query $query -SkipToken $skipToken -subscription $batch.Group;
        $data = $data + $queryResult; 
    }
    $skipToken = $queryResult.SkipToken;
  
   } 
   while ($null -ne $skipToken);
}

$data | Export-Csv "$Env:temp/Azure-temp-totalresources.csv" -notypeinformation

Set-AzContext -SubscriptionId $subscriptionid
Set-AzCurrentStorageAccount -StorageAccountName $storageAccount -ResourceGroupName $resourceGroup
Remove-AzStorageBlob -Blob 'Azure-Azure-totalresources.csv' -Container $storageAccountContainer
Set-AzStorageBlobContent -Container $storageAccountContainer -file "$Env:temp/Azure-temp-totalresources.csv" -Blob "Azure-totalresources.csv" -force

Error we are getting is below

Search-AzGraph: C:\Temp\z11pylt2.z2k\8a832791-6abe-4a38-b4b5-0c4eea1a215d.ps1:61 Line | 61 | … eryResult = Search-AzGraph -Query $query -SkipToken $skipToken -subsc … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Cannot process argument because the value of argument "name" is not | valid. Change the value of the "name" argument and run the operation | again.

A A
  • 21
  • 4

0 Answers0