<commit_before># Azure Calls(RDFE/ARM) performed to get all the resource information used by AzureFileCopy Task #

$ErrorActionPreference = 'Stop'
$ARMStorageAccountResourceType =  "Microsoft.Storage/storageAccounts"
$ARMVirtualMachinesResourceType = "Microsoft.Compute/virtualMachines"
$ARMClassicVirtualMachinesResourceType = "Microsoft.ClassicCompute/virtualMachines"

function Get-AzureStorageAccountResourceGroupName
{
    param([string]$storageAccountName)

    try
    {
        Write-Verbose "[Azure Call]Getting resource details for azure storage account resource: $storageAccountName with resource type: $ARMStorageAccountResourceType" -Verbose
        $azureStorageAccountResourceDetails = Get-AzureRMResource -ResourceName $storageAccountName | Where-Object { $_.ResourceType -eq $ARMStorageAccountResourceType }
        Write-Verbose "[Azure Call]Retrieved resource details successfully for azure storage account resource: $storageAccountName with resource type: $ARMStorageAccountResourceType" -Verbose

        $azureResourceGroupName = $azureStorageAccountResourceDetails.ResourceGroupName
    }
    finally
    {
        if ([string]::IsNullOrEmpty($azureResourceGroupName) -eq $true)
        {
            Write-Verbose "(ARM)Storage account: $storageAccountName not found" -Verbose
            Write-TaskSpecificTelemetry "PREREQ_StorageAccountNotFound"
            Throw (Get-LocalizedString -Key "Storage account: {0} not found. Please specify existing storage account" -ArgumentList $storageAccountName)
        }
    }

    return $azureStorageAccountResourceDetails.ResourceGroupName
}

function Get-AzureStorageKeyFromARM
{
    param([string]$storageAccountName)

    # get azure storage account resource group name
    $azureResourceGroupName = Get-AzureStorageAccountResourceGroupName -storageAccountName $storageAccountName

    Write-Verbose "[Azure Call]Retrieving storage key for the storage account: $storageAccount in resource group: $azureResourceGroupName" -Verbose
    $storageKeyDetails = Get-AzureRMStorageAccountKey -ResourceGroupName $azureResourceGroupName -Name $storageAccount 
    $storageKey = $storageKeyDetails.Key1
    Write-Verbose "[Azure Call]Retrieved storage key successfully for the storage account: $storageAccount in resource group: $azureResourceGroupName" -Verbose

    return $storageKey
}

function Get-AzureStorageKeyFromRDFE
{
    param([string]$storageAccountName)

    Write-Verbose "[Azure Call](RDFE)Retrieving storage key for the storage account: $storageAccount" -Verbose
    $storageKeyDetails = Get-AzureStorageKey -StorageAccountName $storageAccountName
    $storageKey = $storageKeyDetails.Primary
    Write-Verbose "[Azure Call](RDFE)Retrieved storage key successfully for the storage account: $storageAccount" -Verbose

    return $storageKey
}

function Remove-AzureContainer
{
    param([string]$containerName,
          [Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext]$storageContext,
          [string]$storageAccount)

    Write-Verbose "[Azure Call]Deleting container: $containerName in storage account: $storageAccount" -Verbose
    Remove-AzureStorageContainer -Name $containerName -Context $storageContext -Force -ErrorAction SilentlyContinue
    Write-Verbose "[Azure Call]Deleted container: $containerName in storage account: $storageAccount" -Verbose
}

function Initialize-GlobalMaps
{
    $fqdnMap = @{}
    Set-Variable -Name fqdnMap -Value $fqdnMap -Scope "Global"

    $winRmHttpsPortMap = @{}
    Set-Variable -Name winRmHttpsPortMap -Value $winRmHttpsPortMap -Scope "Global"
}

function Does-AzureResourceMatchesFilterCriteria
{
    param([object]$azureVMResource,
          [string]$resourceFilteringMethod,
          [string]$filter)

    # If no filters are provided, by default operations are performed on all azure resources
    if([string]::IsNullOrEmpty($filter))
    {
        return $true
    }

    # machine name based filtering
    if($resourceFilteringMethod -eq "machineNames")
    {
        $machineFilterArray = $filter.Split(',').Trim()
        return $machineFilterArray.Contains($azureVMResource.Name)
    }

    # tag based filtering
    if($resourceFilteringMethod -eq "tags")
    {
        $tagsFilterArray = $tagsFilter.Split(';').Trim()
        foreach($tag in $tagsFilterArray)
        {
            $tagKeyValue = $tags.Split(':').Trim()
            $tagKey =  $tagKeyValue[0]
            $tagValue = $tagKeyValue[1]

            if($tagKeyValue.Length -ne 2 -or [string]::IsNullOrWhiteSpace($tagKey) -or [string]::IsNullOrWhiteSpace($tagValue))
            {
                throw (Get-LocalizedString -Key 'Please have the tags in this format Tag1:TagValue1;Tag2:TagValue2;Tag3:TagValue3')
            }

            if($azureVMResource.Tags.Keys.Contains($tagKey) -and $azureVMResource.Tags[$tagKey] -eq $tagValue)
            {
                return $true
            }
        }
    }
}

