manifests/collectors/Networking/NetworkSecurityGroup.psd1

#
# GENERATED by scripts/ConvertTo-ScoutCollectorDefinition.ps1 from Modules/Public/InventoryModules/Networking/NetworkSecurityGroup.ps1 (AB#5660).
# Field expressions are copied verbatim from the original collector and evaluate in an
# equivalent scope -- see docs/design/decisions/declarative-collectors.md.
# Review before trusting; regenerate rather than hand-patch if the source collector changes.
#
@{
    ResourceTypes = @(
        'microsoft.network/networksecuritygroups'
    )

    ResourceTypeMatching = 'Grouped'

    AdditionalFilter = $null

    FilterPreamble = ''

    RowLoopVariable = '1'

    SetupPreamble = @'
$NSGs = $Resources | Where-Object { $_.TYPE -eq 'microsoft.network/networksecuritygroups' }
    $nic = $Resources | Where-Object {$_.TYPE -eq 'microsoft.network/networkinterfaces'}
    $vmss = $Resources | Where-Object {$_.TYPE -eq 'microsoft.compute/virtualmachinescalesets'}
    $flowlogs = $Resources | Where-Object {$_.TYPE -eq 'microsoft.network/networkwatchers/flowlogs'}
'@


    SetupVariables = @(
        'NSGs'
        'nic'
        'vmss'
        'flowlogs'
    )

    Preamble = @'
$ResUCount = 1
            $sub1 = $SUB | Where-Object { $_.Id -eq $1.subscriptionId }
            $data = $1.PROPERTIES
            $Retired = $Retirements | Where-Object { $_.id -eq $1.id }
            if ($Retired)
                    {
                        $RetiredFeature = foreach ($Retire in $Retired)
                            {
                                $RetiredServiceID = $Unsupported | Where-Object {$_.Id -eq $Retired.ServiceID}
                                $tmp0 = [pscustomobject]@{
                                        'RetiredFeature' = $RetiredServiceID.RetiringFeature
                                        'RetiredDate' = $RetiredServiceID.RetirementDate
                                    }
                                $tmp0
                            }
                        $RetiringFeature = if (@($RetiredFeature.RetiredFeature).count -gt 1) { $RetiredFeature.RetiredFeature | ForEach-Object { $_ + ' ,' } }else { $RetiredFeature.RetiredFeature}
                        $RetiringFeature = [string]$RetiringFeature
                        $RetiringFeature = if ($RetiringFeature -like '* ,*') { $RetiringFeature -replace ".$" }else { $RetiringFeature }
 
                        $RetiringDate = if (@($RetiredFeature.RetiredDate).count -gt 1) { $RetiredFeature.RetiredDate | ForEach-Object { $_ + ' ,' } }else { $RetiredFeature.RetiredDate}
                        $RetiringDate = [string]$RetiringDate
                        $RetiringDate = if ($RetiringDate -like '* ,*') { $RetiringDate -replace ".$" }else { $RetiringDate }
                    }
                else
                    {
                        $RetiringFeature = $null
                        $RetiringDate = $null
                    }
 
            $NSGFlows = foreach ($flow in $flowlogs)
                {
                    if ($flow.properties.targetResourceId -eq $1.id) { $flow }
                }
            $FlowLogsEnabled = if ($NSGFlows.properties.enabled -eq 'true') { $true }else { $false }
            $FlowLogsRetention = if (![string]::IsNullOrEmpty($NSGFlows.properties.retentionPolicy.days)) { $NSGFlows.properties.retentionPolicy.days }else { 'Not Enabled' }
            $FlowLogsStorage = if (![string]::IsNullOrEmpty($NSGFlows.properties.storageId)) { ($NSGFlows.properties.storageId).split('/')[8] }else { 'Not Enabled' }
            $Tags = if (![string]::IsNullOrEmpty($1.tags.psobject.properties)) { $1.tags.psobject.properties }else { '0' }
            $RelatedNics = @()
            $RelatedSubs = @()
 
            if (![string]::IsNullOrEmpty($data.networkInterfaces.id))
                {
                    foreach ($NICID in $data.networkInterfaces.id)
                        {
                            $NICDetails = $nic | Where-Object {$_.id -eq $NICID}
                            if (![string]::IsNullOrEmpty($NICDetails))
                                {
                                    $RelatedNics += ($NICDetails.name + ' ('+$NICDetails.properties.ipconfigurations.properties.privateipaddress+')')
                                }
                            elseif ($NICID -like '*microsoft.compute/virtualmachinescalesets*')
                                {
                                    $RelatedNics += $NICID.split('/')[12]
                                }
                             
                        }
                    $FinalNICs = if ($RelatedNics.count -gt 1) { $RelatedNics | ForEach-Object { $_ + ' ,' } }else { $RelatedNics }
                    $FinalNICs = [string]$FinalNICs
                    $FinalNICs = if ($FinalNICs -like '* ,*') { $FinalNICs -replace ".$" }else { $FinalNICs }
                }
            if (![string]::IsNullOrEmpty($data.Subnets.id))
                {
                    foreach ($SUBID in $data.Subnets.id)
                        {
                            $RelatedSubs += ($SUBID.Split('/')[8] + ' ('+ $SUBID.Split('/')[10] + ')')
                        }
                    $FinalSUBs = if ($RelatedSubs.count -gt 1) { $RelatedSubs | ForEach-Object { $_ + ' ,' } }else { $RelatedSubs }
                    $FinalSUBs = [string]$FinalSUBs
                    $FinalSUBs = if ($FinalSUBs -like '* ,*') { $FinalSUBs -replace ".$" }else { $FinalSUBs }
                }
            elseif ([string]::IsNullOrEmpty($data.Subnets.id) -and $data.networkInterfaces.id -like '*microsoft.compute/virtualmachinescalesets*')
                {
                    $VMSSs = $vmss | Where-Object {$_.properties.virtualmachineprofile.networkprofile.networkinterfaceconfigurations.properties.networksecuritygroup.id -eq $1.id}
                    foreach ($VM in $VMSSs)
                        {
                            $SUBID = $VM.properties.virtualmachineprofile.networkprofile.networkinterfaceconfigurations.properties.ipconfigurations.properties.subnet.id
                            $RelatedSubs += ($SUBID.Split('/')[8] + ' ('+ $SUBID.Split('/')[10] + ')')
                        }
                    $FinalSUBs = if ($RelatedSubs.count -gt 1) { $RelatedSubs | ForEach-Object { $_ + ' ,' } }else { $RelatedSubs }
                    $FinalSUBs = [string]$FinalSUBs
                    $FinalSUBs = if ($FinalSUBs -like '* ,*') { $FinalSUBs -replace ".$" }else { $FinalSUBs }
                }
 
            $SecurityRules = $data.securityRules
            $SecurityRules = if (![string]::IsNullOrEmpty($SecurityRules)) { $SecurityRules }else { '0' }
'@


    AdditionalRowLoops = @(
        @{
            Variable = '2'
            Source = '$SecurityRules'
            Preamble = ''
        }
    )

    TagLoop = @{
        Variable = 'Tag'
        Source = '$Tags'
        Preamble = @'
if (![string]::IsNullOrEmpty($2.properties.sourceAddressPrefixes))
                        {
                            $Source = if (@($2.properties.sourceAddressPrefixes).count -gt 1) { $2.properties.sourceAddressPrefixes | ForEach-Object { $_ + ' ,' } }else { $2.properties.sourceAddressPrefixes }
                            $Source = [string]$Source
                            $Source = if ($Source -like '* ,*') { $Source -replace ".$" }else { $Source }
                        }
                    elseif(![string]::IsNullOrEmpty($2.properties.sourceAddressPrefix))
                        {
                            $Source = if (@($2.properties.sourceAddressPrefix).count -gt 1) { $2.properties.sourceAddressPrefix | ForEach-Object { $_ + ' ,' } }else { $2.properties.sourceAddressPrefix }
                            $Source = [string]$Source
                            $Source = if ($Source -like '* ,*') { $Source -replace ".$" }else { $Source }
                        }
                    else
                        {
                            $Source = ''
                        }
 
                    if (![string]::IsNullOrEmpty($2.properties.sourcePortRanges))
                        {
                            $SourcePort = if (@($2.properties.sourcePortRanges).count -gt 1) { $2.properties.sourcePortRanges | ForEach-Object { $_ + ' ,' } }else { $2.properties.sourcePortRanges }
                            $SourcePort = [string]$SourcePort
                            $SourcePort = if ($SourcePort -like '* ,*') { $SourcePort -replace ".$" }else { $SourcePort }
                        }
                    elseif(![string]::IsNullOrEmpty($2.properties.sourcePortRange))
                        {
                            $SourcePort = if (@($2.properties.sourcePortRange).count -gt 1) { $2.properties.sourcePortRange | ForEach-Object { $_ + ' ,' } }else { $2.properties.sourcePortRange }
                            $SourcePort = [string]$SourcePort
                            $SourcePort = if ($SourcePort -like '* ,*') { $SourcePort -replace ".$" }else { $SourcePort }
                        }
                    else
                        {
                            $SourcePort = ''
                        }
 
                    if (![string]::IsNullOrEmpty($2.properties.destinationAddressPrefixes))
                        {
                            $Destination = if (@($2.properties.destinationAddressPrefixes).count -gt 1) { $2.properties.destinationAddressPrefixes | ForEach-Object { $_ + ' ,' } }else { $2.properties.destinationAddressPrefixes }
                            $Destination = [string]$Destination
                            $Destination = if ($Destination -like '* ,*') { $Destination -replace ".$" }else { $Destination }
                        }
                    elseif(![string]::IsNullOrEmpty($2.properties.destinationAddressPrefix))
                        {
                            $Destination = if (@($2.properties.destinationAddressPrefix).count -gt 1) { $2.properties.destinationAddressPrefix | ForEach-Object { $_ + ' ,' } }else { $2.properties.destinationAddressPrefix }
                            $Destination = [string]$Destination
                            $Destination = if ($Destination -like '* ,*') { $Destination -replace ".$" }else { $Destination }
                        }
                    else
                        {
                            $Destination = ''
                        }
 
                    if (![string]::IsNullOrEmpty($2.properties.destinationPortRanges))
                        {
                            $DestinationPort = if (@($2.properties.destinationPortRanges).count -gt 1) { $2.properties.destinationPortRanges | ForEach-Object { $_ + ' ,' } }else { $2.properties.destinationPortRanges }
                            $DestinationPort = [string]$DestinationPort
                            $DestinationPort = if ($DestinationPort -like '* ,*') { $DestinationPort -replace ".$" }else { $DestinationPort }
                        }
                    elseif(![string]::IsNullOrEmpty($2.properties.destinationPortRange))
                        {
                            $DestinationPort = if (@($2.properties.destinationPortRange).count -gt 1) { $2.properties.destinationPortRange | ForEach-Object { $_ + ' ,' } }else { $2.properties.destinationPortRange }
                            $DestinationPort = [string]$DestinationPort
                            $DestinationPort = if ($DestinationPort -like '* ,*') { $DestinationPort -replace ".$" }else { $DestinationPort }
                        }
                    else
                        {
                            $DestinationPort = ''
                        }
 
                    if (@($data.networkInterfaces).count -eq 0 -and @($data.subnets).count -eq 0)
                        {
                            $Orphaned = $true;
                        } else {
                            $Orphaned = $false;
                        }
'@

    }

    Fields = @(
        @{
            Name = 'ID'
            Expression = '$1.id'
        }
        @{
            Name = 'Subscription'
            Expression = '$sub1.Name'
        }
        @{
            Name = 'Resource Group'
            Expression = '$1.RESOURCEGROUP'
        }
        @{
            Name = 'Name'
            Expression = '$1.NAME'
        }
        @{
            Name = 'Location'
            Expression = '$1.LOCATION'
        }
        @{
            Name = 'Retiring Feature'
            Expression = '$RetiringFeature'
        }
        @{
            Name = 'Retiring Date'
            Expression = '$RetiringDate'
        }
        @{
            Name = 'Orphaned'
            Expression = '$Orphaned'
        }
        @{
            Name = 'Security Rules'
            Expression = '$2.name'
        }
        @{
            Name = 'Direction'
            Expression = '$2.properties.direction'
        }
        @{
            Name = 'Action'
            Expression = '$2.properties.Access'
        }
        @{
            Name = 'Priority'
            Expression = '[string]$2.properties.priority'
        }
        @{
            Name = 'Protocol'
            Expression = '[string]$2.properties.protocol'
        }
        @{
            Name = 'Source'
            Expression = '$Source'
        }
        @{
            Name = 'Source Port'
            Expression = '$SourcePort'
        }
        @{
            Name = 'Destination'
            Expression = '$Destination'
        }
        @{
            Name = 'Destination Port'
            Expression = '$DestinationPort'
        }
        @{
            Name = 'Related NICs'
            Expression = '$FinalNICs'
        }
        @{
            Name = 'Related VNETs and Subnets'
            Expression = '$FinalSUBs'
        }
        @{
            Name = 'Flow Logs Enabled'
            Expression = '$FlowLogsEnabled'
        }
        @{
            Name = 'Flow Logs Retention Days'
            Expression = '$FlowLogsRetention'
        }
        @{
            Name = 'Flow Logs Storage Account'
            Expression = '$FlowLogsStorage'
        }
        @{
            Name = 'Resource U'
            Expression = '$ResUCount'
        }
        @{
            Name = 'Tag Name'
            Expression = '[string]$Tag.Name'
        }
        @{
            Name = 'Tag Value'
            Expression = '[string]$Tag.Value'
        }
    )

    Export = @{
        WorksheetName = 'Network Security Groups'
        TableNamePrefix = 'NSGTable_'
        Columns = @(
            'Subscription'
            'Resource Group'
            'Name'
            'Location'
            'Retiring Feature'
            'Retiring Date'
            'Orphaned'
            'Security Rules'
            'Direction'
            'Action'
            'Priority'
            'Protocol'
            'Source'
            'Source Port'
            'Destination'
            'Destination Port'
            'Related NICs'
            'Related VNETs and Subnets'
            'Flow Logs Enabled'
            'Flow Logs Retention Days'
            'Flow Logs Storage Account'
            'Resource U'
        )
        TagColumns = @(
            'Tag Name'
            'Tag Value'
        )
        TagColumnsBefore = 'Resource U'
        NumberFormat = '0'
        ConditionalText = @(
            'New-ConditionalText TRUE -Range G:G'
            'New-ConditionalText FALSE -Range R:R'
            'New-ConditionalText -Range E2:E100 -ConditionalType ContainsText'
        )
    }

    SourceCollector = 'Modules/Public/InventoryModules/Networking/NetworkSecurityGroup.ps1'
}