scripts/Collect-VMMetrics.ps1

<#
.SYNOPSIS
    Azure Cloud Shell runner: collect VM platform metrics across all (or selected)
    subscriptions and upload one VMPerformance CSV to central blob storage via a SAS token.

.DESCRIPTION
    Self-contained, generic, parametrized. Installs the FinOpsVMMetrics module from the
    PowerShell Gallery if absent, then runs a single multi-subscription collection that
    writes one combined CSV (+ manifest) and uploads it to {Customer}/{date}/ in the
    target container using the supplied container-scoped SAS token (works cross-tenant,
    no account key needed).

.PARAMETER Customer
    Real customer name; first blob path segment (e.g. 'Strauss').

.PARAMETER StorageAccountName
    Central storage account to upload to (e.g. 'projectmgmtfinopsnav').

.PARAMETER SasToken
    Container-scoped SAS token with create+write permission (starts with '?' or 'sv=').

.PARAMETER ContainerName
    Blob container (default 'vmperformance').

.PARAMETER WindowDays
    Look-back window in days (default 90; platform metrics retain ~93 days).

.PARAMETER SubscriptionId
    Optional subscription id(s). Default: every subscription visible to the signed-in identity.

.EXAMPLE
    ./Collect-VMMetrics.ps1 -Customer Strauss -StorageAccountName projectmgmtfinopsnav `
        -SasToken '?sv=2024-...&sig=...'
#>

[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '',
    Justification = 'Interactive Cloud Shell runner; Write-Host is the intended user-facing output.')]
param(
    [Parameter(Mandatory)] [string]   $Customer,
    [Parameter(Mandatory)] [string]   $StorageAccountName,
    [Parameter(Mandatory)] [string]   $SasToken,
    [string]   $ContainerName = 'vmperformance',
    [ValidateRange(1, 93)] [int] $WindowDays = 90,
    [string[]] $SubscriptionId
)

$ErrorActionPreference = 'Stop'

if (-not (Get-Module -ListAvailable -Name FinOpsVMMetrics)) {
    Write-Host "Installing FinOpsVMMetrics from the PowerShell Gallery..."
    Install-Module FinOpsVMMetrics -Scope CurrentUser -Force -AllowClobber
}
Import-Module FinOpsVMMetrics

$subs = if ($SubscriptionId) { $SubscriptionId } else { (Get-AzSubscription).Id }
Write-Host "Collecting VM metrics for '$Customer' across $($subs.Count) subscription(s) (window: $WindowDays days)..."

$report = Invoke-VMMetricsCollection -SubscriptionId $subs -Customer $Customer -WindowDays $WindowDays `
    -StorageAccountName $StorageAccountName -ContainerName $ContainerName -SasToken $SasToken `
    -OutputPath (Join-Path ([System.IO.Path]::GetTempPath()) 'vmmetrics')

$report | Format-List OutputFile, BlobUri, VMsProcessed, VMsSkipped, RowCount
if ($report.Skipped.Count) {
    Write-Host "`nSkipped:"
    $report.Skipped | Format-Table -AutoSize
}
Write-Host "`nDone. Uploaded blob: $($report.BlobUri)"