core/subscription/Select-MonkeyAzureSubscription.ps1

# Monkey365 - the PowerShell Cloud Security Tool for Azure and Microsoft 365 (copyright 2022) by Juan Garrido
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Function Select-MonkeyAzureSubscription{
    <#
        .SYNOPSIS
 
        .DESCRIPTION
 
        .INPUTS
 
        .OUTPUTS
 
        .EXAMPLE
 
        .NOTES
            Author : Juan Garrido
            Twitter : @tr1ana
            File Name : Select-MonkeyAzureSubscription
            Version : 1.0
 
        .LINK
            https://github.com/silverhack/monkey365
    #>


    Begin{
        #Create Array for subscriptions
        $AllSubscriptions = [System.Collections.Generic.List[System.Management.Automation.PSObject]]::new()
        #Create selected subscriptions and sub vars
        $selected_subscriptions = $sub = $null
        If($null -ne $O365Object.auth_tokens.ResourceManager){
            $sparam = @{
                AuthObject = $O365Object.auth_tokens.ResourceManager
                Endpoint = $O365Object.Environment.ResourceManager
            }
            $_subscriptions = Get-MonkeySubscriptionInfo @sparam
        }
        If($_subscriptions){
            ForEach($_subscription in @($_subscriptions).Where({$null -ne $_})){
                If($null -ne $O365Object.Tenant -and $O365Object.Tenant.psobject.Properties.Item('TenantName')){
                    Write-Information -MessageData ("Subscription was found on {0} Tenant" -f $O365Object.Tenant.TenantName) -InformationAction $O365Object.InformationAction
                }
                ElseIf ($O365Object.psobject.Properties.Item('TenantId')){
                    Write-Information -MessageData ("subscription was found on {0} Tenant" -f $O365Object.tenantId) -InformationAction $O365Object.InformationAction
                }
                Else{
                    Write-Information -MessageData ("Subscription {0} was found" -f $_subscription.DisplayName) -InformationAction $O365Object.InformationAction
                }
                $_subscription | Add-Member -type NoteProperty -name TenantID -value $O365Object.TenantId -Force
                If($null -ne $O365Object.Tenant){
                    If($null -ne $O365Object.Tenant.Psobject.Properties.Item('TenantName')){
                        $_subscription | Add-Member -type NoteProperty -name TenantName -value $O365Object.Tenant.TenantName -Force
                    }
                    ElseIf($null -ne $O365Object.Tenant.Psobject.Properties.Item('displayName')){
                        $_subscription | Add-Member -type NoteProperty -name TenantName -value $O365Object.Tenant.displayName -Force
                    }
                    Else{
                        $msg = @{
                            MessageData = ($message.EntraIDTenantNameError);
                            callStack = (Get-PSCallStack | Select-Object -First 1);
                            logLevel = 'warning';
                            InformationAction = $O365Object.InformationAction;
                            Tags = @('EntraIDTenantNameNotFound');
                        }
                        Write-Warning @msg
                        $_subscription | Add-Member -type NoteProperty -name TenantName -value $null -Force
                    }
                    $_subscription | Add-Member -type NoteProperty -name Tenant -value $O365Object.Tenant -Force
                }
                [void]$AllSubscriptions.Add($_subscription);
            }
        }
        Else{
            $msg = @{
                MessageData = ($message.AzureSubscriptionNotFound -f $O365Object.TenantId);
                callStack = (Get-PSCallStack | Select-Object -First 1);
                logLevel = 'warning';
                InformationAction = $O365Object.InformationAction;
                Tags = @('AzureSubscriptionNotFound');
            }
            Write-Warning @msg
        }
    }
    Process{
        If($AllSubscriptions.Count -gt 0){
            If($AllSubscriptions.Count -eq 1){
                $selected_subscriptions = $AllSubscriptions
            }
            ElseIf($O365Object.initParams.ContainsKey('AllSubscriptions') -and $O365Object.initParams.AllSubscriptions -eq $true){
                $selected_subscriptions = $AllSubscriptions
            }
            ElseIf($O365Object.initParams.ContainsKey('Subscriptions')){
                $selected_subscriptions = @()
                ForEach($subscriptionId in $O365Object.initParams.Subscriptions.Split(' ')){
                    $sub = $AllSubscriptions | Where-Object {$_.subscriptionId -eq $subscriptionId} | Select-Object * -ErrorAction Ignore
                    If($sub){
                        $selected_subscriptions += $sub
                    }
                }
            }
            Else{
                If($PSEdition -eq "Desktop"){
                    $selected_subscriptions = $AllSubscriptions | Out-GridView -Title "Choose a Source Subscription ..." -PassThru
                }
                Else{
                    $selected_subscriptions = Select-MonkeySubscriptionConsole -Subscriptions $AllSubscriptions
                }
            }
        }
    }
    End{
        If($null -ne $selected_subscriptions){
            return $selected_subscriptions
        }
    }
}