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 for the devices serial number 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

    # in the Azure table, the computer name is stored without the prefix eg instead of UKC-123456 it is stored as 123456 (the device Serial Number) in the table.
    # so we need to extract this as it's used as the lookup in Get-specEposAndStorename
    $DeviceSerial = (Get-WmiObject Win32_BIOS).SerialNumber

    $params = @{
        value          = $DeviceSerial
        key            = 'RowKey'
        Tablename      = $tablename
        storageaccount = $StorageAccount
        sastoken       = $SASToken
    }

    $tableresult = Get-SpecAzTableRowUsingSAS @params

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

    # Check to see if the computername was actually present in the table
    if ([string]::IsNullOrEmpty($tableresult.rowkey)) {
        Write-host "'$Computername' was not found in table: '$tablename'"
        return 1
    }

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

}