Private/Initialize-CustomAzVm.ps1

# Will be called in VM
function Global:Initialize-CustomAzVm {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        Initializes the newly created VM
    .DESCRIPTION
        ...
    #>

    param()
    process {
        Write-CustomHost -Message "Checking if NuGet is installed..."
        if (-not (Get-PackageProvider -ListAvailable | Where-Object { $_.Name -eq 'NuGet' })) {
            try {
                Write-CustomHost -Message "Installing NuGet..."
                Install-PackageProvider -Name NuGet -Confirm:$False -Force | Out-Null
            }
            catch [Exception] {
                Write-CustomHost -Message "Error installing NuGet"
                $_.message 
                exit
            }
        }
        # TODO: Create array of necessary modules and switch to foreach, to avoid duplicate code
        Write-CustomHost -Message "Checking if module Cloud.Ready.Software.NAV is installed..."
        if (-not (Get-Module -ListAvailable -Name Cloud.Ready.Software.NAV)) {
            try {
                Write-CustomHost -Message "Installing Module Cloud.Ready.Software.NAV..."
                Install-Module Cloud.Ready.Software.NAV -Scope AllUsers -Confirm:$false -Force -SkipPublisherCheck:$true
            }
            catch [Exception] {
                Write-CustomHost -Message "Error installing Module"
                $_.message 
                exit
            }
        }
        Write-CustomHost -Message "Checking if module Az is installed..."
        if (-not (Get-Module -ListAvailable -Name Az)) {
            try {
                Write-CustomHost -Message "Installing Module Az..."
                Install-Module Az -Scope AllUsers -Confirm:$false -Force -SkipPublisherCheck:$true
            }
            catch [Exception] {
                Write-CustomHost -Message "Error installing Module"
                $_.message 
                exit
            }
        }

        Write-CustomHost -Message "Checking if module AzTable is installed..."
        if (-not (Get-Module -ListAvailable -Name AzTable)) {
            try {
                Write-CustomHost -Message "Installing Module AzTable..."
                Install-Module AzTable -Scope AllUsers -Confirm:$false -Force -SkipPublisherCheck:$true
            }
            catch [Exception] {
                Write-CustomHost -Message "Error installing Module"
                $_.message 
                exit
            }
        }

        Write-CustomHost -Message "Checking if module Invoke-CommandAs is installed..."
        if (-not (Get-Module -ListAvailable -Name Invoke-CommandAs)) {
            try {
                Write-CustomHost -Message "Installing Module Invoke-CommandAs..."
                Install-Module Invoke-CommandAs -Scope AllUsers -Confirm:$false -Force -SkipPublisherCheck:$true
            }
            catch [Exception] {
                Write-CustomHost -Message "Error installing Module"
                $_.message 
                exit
            }
        }
    }
}