ContinuousCompliance/ContinuousCompliance.ps1

Set-StrictMode -Version Latest
function Install-AzSDKContinuousAssurance 
{
    <#
    .SYNOPSIS
    This command would help in installing Automation Account in your subscription to setup Continous Assurance feature of AzSDK
    .DESCRIPTION
    This command will install an Automation Account (Name: AzSDKContinuousAssurance) which runs security scan on subscription and resource groups which are specified during installation.
    Security scan results will be populated in OMS which is configured during installation. Also detailed logs will be stored in storage account (Name: azsdkyyyyMMddHHmmss format).
     
    .PARAMETER SubscriptionId
        Subscription id in which Automation Account needs to be installed
    .PARAMETER AutomationAccountLocation
        Location of resource group which contains Automation Account. This is optional. Default location is EastUS2.
    .PARAMETER ResourceGroupNames
        Comma separated Application resource group names on which security scan should be performed by Automation Account.
    .PARAMETER OMSWorkspaceId
        Workspace ID of OMS where security scan results will be populated
    .PARAMETER OMSSharedKey
        Shared key of OMS which is used to monitor security scan results
    .PARAMETER AzureADAppName
        Name for the Azure Active Directory(AD) Application that will be created in the subscription for running the runbook
 
    .NOTES
     
 
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true)]
        [string]
        $SubscriptionId ,

        [Parameter(Mandatory = $false)]
        [string]
        $AutomationAccountLocation="EastUS2",

        [Parameter(Mandatory = $true)]
        [string]
        $ResourceGroupNames ,       

        [Parameter(Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSWorkspaceId,

        [Parameter(Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSSharedKey,

        [Parameter(Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $AzureADAppName
    )
    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try 
        {
            $ccAccount = [CCAutomation]::new($SubscriptionId, $PSCmdlet.MyInvocation,`
                $AutomationAccountLocation, $ResourceGroupNames,$OMSWorkspaceId,$OMSSharedKey,`
                $AzureADAppName);

            if ($ccAccount) 
            {
                return $ccAccount.InvokeFunction($ccAccount.InstallAzSDKContinuousAssurance);
            }
            
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Update-AzSDKContinuousAssurance 
{
    <#
    .SYNOPSIS
    This command would help in updating user configurable properties of Continuous Assurance Automation Account in your subscription
    .DESCRIPTION
    This command is helpful if you want to update any of the following properties. 1. App Resource Groups 2. OMS Workspace ID 3. OMS Shared Key
    4. Connection in Run As Account 5. Update/Renew Certificate in Run As Account
 
    .PARAMETER SubscriptionId
        Subscription id in which Automation Account exists
    .PARAMETER ResourceGroupNames
        Comma separated Application resource group names on which security scan should be performed by Automation Account
    .PARAMETER OMSWorkspaceId
        Workspace ID of OMS where security scan results will be populated
    .PARAMETER OMSSharedKey
        Shared key of OMS which is used to monitor security scan results
    .PARAMETER AzureADAppName
        Name for the Azure Active Directory(AD) Application that will be created to update automation account Connection in Run As Account for running the runbook
    .PARAMETER UpdateCertificate
        Switch to update certificate credential for AzureADApp SPN and also upload the certificate to automation account.
    .NOTES
     
 
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true)]
        [string]
        $SubscriptionId ,
        
        [Parameter(Mandatory = $false)]
        [string]
        $ResourceGroupNames ,       

        [Parameter(Mandatory = $false)]
        [string]
        $OMSWorkspaceId,

        [Parameter(Mandatory = $false)]
        [string]
        $OMSSharedKey,

        [Parameter(Mandatory = $false)]
        [string]
        $AzureADAppName,

        [Parameter(Mandatory = $false)]
        [switch]
        $UpdateCertificate
    )
    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
    try 
        {
            $ccAccount = [CCAutomation]::new($SubscriptionId, $PSCmdlet.MyInvocation);

            if ($ccAccount) 
            {
                return $ccAccount.InvokeFunction($ccAccount.UpdateAzSDKContinuousAssurance,@($ResourceGroupNames,$OMSWorkspaceId,$OMSSharedKey,$AzureADAppName,$UpdateCertificate));
            }
            
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Get-AzSDKContinuousAssurance 
{
    <#
    .SYNOPSIS
    This command would help in getting details of Continuous Assurance Automation Account in your subscription
    .DESCRIPTION
 
    .PARAMETER SubscriptionId
        Subscription id in which Automation Account exists
 
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true)]
        [string]
        $SubscriptionId 
    )
    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
    try 
        {
            $ccAccount = [CCAutomation]::new($SubscriptionId, $PSCmdlet.MyInvocation);

            if ($ccAccount) 
            {
                return $ccAccount.InvokeFunction($ccAccount.GetAzSDKContinuousAssurance);
            }
            
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Remove-AzSDKContinuousAssurance 
{
    <#
    .SYNOPSIS
    This command would help in removing resources created by Continuous Assurance installation in your subscription
    .DESCRIPTION
 
    .PARAMETER SubscriptionId
        Subscription id in which Automation Account exists
    .PARAMETER DeleteStorageReports
        Switch to specify whether security scan logs/reports stored in storage account also should be removed permanently.
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true)]
        [string]
        $SubscriptionId ,
        
        [Parameter(Mandatory = $false)]
        [switch]
        $DeleteStorageReports
    )
    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
    try 
        {
            $ccAccount = [CCAutomation]::new($SubscriptionId, $PSCmdlet.MyInvocation);

            if ($ccAccount) 
            {
                return $ccAccount.InvokeFunction($ccAccount.RemoveAzSDKContinuousAssurance,@($DeleteStorageReports));
            }
            
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}