Public/Get-InitiativeScopedResources.ps1
|
Function Get-InitiativeScopedResources{ [CmdletBinding()] param ( [Parameter()] [string] $InitiativeName ) Begin{ $ResourceList=@() try { Write-Verbose "Intializing the function $($MyInvocation.MyCommand.Name)." Write-Verbose "Getting the assignment details for the initiative - $($InitiativeName)." $InitiativeAssignments = Get-AzPolicyAssignment -IncludeDescendent -ErrorAction Stop | Where-Object{$_.Displayname -eq $InitiativeName} } catch { Write-Error "An error occured while getting the assignment details for the initiative - $($InitiativeName)." } } Process{ If($InitiativeAssignments){ Write-Verbose "[$($InitiativeAssignments.Count)] Assignements found for the intiative - $initiativeName." foreach($InitiativeAssignment in $InitiativeAssignments){ $InitiativeAssignmentScope = $InitiativeAssignment.Scope Write-Verbose "Getting the resources from the assingnment scope - $InitiativeAssignmentScope." $ResourceData = Get-AzResource -ResourceGroupName ($($InitiativeAssignmentScope) -split "/")[-1] If($ResourceData){ $ResourceData| ForEach-Object { $ResourceList += [PSCustomObject]@{ Name = $_.Name ResourceGroupName= $_.ResourceGroupName ResourceType = $_.ResourceType ResourceId = $_.ResourceId } } } } if($ResourceList){ Write-Verbose "[$($ResourceList.Count)] Resources found." Return $ResourceList } else { Write-Verbose "[$($ResourceList.Count)] Resources found under the assigned scopes." } } else { Write-verbose "No Assignment found for the policy initiative - $InitiativeName." Write-Warning "No Assignment found for the policy initiative - $InitiativeName." } } End{ Write-Verbose "Successfully completed $($MyInvocation.MyCommand.Name)." } } |