PolicySetup/PolicySetup.ps1

Set-StrictMode -Version Latest
function Install-AzSKOrganizationPolicy
{
    <#
    .SYNOPSIS
    This command is intended to be used by central Organization team to setup Organization specific policies
    .DESCRIPTION
    This command is intended to be used by central Organization team to setup Organization specific policies
 
    #>

    
    [OutputType([String])]
    Param
    (
        [string]
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Default")]
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Custom")]
        [ValidateNotNullOrEmpty()]
        $SubscriptionId,

        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [Parameter(Mandatory = $false, ParameterSetName = "Custom")]
        [string]
        $ResourceGroupLocation = "EastUS",

        [Parameter(Mandatory = $true, ParameterSetName = "Custom")]
        [string]
        $ResourceGroupName,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom")]
        [string]
        $StorageAccountName,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom")]
        [string]
        $AppInsightName,

        [Parameter(Mandatory = $false, ParameterSetName = "Custom")]
        [string]
        $AppInsightLocation = "EastUS",

        [Parameter(Mandatory = $true, ParameterSetName = "Default")]
        [Parameter(Mandatory = $true, ParameterSetName = "Custom")]
        [string]
        $OrgName,

        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [string]
        $DepartmentName,

        [Parameter(Mandatory = $false, ParameterSetName = "Custom")]
        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [string]
        $PolicyFolderPath
    )

    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try 
        {
            $policy = [PolicySetup]::new($SubscriptionId, $PSCmdlet.MyInvocation, $OrgName, $DepartmentName,$ResourceGroupName, $StorageAccountName, $AppInsightName, $AppInsightLocation, $ResourceGroupLocation, $PolicyFolderPath, [Constants]::NewModuleName);
            if ($policy) 
            {
                return $policy.InvokeFunction($policy.InstallPolicy, @($moduleName));
            }
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}


function Update-AzSKOrganizationPolicy
{
    <#
    .SYNOPSIS
    This command is intended to be used by central Organization team to setup Organization specific policies
    .DESCRIPTION
    This command is intended to be used by central Organization team to setup Organization specific policies
 
    #>

    
    [OutputType([String])]
    Param
    (
        [string]
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Default")]
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Custom")]
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Migrate")]
        [ValidateNotNullOrEmpty()]
        $SubscriptionId,

        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [Parameter(Mandatory = $false, ParameterSetName = "Custom")]
        [Parameter(Mandatory = $false, ParameterSetName = "Migrate")]
        [string]
        $ResourceGroupLocation,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom")]
        [Parameter(Mandatory = $false, ParameterSetName = "Migrate")]
        [string]
        $ResourceGroupName,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom")]
        [Parameter(Mandatory = $false, ParameterSetName = "Migrate")]
        [string]
        $StorageAccountName,

        [Parameter(Mandatory = $false, ParameterSetName = "Migrate")]
        [string]
        $AppInsightName,

        [Parameter(Mandatory = $false, ParameterSetName = "Migrate")]
        [string]
        $AppInsightLocation,

        [Parameter(Mandatory = $true, ParameterSetName = "Default")]
        [Parameter(Mandatory = $true, ParameterSetName = "Custom")]
        [Parameter(Mandatory = $true, ParameterSetName = "Migrate")]
        [string]
        $OrgName,

        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [Parameter(Mandatory = $false, ParameterSetName = "Migrate")]
        [string]
        $DepartmentName,

        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [Parameter(Mandatory = $false, ParameterSetName = "Custom")]
        [string]
        $PolicyFolderPath,
        
        [Parameter(Mandatory = $true, ParameterSetName = "Migrate")]
        [switch]
        $Migrate,

        [Parameter(Mandatory = $false, ParameterSetName = "Migrate")]
        [string]
        $MigrationScriptPath
    )

    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try 
        {        
            if($Migrate)
            {                
                $oldPolicy = [PolicySetup]::new($SubscriptionId, $PSCmdlet.MyInvocation, $OrgName, $DepartmentName, $null , $null, $null, $null, $ResourceGroupLocation, $PolicyFolderPath, [Constants]::OldModuleName);
                $computedAppInsightLocation = $AppInsightLocation;
                if([string]::IsNullOrWhiteSpace($computedAppInsightLocation))
                {
                    $computedAppInsightLocation = $oldPolicy.AppInsightLocation;
                }
                $computedRGLocation = $ResourceGroupLocation;
                if([string]::IsNullOrWhiteSpace($computedRGLocation))
                {
                    $computedRGLocation = $oldPolicy.ResourceGroupLocation;
                }
                $newPolicy = [PolicySetup]::new($SubscriptionId, $PSCmdlet.MyInvocation, $OrgName, $DepartmentName, $ResourceGroupName, $StorageAccountName, $AppInsightName, $computedAppInsightLocation, $computedRGLocation, $PolicyFolderPath, [Constants]::NewModuleName);    
                return $newPolicy.InvokeFunction($newPolicy.MigratePolicy, @($oldPolicy));
            }            
            if ($policy) 
            {
                $policy = [PolicySetup]::new($SubscriptionId, $PSCmdlet.MyInvocation, $OrgName, $DepartmentName,$ResourceGroupName,$StorageAccountName,$AppInsightName, $AppInsightLocation, $ResourceGroupLocation, $PolicyFolderPath, [Constants]::NewModuleName);
                $moduleName = [Constants]::NewModuleName
                return $policy.InvokeFunction($policy.InstallPolicy, @($moduleName));
            }
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}