function Get-FilteredAzureClassicVMsInResourceGroup
{
    param([string]$resourceGroupName,
          [string]$resourceFilteringMethod,
          [string]$filter)

    Write-Verbose -Verbose "[Azure Call]Getting resource group:$resourceGroupName classic virtual machines type resources"
    $allAzureClassicVMResources = Get-AzureVM -ServiceName $resourceGroupName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    Write-Verbose -Verbose "[Azure Call]Got resource group:$resourceGroupName classic virtual machines type resources"

    $azureClassicVMResources = @()
    foreach($azureClassicVMResource in $allAzureClassicVMResources)
    {
        if(Does-AzureResourceMatchesFilterCriteria -azureVMResource $azureClassicVMResource -resourceFilteringMethod $resourceFilteringMethod -filter $filter)
        {
           $azureClassicVMResources += $azureClassicVMResource
        }
    }

    Set-Variable -Name azureClassicVMResources -Value $azureClassicVMResources -Scope "Global"

    return $azureClassicVMResources
}

function Get-FilteredAzureRMVMsInResourceGroup
{
    param([string]$resourceGroupName,
          [string]$resourceFilteringMethod,
          [string]$filter)

    try
    {
        Write-Verbose -Verbose "[Azure Call]Getting resource group:$resourceGroupName RM virtual machines type resources"
        $allAzureRMVMResources = Get-AzureRMVM -ResourceGroupName $resourceGroupName
        Write-Verbose -Verbose "[Azure Call]Got resource group:$resourceGroupName RM virtual machines type resources"

        $azureRMVMResources = @()
        foreach($azureRMVMResource in $allAzureRMVMResources)
        {
            if(Does-AzureResourceMatchesFilterCriteria -azureVMResource $azureRMVMResource -resourceFilteringMethod $resourceFilteringMethod -filter $filter)
            {
                $azureRMVMResources += $azureRMVMResource
            }
        }

        Set-Variable -Name azureRMVMResources -Value $azureRMVMResources -Scope "Global"
    }<commit_msg>Fixing null valued expression error<commit_after># Azure Calls(RDFE/ARM) performed to get all the resource information used by AzureFileCopy Task #

$ErrorActionPreference = 'Stop'
$ARMStorageAccountResourceType =  "Microsoft.Storage/storageAccounts"
$ARMVirtualMachinesResourceType = "Microsoft.Compute/virtualMachines"
$ARMClassicVirtualMachinesResourceType = "Microsoft.ClassicCompute/virtualMachines"

function Get-AzureStorageAccountResourceGroupName
{
    param([string]$storageAccountName)

    try
    {
        Write-Verbose "[Azure Call]Getting resource details for azure storage account resource: $storageAccountName with resource type: $ARMStorageAccountResourceType" -Verbose
        $azureStorageAccountResourceDetails = Get-AzureRMResource -ResourceName $storageAccountName | Where-Object { $_.ResourceType -eq $ARMStorageAccountResourceType }
        Write-Verbose "[Azure Call]Retrieved resource details successfully for azure storage account resource: $storageAccountName with resource type: $ARMStorageAccountResourceType" -Verbose

        $azureResourceGroupName = $azureStorageAccountResourceDetails.ResourceGroupName
    }
    finally
    {
        if ([string]::IsNullOrEmpty($azureResourceGroupName) -eq $true)
        {
            Write-Verbose "(ARM)Storage account: $storageAccountName not found" -Verbose
            Write-TaskSpecificTelemetry "PREREQ_StorageAccountNotFound"
            Throw (Get-LocalizedString -Key "Storage account: {0} not found. Please specify existing storage account" -ArgumentList $storageAccountName)
        }
    }

    return $azureStorageAccountResourceDetails.ResourceGroupName
}

function Get-AzureStorageKeyFromARM
{
    param([string]$storageAccountName)

    # get azure storage account resource group name
    $azureResourceGroupName = Get-AzureStorageAccountResourceGroupName -storageAccountName $storageAccountName

    Write-Verbose "[Azure Call]Retrieving storage key for the storage account: $storageAccount in resource group: $azureResourceGroupName" -Verbose
    $storageKeyDetails = Get-AzureRMStorageAccountKey -ResourceGroupName $azureResourceGroupName -Name $storageAccount 
    $storageKey = $storageKeyDetails.Key1
    Write-Verbose "[Azure Call]Retrieved storage key successfully for the storage account: $storageAccount in resource group: $azureResourceGroupName" -Verbose

    return $storageKey
}

function Get-AzureStorageKeyFromRDFE
{
    param([string]$storageAccountName)

    Write-Verbose "[Azure Call](RDFE)Retrieving storage key for the storage account: $storageAccount" -Verbose
    $storageKeyDetails = Get-AzureStorageKey -StorageAccountName $storageAccountName
    $storageKey = $storageKeyDetails.Primary
    Write-Verbose "[Azure Call](RDFE)Retrieved storage key successfully for the storage account: $storageAccount" -Verbose

    return $storageKey
}

function Remove-AzureContainer
{
    param([string]$containerName,
          [Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext]$storageContext,
          [string]$storageAccount)

    Write-Verbose "[Azure Call]Deleting container: $containerName in storage account: $storageAccount" -Verbose
    Remove-AzureStorageContainer -Name $containerName -Context $storageContext -Force -ErrorAction SilentlyContinue
    Write-Verbose "[Azure Call]Deleted container: $containerName in storage account: $storageAccount" -Verbose
}

function Initialize-GlobalMaps
{
    $fqdnMap = @{}
    Set-Variable -Name fqdnMap -Value $fqdnMap -Scope "Global"

    $winRmHttpsPortMap = @{}
    Set-Variable -Name winRmHttpsPortMap -Value $winRmHttpsPortMap -Scope "Global"
}

function Does-AzureResourceMatchesFilterCriteria
{
    param([object]$azureVMResource,
          [string]$resourceFilteringMethod,
          [string]$filter)

    # If no filters are provided, by default operations are performed on all azure resources
    if([string]::IsNullOrEmpty($filter))
    {
        return $true
    }

    # machine name based filtering
    if($resourceFilteringMethod -eq "machineNames")
    {
        $machineFilterArray = $filter.Split(',').Trim()
        return $machineFilterArray.Contains($azureVMResource.Name)
    }

    # tag based filtering
    if($resourceFilteringMethod -eq "tags")
    {
        $tagsFilterArray = $filter.Split(';').Trim()
        foreach($tag in $tagsFilterArray)
        {
            $tagKeyValue = $tag.Split(':').Trim()
            $tagKey =  $tagKeyValue[0]
            $tagValue = $tagKeyValue[1]

            if($tagKeyValue.Length -ne 2 -or [string]::IsNullOrWhiteSpace($tagKey) -or [string]::IsNullOrWhiteSpace($tagValue))
            {
                throw (Get-LocalizedString -Key 'Please have the tags in this format Tag1:TagValue1;Tag2:TagValue2;Tag3:TagValue3')
            }

            if($azureVMResource.Tags.Keys.Contains($tagKey) -and $azureVMResource.Tags[$tagKey] -eq $tagValue)
            {
                return $true
            }
        }
    }
}

function Get-FilteredAzureClassicVMsInResourceGroup
{
    param([string]$resourceGroupName,
          [string]$resourceFilteringMethod,
          [string]$filter)

    Write-Verbose -Verbose "[Azure Call]Getting resource group:$resourceGroupName classic virtual machines type resources"
    $allAzureClassicVMResources = Get-AzureVM -ServiceName $resourceGroupName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    Write-Verbose -Verbose "[Azure Call]Got resource group:$resourceGroupName classic virtual machines type resources"

    $azureClassicVMResources = @()
    if($allAzureClassicVMResources)
    {
        foreach($azureClassicVMResource in $allAzureClassicVMResources)
        {
            if(Does-AzureResourceMatchesFilterCriteria -azureVMResource $azureClassicVMResource -resourceFilteringMethod $resourceFilteringMethod -filter $filter)
            {
                $azureClassicVMResources += $azureClassicVMResource
            }
        }
    }

    Set-Variable -Name azureClassicVMResources -Value $azureClassicVMResources -Scope "Global"

    return $azureClassicVMResources
}

function Get-FilteredAzureRMVMsInResourceGroup
{
    param([string]$resourceGroupName,
          [string]$resourceFilteringMethod,
          [string]$filter)

    try
    {
        Write-Verbose -Verbose "[Azure Call]Getting resource group:$resourceGroupName RM virtual machines type resources"
        $allAzureRMVMResources = Get-AzureRMVM -ResourceGroupName $resourceGroupName
        Write-Verbose -Verbose "[Azure Call]Got resource group:$resourceGroupName RM virtual machines type resources"

        $azureRMVMResources = @()
        if($allAzureRMVMResources)
        {
            foreach($azureRMVMResource in $allAzureRMVMResources)
            {
                if(Does-AzureResourceMatchesFilterCriteria -azureVMResource $azureRMVMResource -resourceFilteringMethod $resourceFilteringMethod -filter $filter)
                {
                    $azureRMVMResources += $azureRMVMResource
                }
            }
        }

        Set-Variable -Name azureRMVMResources -Value $azureRMVMResources -Scope "Global"
    }
{'fraction_non_alphanumeric': 0.0832443970117396, 'fraction_numerical': 0.0015652792600498043, 'mean_word_length': 3.813698630136986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1670118', 'n_tokens_mistral': 3740, 'n_tokens_neox': 3642, 'n_words': 854}