Public/Get-specTeamViewerAlias.ps1

Function Get-specTeamViewerAlias {
    <#
    .SYNOPSIS
    Use this function with PSEngine child scripts.
    Generates a TeamViewer device alias based on the computer name and Azure table data.
 
    .DESCRIPTION
    Use this function with PSEngine child scripts.
    The Get-specTeamViewerAlias 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 or any other table information.
 
    .PARAMETER StorageAccount
    Specifies the Azure storage account name where the tables reside.
 
    .EXAMPLE
    Get-specTeamViewerAlias -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-specTeamViewerAlias -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 - Refactored based on 'Get-specTVNamingConvention' so it is now flexible - can now easily expand to handle creating a TV alias for different regions \ Computername prefixes
    #>


    [cmdletbinding()]

    Param (
        [parameter (mandatory = $false)]
        [string]$ComputerName = $env:COMPUTERNAME,

        [parameter (mandatory = $true)]
        [string]$TableSASRO,

        [parameter (mandatory = $false)]
        [string]$DeviceIndexTable,

        [parameter (mandatory = $false)]
        [string]$StoreTable,

        [parameter (mandatory = $true)]
        [string]$StorageAccount
    )

    begin {
        # Get local gateway on the device
        try {
            $localGateway = (Get-specLocalDeviceGateway -ea stop).gateway

            $count = 0
            $localGateway | % { $count++ }
            if ($count -gt 1) {
                Write-Error 'Multiple gateways were detected. Script cannot continue processing' -ea stop
            }
        } catch {
            Write-Error "An error occurred trying to get the local gateway: $_" -ea stop
        }

        # Get relevant info for all computernames starting: 'NLRO-'
        if ($computername -match '^NLRO-') {

            # Get the data from the serverconfig table
            Write-Host "`nGetting table data from: " -ForegroundColor DarkCyan -NoNewline
            Write-Host $StoreTable -ForegroundColor DarkGray

            $params = @{
                value          = $localGateway
                Key            = 'Store_Gateway'
                tableName      = $StoreTable
                StorageAccount = $StorageAccount
                SASToken       = $TableSASRO
            }

            try {
                $storeData = Get-SpecAzTableRowUsingSAS @params -ea Stop
                if ([string]::IsNullOrEmpty($storeData)) { Write-Error "No table data returned for gateway: '$($localGateway)' - script cannot continue processing" -ea stop }
            } catch {
                Write-Error "Table '$($StoreTable)' lookup failed: $_" -ea stop
            }
        } else {
            Write-Host 'No computername match - Script cannot continue processing' -ForegroundColor DarkYellow
        }
    }

    process {
        Write-Host

        switch -Regex ($ComputerName) {
            '^NLRO' {
                # This device was not in the deviceindex table, so construct the TV Alias based on the serverconfig table (Using Gateway IP matching)
                Write-Host "`Constructing TeamViewer Alias" -ForegroundColor DarkCyan
                $newDeviceAlias = "$($computername) $($storedata.StoreName) $($storedata.RowKey)"
            }
        }
    }

    end {
        return $newDeviceAlias
    }
}