Public/Get-specTVDeviceAlias.ps1

Function Get-specTVDeviceAlias {
    <#
    .SYNOPSIS
        Retrieves and creates a new computer name alias for a TeamViewer device based on its machine name.
 
    .DESCRIPTION
        The Get-specTVDeviceAlias function retrieves or creates a new computer name alias for a TeamViewer device by analysing
        its machine name. The function queries an Azure table to gather information necessary for creating the alias.
 
    .PARAMETER MachineName
        Specifies the machine name for which the alias is to be retrieved or created. If not provided, the function uses the
        current machine's name (default: $env:COMPUTERNAME).
 
    .PARAMETER TableName
        Specifies the name of the Azure table where relevant data is stored.
 
    .PARAMETER StorageAccount
        Specifies the name of the Azure Storage Account containing the specified table.
 
    .PARAMETER SASToken
        Specifies the Shared Access Signature (SAS) token for authenticating access to the Azure Storage Account.
 
    .EXAMPLE
        Get-specTVDeviceAlias -MachineName "UKC-123456" -TableName "MyAzureTable" -StorageAccount "MyStorageAccount" -SASToken "MySASToken"
        Retrieves or creates a new alias for the specified TeamViewer device using the provided details.
 
    .EXAMPLE
        Get-specTVDeviceAlias
        Retrieves or creates a new alias for the current machine using default values for Azure table and storage account.
 
    .NOTES
        Author : owen.heaume
        Version : 1.0
 
    #>



    [cmdletbinding()]

    param (
        [parameter (mandatory = $false)]
        [string]$MachineName = $env:COMPUTERNAME,

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

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

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

    # Getting device alias
    Write-specLogMessage "Creating new computername alias" -Colour DarkCyan    

    # in the Azure table, the computer name is stored without the prefix eg instead of UKC-123456 it is stored as 123456 in the table.
    # so we need to extract this as it's used as the lookup in Get-specEposAndStorename
    $computerName = ($MachineName).Split('-')[1]         

    $params = @{
        value = $computerName
        key = 'RowKey'
        Tablename = $tablename
        storageaccount = $StorageAccount
        sastoken = $SASToken
    }
    
    $tableresult = Get-SpecAzTableRowUsingSAS @params

    switch ($tableresult) {
        601 { Write-specLogMessage "An error occurred during the retrieval process" -Colour DarkYellow; return 1}
        602 { Write-specLogMessage "The remote server returned a 404 error (Table Not Found)" -Colour DarkYellow; return 1}
        603 { Write-specLogMessage "The remote server name could not be resolved (Storage Account Not Found)" -Colour DarkYellow; return 1}
        604 { Write-specLogMessage "An unspecified WebException occurred" -Colour DarkYellow; return 1}
        605 { Write-specLogMessage "Missing [value] parameter when requesting filtered data" -Colour DarkYellow; return 1}
        606 { Write-specLogMessage "Missing [key] parameter when requesting filtered data" -Colour DarkYellow; return 1}
    }

    $newAlias = "$MachineName $($tableResult.storename) $($tableResult.epos)"

    Write-specLogMessage "New Alias is: $newAlias" -Colour DarkGray 

    return $newAlias

}