SubscriptionSecurity/SubscriptionSecurity.ps1

Set-StrictMode -Version Latest
function Set-AzSDKSubscriptionSecurity 
{
    
    <#
    .SYNOPSIS
    This command would help in setting up the all the critical subscription security packages
 
    .DESCRIPTION
    This command would help in setting up the all the critical subscription security packages
     
    .LINK
    https://aka.ms/azsdkdocs
    #>

    Param(

        [string]
        [Parameter(Mandatory = $true, HelpMessage = "Subscription id for which the security evaluation has to be performed.")]
        [ValidateNotNullOrEmpty()]
        $SubscriptionId,
        
        [string] 
        [Parameter(Mandatory = $false, HelpMessage = "Provide tag names for processing specific policies. Comma seperated values are supported.")]
        $Tags,

        [string]
        [Parameter(Mandatory = $true, HelpMessage = "Provide a security contact email address or addresses separated by a comma. Recommended a mail enabled Security Group with receiving of external emails option turned ON.")]
        $SecurityContactEmails,

        [string]
        [Parameter(Mandatory = $false, HelpMessage = "Provide a security contact international information phone number including the country code (for example, +1-425-1234567)")]
        $SecurityPhoneNumber,

        [string] 
        [Parameter(Mandatory = $false, HelpMessage = "Provide the ResourceGroup on which the AlertPackage has to be configured")]
        $TargetResourceGroup,

        [string] 
        [Parameter(Mandatory = $false, HelpMessage = "Provide the location for alert ResourceGroup")]
        $AlertResourceGroupLocation = "East US",
        
        [switch]
        [Parameter(Mandatory = $false, HelpMessage = "Switch to specify whether to open output folder containing all security evaluation report or not.")]
        $DoNotOpenOutputFolder
    )

    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }

    Process
    {
����    try 
        {
            # Adding all mandatory tags
            $modifiedTags = [string]::Join(",", [ConfigurationManager]::GetAzSdkConfigData().SubscriptionMandatoryTags);
            if(-not [string]::IsNullOrWhiteSpace($Tags))
            {
                $modifiedTags = $modifiedTags + "," +$Tags;
            }

            $subSec = [SubscriptionSecurity]::new($SubscriptionId, $PSCmdlet.MyInvocation, $modifiedTags);
            if ($subSec) 
            {
                return $subSec.InvokeFunction($subSec.SetSubscriptionSecurity, @($SecurityContactEmails, $SecurityPhoneNumber, $TargetResourceGroup, $AlertResourceGroupLocation));
            }
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }          
    }

    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Remove-AzSDKSubscriptionSecurity 
{
    <#
    .SYNOPSIS
    This command would help in cleaning up the AzSDK Subscription Security package for a Subscription
 
    .DESCRIPTION
    This command would help in cleaning up the AzSDK Subscription Security package for a Subscription
     
    .LINK
    https://aka.ms/azsdkdocs
    #>

    Param(

        [string]
        [Parameter(Mandatory = $true, HelpMessage = "Subscription id for which the security evaluation has to be performed.")]
        [ValidateNotNullOrEmpty()]
        $SubscriptionId,

        [string] 
        [Parameter(Mandatory = $true, HelpMessage = "Provide tag names for processing specific policies. Comma seperated values are supported.")]
        $Tags,

        [switch]
        [Parameter(Mandatory = $false, HelpMessage = "Switch to specify whether to delete resource group containing all alerts or not")]
        $DeleteResourceGroup,

        [Parameter(Mandatory = $false, HelpMessage = "Provide the comma seperated values of alert names")]
        [string]
        $AlertNames,
        
        [switch]
        [Parameter(Mandatory = $false, HelpMessage = "Switch to specify whether to open output folder containing all security evaluation report or not.")]
        $DoNotOpenOutputFolder
    )

    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }

    Process
    {
����    try 
        {

            $subSec = [SubscriptionSecurity]::new($SubscriptionId, $PSCmdlet.MyInvocation, $Tags);
            if ($subSec) 
            {
                return $subSec.InvokeFunction($subSec.RemoveSubscriptionSecurity, @([bool] $DeleteResourceGroup, $AlertNames));
            }
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }          
    }

    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}