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
                1.0.1 - Now gets all table data for the store table and filters it in the switch statement. (Required for NZR devices)
                        - NZR now has its own logic to generate the alias based off its IP address obtained from the device NIC.
                1.0.2 - For NZR devices, now use more accurate local IP address querying - to look for 2 specific subnets.
    #>


    [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 as they use their hostname as the alias
        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 -and $computername -notmatch '^NZR') {
                # 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'
                        GetAllRows     = $true
                        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-|^AUQA-|^NZQA-' {
                Write-Host 'Computername starts with AUR, NZR, AUQA or NZQA' -ForegroundColor DarkGray

                # Get the required row using filtering as $storedata contains the entire table
                $storedata = $storedata | ? { $_.rowkey -eq $devicedata.epos }

                # 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

                # Get the required row using filtering as $storedata contains the entire table
                $storedata = $storedata | ? { $_.rowkey -eq $devicedata.epos }

                # 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
            }
            '^NZR-' {
                Write-Host 'Computername starts with NZR' -ForegroundColor DarkGray

                # 0. Get the IP address of the device
                $localIPAddress = (Get-NetIPAddress | Where-Object { $_.IPAddress -match '^10\.121\.' -or $_.IPAddress -match '^10\.122\.' }).ipaddress
                if ([string]::IsNullOrEmpty($localIPAddress)) {
                    Write-Error 'Unable to get the IPAddress - script processing cannot continue.' -ea Stop
                }

                # 0a. Split the IP address by '.' and store the octets in a variable
                $octets = $localIPAddress -split '\.'

                # 0b. Extract the first three octets and the last octet
                $firstThreeOctets = "$($octets[0]).$($octets[1]).$($octets[2])"
                $lastOctet = $octets[3]

                # 1. Construct Team Viewer name
                Write-Host "`nGenerating Team Viewer device alias" -ForegroundColor DarkCyan

                # Get the required row using filtering as $storedata contains the entire table
                $storedata = $storedata | ? { $_.ipaddress -match $firstThreeOctets }

                # Sanity check
                $count = 0
                $storedata | % { $count++ }
                if ($count -gt 1) { Write-Error 'Storedata contains more than one entry - processing cannot continue' -ea stop }

                $newDeviceAlias = "$($storedata.StoreName) $($storedata.RowKey) Client $($lastOctet) $($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
            }
        } #SWITCH
    }
}