AzSDK.psm1

Set-StrictMode -Version Latest
#Import-Module AzureRM.Resources
#Import-Module AzureRM.KeyVault
#Import-Module AzureRM.Sql
#Import-Module AzureRM.Storage
#Import-Module AzureRM.DataLakeAnalytics
#Import-Module AzureRM.DataLakeStore
#Import-Module AzureRM.Network
#Import-Module AzureRM.Compute

. $PSScriptRoot\Framework\Framework.ps1 

@("$PSScriptRoot\SVT", "$PSScriptRoot\AlertMonitoring", "$PSScriptRoot\SubscriptionSecurity", "$PSScriptRoot\ContinuousCompliance") | 
    ForEach-Object{
        (Get-ChildItem -Path $_ -Recurse -File -Include "*.ps1") | 
            ForEach-Object {
                . $_.FullName
            }
    }


#. $PSScriptRoot\SVT\SVT.ps1
#. $PSScriptRoot\AlertMonitoring\OMS.ps1
#. $PSScriptRoot\SubscriptionSecurity\SubscriptionRBAC.ps1

function Get-AzSDKAccessToken 
{
    <#
    .SYNOPSIS
    This command would help in generating the access token from the current login context for the specified resource uri.
    .DESCRIPTION
    This command would help in generating the access token from the current login context for the specified resource uri
     
    .PARAMETER ResourceAppIdURI
        Provide the Resource App ID URI
    .PARAMETER TenantId
        Current logged in user tenant id.
             
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Provide the Resource App ID URI")]
        [string]        
        $ResourceAppIdURI,

        [Parameter(Mandatory = $false, HelpMessage = "Current logged in user tenant id.")]
        [string]
        $TenantId
    )

    [Helpers]::GetAccessToken($ResourceAppIdURI, $TenantId);
}

function Get-AzSDKSupportedResourceTypes 
{
    <#
    .SYNOPSIS
    This command would list all the resource types of Azure Services which are currently supported by AzSDK
    .DESCRIPTION
    This command would list all the resource types of Azure Services which are currently supported by AzSDK
         
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param()

    [SVTMapping]::Mapping | Select-Object -Property ResourceTypeName, ResourceType | Sort-Object -Property ResourceTypeName;
}

function Set-AzSDKPolicySettings{
    <#
    .SYNOPSIS
    This command would help to set online policy store URL.
    .DESCRIPTION
    This command would help to set online policy store URL.
     
    .PARAMETER OnlinePolicyStoreURL
        Provide the online policy URL
    .PARAMETER DisableOnlinePolicy
        Flag to disable online policy.
    .PARAMETER EnableAADAuthForOnlinePolicyStore
        Enables AAD authentication for online policy store.
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $false, ParameterSetName = "UpdatePolicy", HelpMessage = "Provide the Online Policy Store URI")]
        [string]        
        $OnlinePolicyStoreUrl,    

        [Parameter(Mandatory = $false, ParameterSetName = "DisablePolicy", HelpMessage = "Provide the flag to enable online policy")]
        [switch]
        $DisableOnlinePolicy,

        [Parameter(Mandatory = $false, ParameterSetName = "UpdatePolicy", HelpMessage = "Provide the flag to enable auth for online policy")]
        [switch]
        $EnableAADAuthForOnlinePolicyStore
        )
    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            $azsdkSettings = [ConfigurationManager]::GetAzSdkSettings();
            if(-not [string]::IsNullOrWhiteSpace($OnlinePolicyStoreUrl))
            {                
                $azsdkSettings.OnlinePolicyStoreUrl = $OnlinePolicyStoreUrl
            }
            if($DisableOnlinePolicy)
            {                
                $azsdkSettings.UseOnlinePolicyStore = $false
            }
            else
            {
                $azsdkSettings.UseOnlinePolicyStore = $true
            }
            
            if($EnableAADAuthForOnlinePolicyStore)
            {
                $azsdkSettings.EnableAADAuthForOnlinePolicyStore = $true
            }
            else
            {
                $azsdkSettings.EnableAADAuthForOnlinePolicyStore = $false
            }
            
            $azsdkSettings.Update();
            [EventBase]::PublishGenericCustomMessage("Successfully set policy settings");
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Set-AzSDKLocalControlTelemetrySettings{
    <#
    .SYNOPSIS
    This command would help to set local control telemetry settings.
    .DESCRIPTION
    This command would help to set local control telemetry settings.
     
    .PARAMETER LocalControlTelemetryKey
        Provide local telemetry key.
    .PARAMETER EnableLocalControlTelemetry
        Enables local control telemetry.
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Provide the local control telemetry key")]
        [string]        
        $LocalControlTelemetryKey,

        [Parameter(Mandatory = $true, HelpMessage = "Provide the flag to enable local control telemetry")]
        [bool]
        $EnableLocalControlTelemetry
        )
    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            $azsdkSettings = [ConfigurationManager]::GetAzSdkSettings();
            $azsdkSettings.LocalControlTelemetryKey = $LocalControlTelemetryKey
            $azsdkSettings.LocalEnableControlTelemetry = $EnableLocalControlTelemetry
            $azsdkSettings.Update();
            [EventBase]::PublishGenericCustomMessage("Successfully set control telemetry settings");
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Set-AzSDKUsageTelemetryLevel {
    <#
    .SYNOPSIS
    This command would help to set telemetry level.
    .DESCRIPTION
    This command would help to set telemetry level.
     
    .PARAMETER Level
        Provide the telemetry level
    .LINK
    https://aka.ms/azsdkdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Provide the telemetry level")]
        [ValidateSet("None", "Anonymous")]
        [string]
        $Level
        )
    Begin
    {
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            $azsdkSettings = [ConfigurationManager]::GetAzSdkSettings();
            $azsdkSettings.UsageTelemetryLevel = $Level
            $azsdkSettings.Update();
            [EventBase]::PublishGenericCustomMessage("Successfully set usage telemetry level");
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}