src/collect/Get-ScoutSubscriptionSecurityPolicySweep.ps1
|
#Requires -Version 7.0 Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' <# .SYNOPSIS Collects the subscription-scoped security, diagnostic, and policy datasets used by the legacy inventory collectors. .DESCRIPTION Performs one context-scoped sweep per subscription and returns one synthetic resource envelope for that subscription. Each Azure call is isolated so a denied or unavailable dataset becomes an empty collection without discarding its neighbours. Defender assessment queries retry transient HTTP 5xx failures up to three times. A Microsoft.Security provider-registration failure for Defender pricing is represented as an empty, Unavailable dataset rather than as a collection error. .PARAMETER Subscriptions Subscription objects with an `id` property and an optional `name` property. .OUTPUTS One `[pscustomobject]` per valid subscription with this integration contract: type = 'AZSC/Subscription/SecurityPolicySweep' subscriptionId subscriptionName properties.DefenderAlerts properties.DefenderAssessments properties.DefenderPricing properties.DefenderSecureScores properties.DefenderSecureScoreControls properties.SubscriptionDiagnosticSettings properties.PolicyComplianceStates properties.CollectionStatus properties.CollectionErrors Every dataset property is always an array, including on failure. CollectionStatus records Success, Unavailable, or Skipped for every dataset. CollectionErrors contains only actual failures and is also always an array. .NOTES Collect-phase implementation for Epic AB#5638. The legacy collectors and the collect orchestrator intentionally remain unchanged until their declarative definitions are integrated. #> function Get-ScoutSubscriptionSecurityPolicySweep { [CmdletBinding()] param( [Parameter(Mandatory)] [AllowEmptyCollection()] [object[]] $Subscriptions, # Read Defender assessments for every subscription at the same time before the # per-subscription pass. The assessments list is the slowest call in the sweep. [switch] $PrefetchAssessments ) if ($Subscriptions.Count -eq 0) { return @() } function Invoke-ScoutSweepDataset { [CmdletBinding()] param( [Parameter(Mandatory)] [string] $Dataset, [Parameter(Mandatory)] [string] $SubscriptionName, [Parameter(Mandatory)] [scriptblock] $Operation, [ValidateRange(1, 5)] [int] $MaxAttempts = 1, [switch] $ProviderRegistrationIsUnavailable ) for ($attempt = 1; $attempt -le $MaxAttempts; $attempt++) { try { return [pscustomobject]@{ Data = @(& $Operation) Status = 'Success' Error = $null } } catch { $message = $_.Exception.Message # 'Please register to Microsoft.Security in order to view your security status' is # the phrasing Azure actually uses for an unregistered provider on this endpoint # (observed live, AB#6900) -- it says "register to", never "not registered", so the # original patterns missed it and the quiet Unavailable path fell through to a # raw Write-Warning on every collect against a subscription without Defender. $providerUnavailable = $ProviderRegistrationIsUnavailable -and $message -match '(?i)MissingSubscriptionRegistration|SubscriptionNotRegistered|not registered.+Microsoft\.Security|Microsoft\.Security.+not registered|register to Microsoft\.Security' # Regulatory compliance exists only on subscriptions with a paid Defender for Cloud # plan. On the free tier Azure answers "Regulatory compliance is not supported for # subscription '<id>' as it has no standard pricing bundle". That is the state of # the subscription, not a failed read: one field tenant raised this warning for # eight of its nine subscriptions on every scan. $notOfferedOnThisTier = $message -match '(?i)Regulatory compliance is not supported for subscription.+no standard pricing bundle' if ($providerUnavailable -or $notOfferedOnThisTier) { # The reason travels with the result so collection health can say why the # dataset is empty, instead of a bare "not available". return [pscustomobject]@{ Data = @() Status = 'Unavailable' Error = $null Reason = if ($notOfferedOnThisTier) { 'Not applicable: regulatory compliance needs a paid Microsoft Defender for Cloud plan, and this subscription is on the free tier.' } else { 'Not applicable: the Microsoft.Security resource provider is not registered on this subscription.' } } } $transientFailure = $message -match '(?i)\bHTTP\s*(429|500|502|503|504)\b|InternalServerError|BadGateway|ServiceUnavailable|GatewayTimeout|TooManyRequests|HttpClient\.Timeout|timed out|TaskCanceled|operation (?:was|has been) canceled' if ($transientFailure -and $attempt -lt $MaxAttempts) { # Seconds, not milliseconds: a server-side 5xx from Defender did not clear in the # 0.6 s the old 200/400 ms back-off allowed, and the subscription went Partial # (AB#9300). 2, 4, 8, 16 s (capped at 20) gives a transient fault time to clear. Start-Sleep -Seconds ([Math]::Min(20, [Math]::Pow(2, $attempt))) continue } Write-Warning "Get-ScoutSubscriptionSecurityPolicySweep: '$Dataset' failed for subscription '$SubscriptionName' after $attempt attempt(s): $message" return [pscustomobject]@{ Data = @() Status = 'Unavailable' Error = [pscustomobject]@{ Dataset = $Dataset Message = $message } } } } } function Invoke-ScoutDefenderPricingDataset { param( [Parameter(Mandatory)][string] $SubscriptionName, [Parameter(Mandatory)][string] $SubscriptionId, # Rows the concurrent prefetch already read without any error. [AllowNull()] $Prefetched ) if ($null -ne $Prefetched) { return [pscustomobject]@{ Data = @($Prefetched); Status = 'Success'; Error = $null } } # Az.Security emits an error when Microsoft.Security is unregistered. Run this known # optional read with a quiet error variable so the expected provider state does not leak # into scout-console.log as a TerminatingError before Scout can classify it. $pricingErrors = @() $pricingData = @() try { if ($cmdAvailable['Get-AzSecurityPricing']) { $pricingData = @(Get-AzSecurityPricing -ErrorAction SilentlyContinue -ErrorVariable +pricingErrors) } else { # Az.Security is not installed: read the same list through ARM. A failure is # classified below exactly like a cmdlet failure (provider registration etc.). $pricingData = @(Get-ScoutDefenderPricingRest -SubscriptionId $SubscriptionId) } } catch { $pricingErrors += $_ } if (@($pricingErrors).Count -eq 0) { return [pscustomobject]@{ Data = $pricingData; Status = 'Success'; Error = $null } } $message = (@($pricingErrors | ForEach-Object { $_.Exception.Message }) -join '; ') if ($message -match '(?i)MissingSubscriptionRegistration|SubscriptionNotRegistered|not registered.+Microsoft\.Security|Microsoft\.Security.+not registered|register to Microsoft\.Security') { return [pscustomobject]@{ Data = @() Status = 'Unavailable' Error = $null Reason = 'Not applicable: the Microsoft.Security resource provider is not registered on this subscription.' } } Write-Warning "Get-ScoutSubscriptionSecurityPolicySweep: 'DefenderPricing' failed for subscription '$SubscriptionName': $message" return [pscustomobject]@{ Data = @() Status = 'Unavailable' Error = [pscustomobject]@{ Dataset = 'DefenderPricing'; Message = $message } } } function ConvertFrom-ScoutDefenderAlertRestRow { param([Parameter(Mandatory)]$Alert) function Get-AlertValue { param($Object, [Parameter(Mandatory)][string]$Name) if ($null -eq $Object) { return $null } $property = $Object.PSObject.Properties[$Name] if ($null -eq $property) { return $null } return $property.Value } $properties = Get-AlertValue -Object $Alert -Name 'properties' if ($null -eq $properties) { $properties = $Alert } $timeGeneratedValue = Get-AlertValue -Object $properties -Name 'timeGeneratedUtc' $timeGenerated = if ($timeGeneratedValue) { [datetime]$timeGeneratedValue } else { $null } # Preserve the established Get-AzSecurityAlert projection consumed by the declarative # DefenderAlerts collector. The REST API nests these values under `properties`. [pscustomobject]@{ Id = Get-AlertValue -Object $Alert -Name 'id' Name = Get-AlertValue -Object $Alert -Name 'name' AlertDisplayName = Get-AlertValue -Object $properties -Name 'alertDisplayName' AlertType = Get-AlertValue -Object $properties -Name 'alertType' Severity = Get-AlertValue -Object $properties -Name 'severity' Status = Get-AlertValue -Object $properties -Name 'status' TimeGeneratedUtc = $timeGenerated Description = Get-AlertValue -Object $properties -Name 'description' RemediationSteps = @(Get-AlertValue -Object $properties -Name 'remediationSteps') Intent = Get-AlertValue -Object $properties -Name 'intent' Entities = @(Get-AlertValue -Object $properties -Name 'entities') ResourceIdentifiers = @(Get-AlertValue -Object $properties -Name 'resourceIdentifiers') ExtendedProperties = Get-AlertValue -Object $properties -Name 'extendedProperties' ConfidenceLevel = Get-AlertValue -Object $properties -Name 'confidenceLevel' } } function Invoke-ScoutDefenderAlertRestFallback { param([Parameter(Mandatory)][string]$SubscriptionId) $alerts = [System.Collections.Generic.List[object]]::new() $path = "/subscriptions/$SubscriptionId/providers/Microsoft.Security/alerts?api-version=2022-01-01" do { # Do not pass -SkipHttpErrorCheck: older supported Az.Accounts releases do not expose # it on Invoke-AzRestMethod. Non-success responses throw and are classified from their # actual service error by the surrounding dataset boundary. $response = Invoke-AzRestMethod -Path $path -Method GET -ErrorAction Stop if ($null -eq $response) { throw 'Defender alerts REST fallback returned no response.' } $statusProperty = $response.PSObject.Properties['StatusCode'] if ($null -ne $statusProperty) { $statusCode = [int]$statusProperty.Value if ($statusCode -lt 200 -or $statusCode -ge 300) { $responseContent = $response.PSObject.Properties['Content'] $responseText = if ($null -ne $responseContent) { [string]$responseContent.Value } else { '' } throw "Defender alerts REST fallback returned HTTP $statusCode. $responseText" } } $contentProperty = $response.PSObject.Properties['Content'] $content = if ($null -ne $contentProperty) { $contentProperty.Value } else { $response } if ($content -is [string]) { if ([string]::IsNullOrWhiteSpace($content)) { $content = [pscustomobject]@{ value = @() } } else { $content = $content | ConvertFrom-Json } } if ($null -eq $content) { $content = [pscustomobject]@{ value = @() } } $valueProperty = $content.PSObject.Properties['value'] $pageRows = if ($null -ne $valueProperty) { @($valueProperty.Value) } else { @() } foreach ($alert in $pageRows) { if ($null -ne $alert) { $alerts.Add((ConvertFrom-ScoutDefenderAlertRestRow -Alert $alert)) } } $nextLinkProperty = $content.PSObject.Properties['nextLink'] $path = if ($null -ne $nextLinkProperty) { [string]$nextLinkProperty.Value } else { $null } } while (-not [string]::IsNullOrWhiteSpace($path)) return $alerts.ToArray() } function Get-ScoutDefenderAlerts { param([Parameter(Mandatory)][string]$SubscriptionId) if (-not $cmdAvailable['Get-AzSecurityAlert']) { return @(Invoke-ScoutDefenderAlertRestFallback -SubscriptionId $SubscriptionId) } try { return @(Get-AzSecurityAlert -ErrorAction Stop) } catch { if ($_.Exception -isnot [System.NullReferenceException] -and $_.Exception.Message -notmatch '(?i)Object reference not set to an instance of an object') { throw } Write-Verbose 'Get-AzSecurityAlert failed inside Az.Security; using the documented Defender for Cloud alerts REST endpoint.' return @(Invoke-ScoutDefenderAlertRestFallback -SubscriptionId $SubscriptionId) } } function ConvertFrom-ScoutDefenderAssessmentRestRow { param([Parameter(Mandatory)]$Assessment) function Get-AssessmentValue { param($Object, [Parameter(Mandatory)][string]$Name) if ($null -eq $Object) { return $null } $property = $Object.PSObject.Properties[$Name] if ($null -eq $property) { return $null } return $property.Value } $properties = Get-AssessmentValue -Object $Assessment -Name 'properties' if ($null -eq $properties) { $properties = $Assessment } # Preserve the Get-AzSecurityAssessment projection consumed by the shipped collector. # The documented REST response nests these values beneath `properties`. [pscustomobject]@{ Id = Get-AssessmentValue -Object $Assessment -Name 'id' Name = Get-AssessmentValue -Object $Assessment -Name 'name' DisplayName = Get-AssessmentValue -Object $properties -Name 'displayName' ResourceDetails = Get-AssessmentValue -Object $properties -Name 'resourceDetails' Status = Get-AssessmentValue -Object $properties -Name 'status' Metadata = Get-AssessmentValue -Object $properties -Name 'metadata' AdditionalData = Get-AssessmentValue -Object $properties -Name 'additionalData' Links = Get-AssessmentValue -Object $properties -Name 'links' PartnersData = Get-AssessmentValue -Object $properties -Name 'partnersData' } } function Invoke-ScoutDefenderAssessmentRestFallback { param([Parameter(Mandatory)][string]$SubscriptionId) $assessments = [System.Collections.Generic.List[object]]::new() $path = "/subscriptions/$SubscriptionId/providers/Microsoft.Security/assessments?api-version=2021-06-01" do { $response = Invoke-AzRestMethod -Path $path -Method GET -ErrorAction Stop if ($null -eq $response) { throw 'Defender assessments REST fallback returned no response.' } $statusProperty = $response.PSObject.Properties['StatusCode'] if ($null -ne $statusProperty) { $statusCode = [int]$statusProperty.Value if ($statusCode -lt 200 -or $statusCode -ge 300) { $responseContent = $response.PSObject.Properties['Content'] $responseText = if ($null -ne $responseContent) { [string]$responseContent.Value } else { '' } throw "Defender assessments REST fallback returned HTTP $statusCode. $responseText" } } $contentProperty = $response.PSObject.Properties['Content'] $content = if ($null -ne $contentProperty) { $contentProperty.Value } else { $response } if ($content -is [string]) { if ([string]::IsNullOrWhiteSpace($content)) { $content = [pscustomobject]@{ value = @() } } else { $content = $content | ConvertFrom-Json } } if ($null -eq $content) { $content = [pscustomobject]@{ value = @() } } $valueProperty = $content.PSObject.Properties['value'] foreach ($assessment in @(if ($null -ne $valueProperty) { $valueProperty.Value } else { @() })) { if ($null -ne $assessment) { $assessments.Add((ConvertFrom-ScoutDefenderAssessmentRestRow -Assessment $assessment)) } } $nextLinkProperty = $content.PSObject.Properties['nextLink'] $path = if ($null -ne $nextLinkProperty) { [string]$nextLinkProperty.Value } else { $null } } while (-not [string]::IsNullOrWhiteSpace($path)) return $assessments.ToArray() } function Get-ScoutDefenderAssessments { param( [Parameter(Mandatory)][string]$SubscriptionId ) if (-not $cmdAvailable['Get-AzSecurityAssessment']) { return @(Invoke-ScoutDefenderAssessmentRestFallback -SubscriptionId $SubscriptionId) } # Az.Security can surface an HttpClient.Timeout after 100 seconds even though the # documented list endpoint remains healthy. Suppress the raw cmdlet error record, retain # its message for classification, and fall back only for client-side timeout/null failures. $assessmentErrors = @() $assessmentData = @() try { $assessmentData = @(Get-AzSecurityAssessment -ErrorAction SilentlyContinue -ErrorVariable +assessmentErrors) } catch { $assessmentErrors += $_ } if (@($assessmentErrors).Count -eq 0) { return $assessmentData } $message = (@($assessmentErrors | ForEach-Object { $_.Exception.Message }) -join '; ') $fallbackRequired = $message -match '(?i)HttpClient\.Timeout|timed out|TaskCanceled|operation (?:was|has been) canceled|Object reference not set' if (-not $fallbackRequired) { throw $message } Write-Verbose 'Get-AzSecurityAssessment failed inside Az.Security; using the documented Defender for Cloud assessments REST endpoint.' return @(Invoke-ScoutDefenderAssessmentRestFallback -SubscriptionId $SubscriptionId) } # --- ARM REST readers for datasets whose Az cmdlet is not installed ------------------------- # Az.Security, Az.Monitor and Az.PolicyInsights are optional (not in RequiredModules). When a # cmdlet is missing the same data is read, read-only, through Invoke-AzRestMethod # (Az.Accounts) and projected to the property names the cmdlet objects carry, so every # downstream collector keeps working unchanged. function Get-ScoutRestValue { param($Object, [Parameter(Mandatory)][string]$Name) if ($null -eq $Object) { return $null } $property = $Object.PSObject.Properties[$Name] if ($null -eq $property) { return $null } return $property.Value } function Invoke-ScoutSweepRestList { param( [Parameter(Mandatory)][string]$Path, [ValidateSet('GET', 'POST')][string]$Method = 'GET', [Parameter(Mandatory)][string]$Label ) $rows = [System.Collections.Generic.List[object]]::new() $seen = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) $next = $Path do { if (-not $seen.Add($next)) { throw "$Label REST read returned a repeating nextLink." } # Non-success responses are thrown with the service body so the surrounding dataset # boundary classifies the real error (provider registration, free tier, 403, 5xx). $response = Invoke-AzRestMethod -Path $next -Method $Method -ErrorAction Stop if ($null -eq $response) { throw "$Label REST read returned no response." } $statusProperty = $response.PSObject.Properties['StatusCode'] if ($null -ne $statusProperty) { $statusCode = [int]$statusProperty.Value if ($statusCode -lt 200 -or $statusCode -ge 300) { $responseContent = $response.PSObject.Properties['Content'] $responseText = if ($null -ne $responseContent) { [string]$responseContent.Value } else { '' } throw "$Label REST read returned HTTP $statusCode. $responseText" } } $contentProperty = $response.PSObject.Properties['Content'] $content = if ($null -ne $contentProperty) { $contentProperty.Value } else { $response } if ($content -is [System.Collections.IDictionary]) { $content = $content | ConvertTo-Json -Depth 20 | ConvertFrom-Json } if ($content -is [string]) { if ([string]::IsNullOrWhiteSpace($content)) { $content = [pscustomobject]@{ value = @() } } else { $content = $content | ConvertFrom-Json } } if ($null -eq $content) { $content = [pscustomobject]@{ value = @() } } foreach ($row in @(Get-ScoutRestValue -Object $content -Name 'value')) { if ($null -ne $row) { $rows.Add($row) } } $link = Get-ScoutRestValue -Object $content -Name 'nextLink' if ([string]::IsNullOrWhiteSpace([string]$link)) { $link = Get-ScoutRestValue -Object $content -Name '@odata.nextLink' } $next = $null if (-not [string]::IsNullOrWhiteSpace([string]$link)) { # Invoke-AzRestMethod -Path wants a relative path; ARM nextLinks are absolute. $next = if ([string]$link -match '^(?i)https?://') { ([uri][string]$link).PathAndQuery } else { [string]$link } } } while (-not [string]::IsNullOrWhiteSpace($next)) return $rows.ToArray() } function Get-ScoutDefenderPricingRest { param([Parameter(Mandatory)][string]$SubscriptionId) foreach ($item in @(Invoke-ScoutSweepRestList -Label 'Defender pricing' -Path "/subscriptions/$SubscriptionId/providers/Microsoft.Security/pricings?api-version=2024-01-01")) { $properties = Get-ScoutRestValue -Object $item -Name 'properties' $trialRaw = Get-ScoutRestValue -Object $properties -Name 'freeTrialRemainingTime' $trial = $null if ($trialRaw -is [timespan]) { $trial = $trialRaw } elseif (-not [string]::IsNullOrWhiteSpace([string]$trialRaw)) { # ISO 8601 duration, for example P30D. Cmdlet objects expose a [timespan]. try { $trial = [System.Xml.XmlConvert]::ToTimeSpan([string]$trialRaw) } catch { $trial = $null } } $extensions = @(foreach ($extension in @(Get-ScoutRestValue -Object $properties -Name 'extensions')) { if ($null -eq $extension) { continue } [pscustomobject]@{ Name = Get-ScoutRestValue -Object $extension -Name 'name' IsEnabled = Get-ScoutRestValue -Object $extension -Name 'isEnabled' AdditionalExtensionProperties = Get-ScoutRestValue -Object $extension -Name 'additionalExtensionProperties' OperationStatus = Get-ScoutRestValue -Object $extension -Name 'operationStatus' } }) [pscustomobject]@{ Id = Get-ScoutRestValue -Object $item -Name 'id' Name = Get-ScoutRestValue -Object $item -Name 'name' Type = Get-ScoutRestValue -Object $item -Name 'type' PricingTier = Get-ScoutRestValue -Object $properties -Name 'pricingTier' SubPlan = Get-ScoutRestValue -Object $properties -Name 'subPlan' FreeTrialRemainingTime = $trial Deprecated = Get-ScoutRestValue -Object $properties -Name 'deprecated' ReplacedBy = @(@(Get-ScoutRestValue -Object $properties -Name 'replacedBy') | Where-Object { $null -ne $_ }) Extensions = $extensions } } } function Get-ScoutSecureScoreRest { param([Parameter(Mandatory)][string]$SubscriptionId) foreach ($item in @(Invoke-ScoutSweepRestList -Label 'Defender secure scores' -Path "/subscriptions/$SubscriptionId/providers/Microsoft.Security/secureScores?api-version=2020-01-01")) { $properties = Get-ScoutRestValue -Object $item -Name 'properties' $score = Get-ScoutRestValue -Object $properties -Name 'score' [pscustomobject]@{ Id = Get-ScoutRestValue -Object $item -Name 'id' Name = Get-ScoutRestValue -Object $item -Name 'name' Type = Get-ScoutRestValue -Object $item -Name 'type' DisplayName = Get-ScoutRestValue -Object $properties -Name 'displayName' MaxScore = Get-ScoutRestValue -Object $score -Name 'max' CurrentScore = Get-ScoutRestValue -Object $score -Name 'current' Percentage = Get-ScoutRestValue -Object $score -Name 'percentage' Weight = Get-ScoutRestValue -Object $properties -Name 'weight' Score = $score } } } function Get-ScoutSecureScoreControlRest { param([Parameter(Mandatory)][string]$SubscriptionId) # $expand=definition adds the assessment definitions each control groups; the Defender # derived-evidence step uses them to attach a control to an unhealthy assessment. $path = "/subscriptions/$SubscriptionId/providers/Microsoft.Security/secureScoreControls?api-version=2020-01-01&`$expand=definition" foreach ($item in @(Invoke-ScoutSweepRestList -Label 'Defender secure score controls' -Path $path)) { $properties = Get-ScoutRestValue -Object $item -Name 'properties' $score = Get-ScoutRestValue -Object $properties -Name 'score' $definition = Get-ScoutRestValue -Object $properties -Name 'definition' $definitionProperties = Get-ScoutRestValue -Object $definition -Name 'properties' $assessmentDefinitions = @(foreach ($link in @(Get-ScoutRestValue -Object $definitionProperties -Name 'assessmentDefinitions')) { if ($null -ne $link) { [pscustomobject]@{ Id = Get-ScoutRestValue -Object $link -Name 'id' } } }) [pscustomobject]@{ Id = Get-ScoutRestValue -Object $item -Name 'id' Name = Get-ScoutRestValue -Object $item -Name 'name' Type = Get-ScoutRestValue -Object $item -Name 'type' DisplayName = Get-ScoutRestValue -Object $properties -Name 'displayName' MaxScore = Get-ScoutRestValue -Object $score -Name 'max' CurrentScore = Get-ScoutRestValue -Object $score -Name 'current' Percentage = Get-ScoutRestValue -Object $score -Name 'percentage' Weight = Get-ScoutRestValue -Object $properties -Name 'weight' HealthyResourceCount = Get-ScoutRestValue -Object $properties -Name 'healthyResourceCount' UnhealthyResourceCount = Get-ScoutRestValue -Object $properties -Name 'unhealthyResourceCount' NotApplicableResourceCount = Get-ScoutRestValue -Object $properties -Name 'notApplicableResourceCount' Score = $score Definition = [pscustomobject]@{ Id = Get-ScoutRestValue -Object $definition -Name 'id' Name = Get-ScoutRestValue -Object $definition -Name 'name' DisplayName = Get-ScoutRestValue -Object $definitionProperties -Name 'displayName' MaxScore = Get-ScoutRestValue -Object $definitionProperties -Name 'maxScore' AssessmentDefinitions = $assessmentDefinitions } } } } function Get-ScoutRegulatoryComplianceStandardRest { param([Parameter(Mandatory)][string]$SubscriptionId) foreach ($item in @(Invoke-ScoutSweepRestList -Label 'Defender regulatory compliance' -Path "/subscriptions/$SubscriptionId/providers/Microsoft.Security/regulatoryComplianceStandards?api-version=2019-01-01-preview")) { $properties = Get-ScoutRestValue -Object $item -Name 'properties' [pscustomobject]@{ Id = Get-ScoutRestValue -Object $item -Name 'id' Name = Get-ScoutRestValue -Object $item -Name 'name' Type = Get-ScoutRestValue -Object $item -Name 'type' State = Get-ScoutRestValue -Object $properties -Name 'state' PassedControls = Get-ScoutRestValue -Object $properties -Name 'passedControls' FailedControls = Get-ScoutRestValue -Object $properties -Name 'failedControls' SkippedControls = Get-ScoutRestValue -Object $properties -Name 'skippedControls' UnsupportedControls = Get-ScoutRestValue -Object $properties -Name 'unsupportedControls' } } } function Get-ScoutDiagnosticSettingRest { param([Parameter(Mandatory)][string]$SubscriptionId) foreach ($item in @(Invoke-ScoutSweepRestList -Label 'Subscription diagnostic settings' -Path "/subscriptions/$SubscriptionId/providers/Microsoft.Insights/diagnosticSettings?api-version=2021-05-01-preview")) { $properties = Get-ScoutRestValue -Object $item -Name 'properties' $logs = @(foreach ($log in @(Get-ScoutRestValue -Object $properties -Name 'logs')) { if ($null -eq $log) { continue } $retention = Get-ScoutRestValue -Object $log -Name 'retentionPolicy' [pscustomobject]@{ Category = Get-ScoutRestValue -Object $log -Name 'category' CategoryGroup = Get-ScoutRestValue -Object $log -Name 'categoryGroup' Enabled = Get-ScoutRestValue -Object $log -Name 'enabled' # Subscription settings carry no retention policy; give consumers the # same (disabled, 0 days) shape the cmdlet returns. RetentionPolicy = if ($null -ne $retention) { [pscustomobject]@{ Enabled = Get-ScoutRestValue -Object $retention -Name 'enabled'; Days = Get-ScoutRestValue -Object $retention -Name 'days' } } else { [pscustomobject]@{ Enabled = $false; Days = 0 } } } }) [pscustomobject]@{ Id = Get-ScoutRestValue -Object $item -Name 'id' Name = Get-ScoutRestValue -Object $item -Name 'name' Type = Get-ScoutRestValue -Object $item -Name 'type' StorageAccountId = Get-ScoutRestValue -Object $properties -Name 'storageAccountId' ServiceBusRuleId = Get-ScoutRestValue -Object $properties -Name 'serviceBusRuleId' EventHubName = Get-ScoutRestValue -Object $properties -Name 'eventHubName' EventHubAuthorizationRuleId = Get-ScoutRestValue -Object $properties -Name 'eventHubAuthorizationRuleId' WorkspaceId = Get-ScoutRestValue -Object $properties -Name 'workspaceId' MarketplacePartnerId = Get-ScoutRestValue -Object $properties -Name 'marketplacePartnerId' Logs = $logs ResourceId = "/subscriptions/$SubscriptionId" } } } function Get-ScoutPolicyStateRest { param([Parameter(Mandatory)][string]$SubscriptionId) $path = "/subscriptions/$SubscriptionId/providers/Microsoft.PolicyInsights/policyStates/latest/queryResults?api-version=2019-10-01" foreach ($item in @(Invoke-ScoutSweepRestList -Label 'Policy states' -Method POST -Path $path)) { # The REST row is flat camelCase (policyAssignmentName, complianceState, ...); the # Get-AzPolicyState object carries the same names in PascalCase. @odata.* annotations # are transport metadata and are dropped. $row = [ordered]@{} foreach ($property in $item.PSObject.Properties) { if ($property.Name.StartsWith('@')) { continue } $name = $property.Name.Substring(0, 1).ToUpperInvariant() + $property.Name.Substring(1) $row[$name] = $property.Value } [pscustomobject]$row } } # Detect the optional cmdlets once per sweep (not per subscription) and warn once. $optionalCommands = @( [pscustomobject]@{ Command = 'Get-AzSecurityAlert'; Module = 'Az.Security'; Dataset = 'DefenderAlerts' } [pscustomobject]@{ Command = 'Get-AzSecurityAssessment'; Module = 'Az.Security'; Dataset = 'DefenderAssessments' } [pscustomobject]@{ Command = 'Get-AzSecurityPricing'; Module = 'Az.Security'; Dataset = 'DefenderPricing' } [pscustomobject]@{ Command = 'Get-AzSecuritySecureScore'; Module = 'Az.Security'; Dataset = 'DefenderSecureScores' } [pscustomobject]@{ Command = 'Get-AzSecuritySecureScoreControl'; Module = 'Az.Security'; Dataset = 'DefenderSecureScoreControls' } [pscustomobject]@{ Command = 'Get-AzRegulatoryComplianceStandard'; Module = 'Az.Security'; Dataset = 'DefenderRegulatoryStandards' } [pscustomobject]@{ Command = 'Get-AzDiagnosticSetting'; Module = 'Az.Monitor'; Dataset = 'SubscriptionDiagnosticSettings' } [pscustomobject]@{ Command = 'Get-AzPolicyState'; Module = 'Az.PolicyInsights'; Dataset = 'PolicyComplianceStates' } ) $cmdAvailable = @{} foreach ($optionalCommand in $optionalCommands) { $cmdAvailable[$optionalCommand.Command] = $null -ne (Get-Command -Name $optionalCommand.Command -ErrorAction SilentlyContinue) } $missingCommands = @($optionalCommands | Where-Object { -not $cmdAvailable[$_.Command] }) if ($missingCommands.Count -gt 0) { $missingSummary = @(foreach ($group in ($missingCommands | Group-Object -Property Module)) { '{0} (datasets: {1})' -f $group.Name, (@($group.Group | ForEach-Object { $_.Dataset }) -join ', ') }) -join '; ' Write-Warning ("Get-ScoutSubscriptionSecurityPolicySweep: optional Azure module(s) not installed: $missingSummary. " + 'The same data is read read-only through ARM REST (Invoke-AzRestMethod) instead, so no dataset is lost. ' + 'Install the module(s) to use the native cmdlets.') } $originalContext = try { Get-AzContext -ErrorAction SilentlyContinue } catch { $null } function Get-ScoutDefenderAssessmentPrefetch { param( [Parameter(Mandatory)][AllowEmptyCollection()][string[]]$SubscriptionIds, [string]$TenantId ) # Returns subscriptionId -> raw REST assessment rows for every subscription whose list # call succeeded. A subscription that is missing here is read by the per-subscription # pass exactly as before, so a failed or unavailable prefetch costs nothing but time. $prefetched = @{} if ($SubscriptionIds.Count -lt 2) { return $prefetched } try { $armUrl = 'https://management.azure.com/' $context = Get-AzContext -ErrorAction SilentlyContinue if ($null -ne $context -and $null -ne $context.PSObject.Properties['Environment'] -and $null -ne $context.Environment -and $null -ne $context.Environment.PSObject.Properties['ResourceManagerUrl'] -and -not [string]::IsNullOrWhiteSpace([string]$context.Environment.ResourceManagerUrl)) { $armUrl = [string]$context.Environment.ResourceManagerUrl } $tokenParams = @{ ResourceUrl = $armUrl; AsSecureString = $true; ErrorAction = 'Stop'; WarningAction = 'SilentlyContinue' } if (-not [string]::IsNullOrWhiteSpace($TenantId)) { $tokenParams['TenantId'] = $TenantId } $token = Get-AzAccessToken @tokenParams $authorization = 'Bearer ' + ($token.Token | ConvertFrom-SecureString -AsPlainText) } catch { Write-Verbose "Get-ScoutSubscriptionSecurityPolicySweep: assessment prefetch skipped, no ARM token: $($_.Exception.Message)" return $prefetched } $baseUrl = $armUrl.TrimEnd('/') $timer = [System.Diagnostics.Stopwatch]::StartNew() try { $SubscriptionIds | ForEach-Object -ThrottleLimit 8 -Parallel { $subscriptionId = $_ $rows = [System.Collections.Generic.List[object]]::new() $uri = "$using:baseUrl/subscriptions/$subscriptionId/providers/Microsoft.Security/assessments?api-version=2021-06-01" try { do { # A single 502 from Defender used to send the whole subscription to the # slow per-subscription pass (about a minute on a live tenant). Retry a # transient answer twice before giving up on the prefetch. $attempt = 0 while ($true) { try { $response = Invoke-RestMethod -Uri $uri -Method GET -Headers @{ Authorization = $using:authorization } -TimeoutSec 120 -ErrorAction Stop break } catch { $attempt++ $statusCode = 0 try { $statusCode = [int]$_.Exception.Response.StatusCode } catch { $statusCode = 0 } $transient = $statusCode -in @(408, 429, 500, 502, 503, 504) -or $_.Exception -is [System.Threading.Tasks.TaskCanceledException] -or [string]$_.Exception.Message -match '(?i)timed out|Bad Gateway|Service Unavailable|Gateway Timeout' if (-not $transient -or $attempt -ge 3) { throw } Start-Sleep -Milliseconds (500 * $attempt) } } $uri = $null if ($null -ne $response) { if ($response.PSObject.Properties.Name -contains 'value') { foreach ($row in @($response.value)) { if ($null -ne $row) { $rows.Add($row) } } } if ($response.PSObject.Properties.Name -contains 'nextLink') { $uri = [string]$response.nextLink } } } while (-not [string]::IsNullOrWhiteSpace($uri)) [pscustomobject]@{ SubscriptionId = $subscriptionId; Rows = $rows.ToArray(); Error = '' } } catch { [pscustomobject]@{ SubscriptionId = $subscriptionId; Rows = @(); Error = [string]$_.Exception.Message } } } | ForEach-Object { if ([string]::IsNullOrEmpty($_.Error)) { $prefetched[[string]$_.SubscriptionId] = @($_.Rows) } else { Write-Verbose "Get-ScoutSubscriptionSecurityPolicySweep: assessment prefetch failed for one subscription, it will be read in the per-subscription pass: $($_.Error)" } } } catch { Write-Verbose "Get-ScoutSubscriptionSecurityPolicySweep: assessment prefetch unavailable: $($_.Exception.Message)" } Write-Verbose ('Get-ScoutSubscriptionSecurityPolicySweep: assessment prefetch read {0} of {1} subscription(s) in {2} ms' -f $prefetched.Count, $SubscriptionIds.Count, $timer.ElapsedMilliseconds) return $prefetched } function Get-ScoutPolicyStatePrefetch { <# Get-AzPolicyState is the slowest call in this sweep (about 4.4 s per subscription on a live tenant, 3 minutes across 42 subscriptions, strictly one after another). Run the SAME cmdlet for up to 8 subscriptions at once. The objects are the cmdlet's own PolicyState objects, so nothing downstream sees a different shape: on nine live subscriptions the parallel and serial results serialised byte-for-byte identical. A subscription that fails here is simply absent and goes through the ordinary per-subscription call, which owns retries, classification and health. #> param([string[]]$SubscriptionIds) $prefetched = @{} if (@($SubscriptionIds).Count -lt 2) { return $prefetched } $policyCommand = Get-Command Get-AzPolicyState -ErrorAction SilentlyContinue # A stub or mock stands in for the cmdlet in tests: every call must go through it. The real # command is a compiled cmdlet in older Az.PolicyInsights and a generated FUNCTION in 2.x, # so accept either as long as it comes from Az.PolicyInsights. (3.18.2 checked for a # cmdlet only, so on Az.PolicyInsights 2.0 the prefetch silently never ran.) $isRealPolicyCommand = $null -ne $policyCommand -and ( $policyCommand.CommandType -eq 'Cmdlet' -or ($policyCommand.CommandType -eq 'Function' -and [string]$policyCommand.ModuleName -eq 'Az.PolicyInsights')) if (-not $isRealPolicyCommand) { if ($null -eq $policyCommand) { Write-Verbose 'Get-ScoutSubscriptionSecurityPolicySweep: policy-state prefetch skipped, the Az.PolicyInsights module is not installed; every subscription is read through ARM REST in the per-subscription pass.' } else { Write-Verbose 'Get-ScoutSubscriptionSecurityPolicySweep: policy-state prefetch skipped, Get-AzPolicyState is not the Az.PolicyInsights command (a test stub or wrapper); every subscription is read in the per-subscription pass.' } return $prefetched } $timer = [System.Diagnostics.Stopwatch]::StartNew() try { $SubscriptionIds | ForEach-Object -ThrottleLimit 8 -Parallel { $subscriptionId = $_ try { [pscustomobject]@{ SubscriptionId = $subscriptionId; Rows = @(Get-AzPolicyState -SubscriptionId $subscriptionId -ErrorAction Stop -WarningAction SilentlyContinue); Error = '' } } catch { [pscustomobject]@{ SubscriptionId = $subscriptionId; Rows = @(); Error = [string]$_.Exception.Message } } } | ForEach-Object { if ([string]::IsNullOrEmpty($_.Error)) { $prefetched[[string]$_.SubscriptionId] = @($_.Rows) } else { Write-Verbose "Get-ScoutSubscriptionSecurityPolicySweep: policy-state prefetch failed for one subscription, it will be read in the per-subscription pass: $($_.Error)" } } } catch { Write-Verbose "Get-ScoutSubscriptionSecurityPolicySweep: policy-state prefetch unavailable: $($_.Exception.Message)" } Write-Verbose ('Get-ScoutSubscriptionSecurityPolicySweep: policy-state prefetch read {0} of {1} subscription(s) in {2} ms' -f $prefetched.Count, @($SubscriptionIds).Count, $timer.ElapsedMilliseconds) return $prefetched } function Get-ScoutSweepCallPrefetch { <# The rest of the per-subscription reads (alerts, pricing, secure score and controls, regulatory standards, subscription diagnostic settings) used to run strictly one subscription after another, each after a Set-AzContext switch: about 11 s per subscription on a live tenant, most of it Get-AzDiagnosticSetting (4.3 s each). Each read here runs concurrently (8 at a time) against that subscription's own cached Az context passed as -DefaultProfile, so the session's context never changes. On nine live subscriptions all 54 results serialised identical to the serial loop, 104 s -> 47 s. Only clean successes are kept; anything else is read by the ordinary per-subscription pass, which owns retries, provider/tier classification, the alerts REST fallback and health. #> param([object[]]$Entries) $prefetched = @{} if (@($Entries).Count -lt 2) { return $prefetched } $securityCommand = Get-Command Get-AzSecurityPricing -ErrorAction SilentlyContinue if ($null -eq $securityCommand) { Write-Verbose 'Get-ScoutSubscriptionSecurityPolicySweep: per-subscription prefetch skipped, the Az.Security module is not installed; Defender datasets are read through ARM REST in the per-subscription pass.' return $prefetched } if ($securityCommand.CommandType -ne 'Cmdlet') { Write-Verbose 'Get-ScoutSubscriptionSecurityPolicySweep: per-subscription prefetch skipped, Get-AzSecurityPricing is installed but is not a compiled Az.Security cmdlet (a test stub or wrapper function).' return $prefetched } $datasetCommands = @{ DefenderAlerts = 'Get-AzSecurityAlert' DefenderPricing = 'Get-AzSecurityPricing' DefenderSecureScores = 'Get-AzSecuritySecureScore' DefenderSecureScoreControls = 'Get-AzSecuritySecureScoreControl' DefenderRegulatoryStandards = 'Get-AzRegulatoryComplianceStandard' SubscriptionDiagnosticSettings = 'Get-AzDiagnosticSetting' } $cached = @(Get-AzContext -ListAvailable -ErrorAction SilentlyContinue) $work = [System.Collections.Generic.List[object]]::new() foreach ($entry in $Entries) { $context = $cached | Where-Object { $null -ne $_.Subscription -and [string]$_.Subscription.Id -eq $entry.Id -and (-not $entry.TenantId -or [string]$_.Tenant.Id -eq $entry.TenantId) -and ($null -eq $originalContext -or $null -eq $originalContext.Account -or [string]$_.Account.Id -eq [string]$originalContext.Account.Id) } | Select-Object -First 1 if ($null -eq $context) { continue } foreach ($dataset in @('DefenderAlerts', 'DefenderPricing', 'DefenderSecureScores', 'DefenderSecureScoreControls', 'DefenderRegulatoryStandards', 'SubscriptionDiagnosticSettings')) { # A dataset whose cmdlet is not installed is read through REST by the ordinary pass. if (-not $cmdAvailable[$datasetCommands[$dataset]]) { continue } $work.Add([pscustomobject]@{ SubscriptionId = $entry.Id; Dataset = $dataset; Context = $context }) } } if ($work.Count -eq 0) { return $prefetched } $timer = [System.Diagnostics.Stopwatch]::StartNew() try { $work | ForEach-Object -ThrottleLimit 8 -Parallel { $item = $_ $azProfile = $item.Context $readErrors = @() try { $rows = switch ($item.Dataset) { 'DefenderAlerts' { @(Get-AzSecurityAlert -DefaultProfile $azProfile -ErrorAction Stop -WarningAction SilentlyContinue) } 'DefenderPricing' { @(Get-AzSecurityPricing -DefaultProfile $azProfile -ErrorAction SilentlyContinue -ErrorVariable +readErrors -WarningAction SilentlyContinue) } 'DefenderSecureScores' { @(Get-AzSecuritySecureScore -DefaultProfile $azProfile -ErrorAction Stop -WarningAction SilentlyContinue) } 'DefenderSecureScoreControls' { @(Get-AzSecuritySecureScoreControl -DefaultProfile $azProfile -ErrorAction Stop -WarningAction SilentlyContinue) } 'DefenderRegulatoryStandards' { @(Get-AzRegulatoryComplianceStandard -DefaultProfile $azProfile -ErrorAction Stop -WarningAction SilentlyContinue) } # Scoped by -ResourceId; Az.Monitor ignores -DefaultProfile and says so. 'SubscriptionDiagnosticSettings' { @(Get-AzDiagnosticSetting -ResourceId "/subscriptions/$($item.SubscriptionId)" -ErrorAction Stop -WarningAction SilentlyContinue) } } if (@($readErrors).Count -gt 0) { [pscustomobject]@{ Key = "$($item.SubscriptionId)|$($item.Dataset)"; Ok = $false; Rows = @() } } else { [pscustomobject]@{ Key = "$($item.SubscriptionId)|$($item.Dataset)"; Ok = $true; Rows = @($rows) } } } catch { [pscustomobject]@{ Key = "$($item.SubscriptionId)|$($item.Dataset)"; Ok = $false; Rows = @() } } } | ForEach-Object { if ($_.Ok) { $prefetched[[string]$_.Key] = @($_.Rows) } } } catch { Write-Verbose "Get-ScoutSubscriptionSecurityPolicySweep: per-subscription prefetch unavailable: $($_.Exception.Message)" } Write-Verbose ('Get-ScoutSubscriptionSecurityPolicySweep: per-subscription prefetch read {0} of {1} dataset(s) in {2} ms' -f $prefetched.Count, $work.Count, $timer.ElapsedMilliseconds) return $prefetched } $prefetchedCalls = Get-ScoutSweepCallPrefetch -Entries @(foreach ($candidate in $Subscriptions) { $candidateId = if ($null -ne $candidate) { $candidate.PSObject.Properties['id'] } else { $null } if ($null -eq $candidateId -or [string]::IsNullOrWhiteSpace([string]$candidateId.Value)) { continue } $candidateTenant = $candidate.PSObject.Properties['tenantId'] [pscustomobject]@{ Id = [string]$candidateId.Value TenantId = if ($null -ne $candidateTenant -and -not [string]::IsNullOrWhiteSpace([string]$candidateTenant.Value)) { [string]$candidateTenant.Value } elseif ($null -ne $originalContext -and $null -ne $originalContext.PSObject.Properties['Tenant'] -and $null -ne $originalContext.Tenant) { [string]$originalContext.Tenant.Id } else { '' } } }) $prefetchedPolicyStates = Get-ScoutPolicyStatePrefetch -SubscriptionIds @(foreach ($candidate in $Subscriptions) { $candidateId = if ($null -ne $candidate) { $candidate.PSObject.Properties['id'] } else { $null } if ($null -ne $candidateId -and -not [string]::IsNullOrWhiteSpace([string]$candidateId.Value)) { [string]$candidateId.Value } }) $prefetchedAssessments = @{} if ($PrefetchAssessments) { $prefetchIds = @(foreach ($candidate in $Subscriptions) { $candidateId = if ($null -ne $candidate) { $candidate.PSObject.Properties['id'] } else { $null } if ($null -ne $candidateId -and -not [string]::IsNullOrWhiteSpace([string]$candidateId.Value)) { [string]$candidateId.Value } }) $prefetchTenantId = if ($null -ne $originalContext -and $null -ne $originalContext.PSObject.Properties['Tenant'] -and $null -ne $originalContext.Tenant -and $null -ne $originalContext.Tenant.PSObject.Properties['Id']) { [string]$originalContext.Tenant.Id } else { '' } $prefetchedAssessments = Get-ScoutDefenderAssessmentPrefetch -SubscriptionIds $prefetchIds -TenantId $prefetchTenantId } $sweepCanReport = $null -ne (Get-Command Update-ScoutRunProgressCount -ErrorAction SilentlyContinue) $sweepTotal = @($Subscriptions).Count $sweepDone = 0 $results = try { foreach ($subscription in $Subscriptions) { if ($sweepCanReport) { Update-ScoutRunProgressCount -Done $sweepDone -Total $sweepTotal -Unit 'subscriptions' } $sweepDone++ $idProperty = $subscription.PSObject.Properties['id'] if ($null -eq $idProperty -or [string]::IsNullOrWhiteSpace([string] $idProperty.Value)) { Write-Warning 'Get-ScoutSubscriptionSecurityPolicySweep: skipping a subscription object without an id.' continue } $subscriptionId = [string] $idProperty.Value $tenantIdProperty = $subscription.PSObject.Properties['tenantId'] $subscriptionTenantId = if ($null -ne $tenantIdProperty -and -not [string]::IsNullOrWhiteSpace([string] $tenantIdProperty.Value)) { [string] $tenantIdProperty.Value } elseif ($null -ne $originalContext -and $null -ne $originalContext.PSObject.Properties['Tenant'] -and $null -ne $originalContext.Tenant -and $null -ne $originalContext.Tenant.PSObject.Properties['Id']) { [string] $originalContext.Tenant.Id } else { $null } $nameProperty = $subscription.PSObject.Properties['name'] $subscriptionName = if ( $null -ne $nameProperty -and -not [string]::IsNullOrWhiteSpace([string] $nameProperty.Value) ) { [string] $nameProperty.Value } else { $subscriptionId } $emptyData = [ordered]@{ DefenderAlerts = @() DefenderAssessments = @() DefenderPricing = @() DefenderSecureScores = @() DefenderSecureScoreControls = @() DefenderRegulatoryStandards = @() SubscriptionDiagnosticSettings = @() PolicyComplianceStates = @() } $statuses = [ordered]@{} $collectionErrors = [System.Collections.Generic.List[object]]::new() # Every dataset for this subscription already read by the concurrent prefetches: no # live call follows, so the one-second context switch is skipped as well. $hasScores = $prefetchedCalls.ContainsKey("$subscriptionId|DefenderSecureScores") -and @($prefetchedCalls["$subscriptionId|DefenderSecureScores"]).Count -gt 0 $allPrefetched = $prefetchedAssessments.ContainsKey($subscriptionId) -and $prefetchedPolicyStates.ContainsKey($subscriptionId) -and @('DefenderAlerts', 'DefenderPricing', 'DefenderSecureScores', 'DefenderRegulatoryStandards', 'SubscriptionDiagnosticSettings' | Where-Object { -not $prefetchedCalls.ContainsKey("$subscriptionId|$_") }).Count -eq 0 -and (-not $hasScores -or $prefetchedCalls.ContainsKey("$subscriptionId|DefenderSecureScoreControls")) try { if (-not $allPrefetched) { $contextParams = @{ Subscription = $subscriptionId; ErrorAction = 'Stop' } if ($subscriptionTenantId) { $contextParams['Tenant'] = $subscriptionTenantId } Set-AzContext @contextParams | Out-Null } } catch { $message = $_.Exception.Message Write-Warning "Get-ScoutSubscriptionSecurityPolicySweep: could not enter subscription context '$subscriptionName': $message" foreach ($datasetName in $emptyData.Keys) { $statuses[$datasetName] = 'Skipped' } $collectionErrors.Add([pscustomobject]@{ Dataset = 'Context' Message = $message }) [pscustomobject]@{ id = "/subscriptions/$subscriptionId/providers/AzureScout/securityPolicySweep/default" name = 'default' type = 'AZSC/Subscription/SecurityPolicySweep' subscriptionId = $subscriptionId subscriptionName = $subscriptionName properties = [pscustomobject]@{ DefenderAlerts = @() DefenderAssessments = @() DefenderPricing = @() DefenderSecureScores = @() DefenderSecureScoreControls = @() DefenderRegulatoryStandards = @() SubscriptionDiagnosticSettings = @() PolicyComplianceStates = @() CollectionStatus = [pscustomobject] $statuses CollectionErrors = @($collectionErrors) } } continue } $queries = [ordered]@{} $queries.DefenderAlerts = Invoke-ScoutSweepDataset ` -Dataset 'DefenderAlerts' ` -SubscriptionName $subscriptionName ` -MaxAttempts 5 ` -ProviderRegistrationIsUnavailable ` -Operation { if ($prefetchedCalls.ContainsKey("$subscriptionId|DefenderAlerts")) { @($prefetchedCalls["$subscriptionId|DefenderAlerts"]) } else { Get-ScoutDefenderAlerts -SubscriptionId $subscriptionId } } $queries.DefenderAssessments = Invoke-ScoutSweepDataset ` -Dataset 'DefenderAssessments' ` -SubscriptionName $subscriptionName ` -MaxAttempts 5 ` -ProviderRegistrationIsUnavailable ` -Operation { if ($prefetchedAssessments.ContainsKey($subscriptionId)) { @(foreach ($prefetchedRow in @($prefetchedAssessments[$subscriptionId])) { if ($null -ne $prefetchedRow) { ConvertFrom-ScoutDefenderAssessmentRestRow -Assessment $prefetchedRow } }) } else { Get-ScoutDefenderAssessments -SubscriptionId $subscriptionId } } $queries.DefenderPricing = Invoke-ScoutDefenderPricingDataset -SubscriptionName $subscriptionName -SubscriptionId $subscriptionId ` -Prefetched $(if ($prefetchedCalls.ContainsKey("$subscriptionId|DefenderPricing")) { , @($prefetchedCalls["$subscriptionId|DefenderPricing"]) } else { $null }) $queries.DefenderSecureScores = Invoke-ScoutSweepDataset ` -Dataset 'DefenderSecureScores' ` -SubscriptionName $subscriptionName ` -Operation { if ($prefetchedCalls.ContainsKey("$subscriptionId|DefenderSecureScores")) { @($prefetchedCalls["$subscriptionId|DefenderSecureScores"]) } elseif ($cmdAvailable['Get-AzSecuritySecureScore']) { Get-AzSecuritySecureScore -ErrorAction Stop } else { Get-ScoutSecureScoreRest -SubscriptionId $subscriptionId } } if (@($queries.DefenderSecureScores.Data).Count -gt 0) { $queries.DefenderSecureScoreControls = Invoke-ScoutSweepDataset ` -Dataset 'DefenderSecureScoreControls' ` -SubscriptionName $subscriptionName ` -Operation { if ($prefetchedCalls.ContainsKey("$subscriptionId|DefenderSecureScoreControls")) { @($prefetchedCalls["$subscriptionId|DefenderSecureScoreControls"]) } elseif ($cmdAvailable['Get-AzSecuritySecureScoreControl']) { Get-AzSecuritySecureScoreControl -ErrorAction Stop } else { Get-ScoutSecureScoreControlRest -SubscriptionId $subscriptionId } } } else { $queries.DefenderSecureScoreControls = [pscustomobject]@{ Data = @() Status = 'Skipped' Error = $null } } $queries.DefenderRegulatoryStandards = Invoke-ScoutSweepDataset -Dataset 'DefenderRegulatoryStandards' -SubscriptionName $subscriptionName -ProviderRegistrationIsUnavailable -Operation { if ($prefetchedCalls.ContainsKey("$subscriptionId|DefenderRegulatoryStandards")) { @($prefetchedCalls["$subscriptionId|DefenderRegulatoryStandards"]) } elseif ($cmdAvailable['Get-AzRegulatoryComplianceStandard']) { Get-AzRegulatoryComplianceStandard -ErrorAction Stop } else { Get-ScoutRegulatoryComplianceStandardRest -SubscriptionId $subscriptionId } } $resourceId = "/subscriptions/$subscriptionId" $queries.SubscriptionDiagnosticSettings = Invoke-ScoutSweepDataset ` -Dataset 'SubscriptionDiagnosticSettings' ` -SubscriptionName $subscriptionName ` -Operation { if ($prefetchedCalls.ContainsKey("$subscriptionId|SubscriptionDiagnosticSettings")) { @($prefetchedCalls["$subscriptionId|SubscriptionDiagnosticSettings"]) } elseif ($cmdAvailable['Get-AzDiagnosticSetting']) { Get-AzDiagnosticSetting -ResourceId $resourceId -ErrorAction Stop } else { Get-ScoutDiagnosticSettingRest -SubscriptionId $subscriptionId } } $queries.PolicyComplianceStates = Invoke-ScoutSweepDataset ` -Dataset 'PolicyComplianceStates' ` -SubscriptionName $subscriptionName ` -Operation { if ($prefetchedPolicyStates.ContainsKey($subscriptionId)) { @($prefetchedPolicyStates[$subscriptionId]) } elseif ($cmdAvailable['Get-AzPolicyState']) { Get-AzPolicyState -SubscriptionId $subscriptionId -ErrorAction Stop } else { Get-ScoutPolicyStateRest -SubscriptionId $subscriptionId } } $reasons = [ordered]@{} foreach ($datasetName in @($emptyData.Keys)) { $query = $queries[$datasetName] $emptyData[$datasetName] = @($query.Data) $statuses[$datasetName] = $query.Status if ($null -ne $query.Error) { $collectionErrors.Add($query.Error) } $reasonProperty = $query.PSObject.Properties['Reason'] if ($reasonProperty -and -not [string]::IsNullOrWhiteSpace([string]$reasonProperty.Value)) { $reasons[$datasetName] = [string]$reasonProperty.Value } } $operationNames = @{ DefenderAlerts = 'Get-AzSecurityAlert / ARM alerts fallback' DefenderAssessments = 'Get-AzSecurityAssessment / ARM assessments fallback' DefenderPricing = 'Get-AzSecurityPricing' DefenderSecureScores = 'Get-AzSecuritySecureScore' DefenderSecureScoreControls = 'Get-AzSecuritySecureScoreControl' DefenderRegulatoryStandards = 'Get-AzRegulatoryComplianceStandard' SubscriptionDiagnosticSettings = 'Get-AzDiagnosticSetting' PolicyComplianceStates = 'Get-AzPolicyState' } # Say so when the data came from ARM REST because the module is not installed. foreach ($optionalCommand in $optionalCommands) { if (-not $cmdAvailable[$optionalCommand.Command]) { $operationNames[$optionalCommand.Dataset] = 'ARM REST via Invoke-AzRestMethod ({0} not installed)' -f $optionalCommand.Module } } $sourceOperations = @($queries.Keys | ForEach-Object { $datasetName = [string]$_ $queryResult = $queries[$datasetName] [pscustomobject][ordered]@{ Source = 'Azure PowerShell control plane' Dataset = $datasetName SubscriptionId = $subscriptionId Operation = $operationNames[$datasetName] Status = $queryResult.Status Count = @($queryResult.Data).Count Reason = if ($queryResult.Error) { $queryResult.Error.Message } else { $null } } }) [pscustomobject]@{ id = "/subscriptions/$subscriptionId/providers/AzureScout/securityPolicySweep/default" name = 'default' type = 'AZSC/Subscription/SecurityPolicySweep' subscriptionId = $subscriptionId subscriptionName = $subscriptionName properties = [pscustomobject]@{ DefenderAlerts = @($emptyData.DefenderAlerts) DefenderAssessments = @($emptyData.DefenderAssessments) DefenderPricing = @($emptyData.DefenderPricing) DefenderSecureScores = @($emptyData.DefenderSecureScores) DefenderSecureScoreControls = @($emptyData.DefenderSecureScoreControls) DefenderRegulatoryStandards = @($emptyData.DefenderRegulatoryStandards) SubscriptionDiagnosticSettings = @($emptyData.SubscriptionDiagnosticSettings) PolicyComplianceStates = @($emptyData.PolicyComplianceStates) CollectionStatus = [pscustomobject] $statuses CollectionErrors = @($collectionErrors) CollectionReasons = [pscustomobject] $reasons SourceOperations = $sourceOperations } } } } finally { $restoreId = $null if ( $null -ne $originalContext -and $null -ne $originalContext.PSObject.Properties['Subscription'] -and $null -ne $originalContext.Subscription -and $null -ne $originalContext.Subscription.PSObject.Properties['Id'] ) { $restoreId = [string] $originalContext.Subscription.Id } if (-not [string]::IsNullOrWhiteSpace($restoreId)) { try { $restoreParams = @{ Subscription = $restoreId; ErrorAction = 'Stop' } if ($null -ne $originalContext.PSObject.Properties['Tenant'] -and $null -ne $originalContext.Tenant -and $null -ne $originalContext.Tenant.PSObject.Properties['Id'] -and $originalContext.Tenant.Id) { $restoreParams['Tenant'] = $originalContext.Tenant.Id } Set-AzContext @restoreParams | Out-Null } catch { Write-Warning "Get-ScoutSubscriptionSecurityPolicySweep: could not restore subscription context '$restoreId': $($_.Exception.Message)" } } } return @($results) } |