GinShell.Azure/Public/Get-GsAzureVmAndDisk.ps1

function Get-GsAzureVmAndDisk {
    <#
    .SYNOPSIS
        Retrieves VMs and their associated managed disks.
    .PARAMETER VmNames
        One or more VM names to look up.
    .EXAMPLE
        Get-GsAzureVmAndDisk -VmNames 'VM1','VM2'
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string[]]$VmNames
    )

    Write-GsLog -Message "Getting VM and disk list from Azure..." -Type Info

    try {
        $vmList   = Get-AzVM -ErrorAction Stop
        $diskList = Get-AzDisk -ErrorAction Stop

        Write-GsLog -Message "Filtering VMs and their associated disks..." -Type Debug

        $selectedVms = $vmList | Where-Object { $_.Name -in $VmNames }

        if (-not $selectedVms) {
            Write-GsLog -Message "No VMs found matching: $($VmNames -join ', ')" -Type Error
            return
        }

        $selectedVmIds   = $selectedVms.Id
        $associatedDisks = $diskList | Where-Object { $_.ManagedBy -in $selectedVmIds }

        Write-GsLog -Message "Found $($selectedVms.Count) VM(s) and $($associatedDisks.Count) associated disk(s)." -Type Info

        return [PSCustomObject]@{
            VMs   = $selectedVms
            Disks = $associatedDisks
        }
    }
    catch {
        Write-GsLog -Message "Failed to get VM or disk information: $($_.Exception.Message)" -Type Error
        throw
    }
}