Public/Get-SpecTVNamingConvention.ps1

Function Get-SpecTVNamingConvention {
    <#
    .SYNOPSIS
    Generates a TeamViewer device alias based on the computer name and Azure table data.
 
    .DESCRIPTION
    The Get-SpecTVNamingConvention function constructs a TeamViewer device alias for a specified computer. It checks the computer name's prefix and either generates an alias without querying Azure tables or retrieves data from specified Azure tables using a Shared Access Signature (SAS) token. The function supports conditional lookups for device and store information from Azure tables based on the computer name.
 
    .PARAMETER ComputerName
    Specifies the computer name. If not provided, the current machine's name will be used by default.
 
    .PARAMETER TableSASRO
    Specifies the read-only SAS token used for accessing the Azure tables.
 
    .PARAMETER DeviceIndexTable
    Specifies the Azure table name containing device index information.
 
    .PARAMETER StoreTable
    Specifies the Azure table name containing store information.
 
    .PARAMETER StorageAccount
    Specifies the Azure storage account name where the tables reside.
 
    .EXAMPLE
    Get-SpecTVNamingConvention -ComputerName "AUC-12345" -TableSASRO "<SAS token>" -DeviceIndexTable "DeviceIndex" -StoreTable "StoreData" -StorageAccount "myStorageAccount"
 
    This command generates a TeamViewer alias for the computer "AUC-12345" without querying Azure tables, as the name matches a predefined prefix.
 
    .EXAMPLE
    Get-SpecTVNamingConvention -TableSASRO "<SAS token>" -DeviceIndexTable "DeviceIndex" -StoreTable "StoreData" -StorageAccount "myStorageAccount"
 
    This command generates a TeamViewer alias for the current machine, querying the "DeviceIndex" and "StoreData" tables using the provided SAS token.
 
    .NOTES
    Author: owen.heaume
    Version: 1.0.0 - initial release
    #>


    [cmdletbinding()]

    Param (
        [string]$ComputerName = $env:COMPUTERNAME,

        [string]$TableSASRO,

        [string]$DeviceIndexTable,

        [string]$StoreTable,

        [string]$StorageAccount
    )

    Begin {
        $tableError = $false

        #If device name starts with... then don't do table calls.
        if ($computername -match '^AUC-|^NZC-|^AURR-|^NZRR-|^AUSC-|^NZSC-|^AURT-|^NZRT-|^HKC-|^HKSC-') {
            Write-Host "No need to make Azure table calls for this host name: $Computername" -ForegroundColor DarkGray
        } else {
            if ($DeviceIndexTable) {
                # 1. get Device Index data
                Write-Host "Getting table data from $deviceIndexTable" -ForegroundColor DarkCyan

                $params = @{
                    value          = $ComputerName
                    Key            = 'HostName'
                    tableName      = $DeviceIndexTable
                    StorageAccount = $StorageAccount
                    SASToken       = $TableSASRO
                }
                try {
                    $deviceData = Get-SpecAzTableRowUsingSAS @params -ea Stop
                } catch {
                    Write-Error "Table $DeviceIndexTable lookup failed: $_"
                    $tableError = $true
                }
            }

            if (!$tableError) {
                if ($StoreTable) {
                    # 2. get Store data
                    Write-Host "Getting table data from $StoreTable" -ForegroundColor DarkCyan

                    $params = @{
                        value          = $deviceData.EPOS
                        Key            = 'RowKey'
                        tableName      = $StoreTable
                        StorageAccount = $StorageAccount
                        SASToken       = $TableSASRO
                    }
                    try {
                        $storeData = Get-SpecAzTableRowUsingSAS @params -ea Stop
                    } catch {
                        Write-Error "Table $StoreTable lookup failed: $_"
                        $tableError = $true
                    }
                }
            }
        }
    }

    Process {
        if ($tableError) { return $false }

        # Determine which tables and naming convention are required based on the device's computername
        switch -Regex ($ComputerName) {
            '^AUR-|^NZR-|^AUQA-|^NZQA-' {
                Write-Host 'Computername starts with AUR, NZR, AUQA or NZQA' -ForegroundColor DarkGray

                # 1. Construct Team Viewer name
                Write-Host "`nGenerating Team Viewer device alias" -ForegroundColor DarkCyan
                $newDeviceAlias = "$($storedata.StoreName) $($devicedata.EPOS) Client $($devicedata.ClientNumber) $($devicedata.Hostname)"
                Write-Host "Generated alias is: $newDeviceAlias" -ForegroundColor DarkGray

                return $newDeviceAlias
            }
            '^AUA-|^NZA-' {
                Write-Host 'Computername starts with AUA or NZA' -ForegroundColor DarkGray

                # 1. Construct Team Viewer name
                Write-Host "`nGenerating Team Viewer device alias" -ForegroundColor DarkCyan
                # Format EPOS - need to replace first digit with '7'
                $formattedEPOS = $devicedata.EPOS -replace '^\d', '7'

                $newDeviceAlias = "$($storedata.StoreName) $($formattedEPOS) Client $($devicedata.ClientNumber) $($devicedata.Hostname)"
                Write-Host "Generated alias is: $newDeviceAlias" -ForegroundColor DarkGray

                return $newDeviceAlias
            }
            '^AUC-|^NZC-|^AURR-|^NZRR-|^AUSC-|^NZSC-|^AUAT-|^NZAT-|^AURT-|^NZRT-|^HKC-|^HKSC-' {
                Write-Host 'Computername starts with AUC or NZC' -ForegroundColor Green

                # 1. Construct Team Viewer name
                Write-Host "`nGenerating Team Viewer device alias" -ForegroundColor DarkCyan
                $newDeviceAlias = $computername
                Write-Host "Generated alias is: $newDeviceAlias" -ForegroundColor DarkGray

                return $newDeviceAlias
            }
            Default {
                Write-Host 'Device prefix is not in the list of known prefixes' -ForegroundColor DarkYellow
                return $false
            }
        }
    }
}