Public/Get-PdqVariable.ps1

<#
.SYNOPSIS
Retrieves the value of a Variable from the database of Deploy or Inventory.
 
.INPUTS
None.
 
.OUTPUTS
System.String
 
.EXAMPLE
Get-PdqVariable -Product 'Deploy' -Name '$(Repository)'
Retrieves the System Variable named Repository.
 
.EXAMPLE
Get-PdqVariable -Product 'Inventory' -Name '@(DaveSSN)'
Retrieves the Custom Variable named DaveSSN.
 
.EXAMPLE
Get-PdqVariable -Product 'Inventory' -Name 'DaveSSN'
Retrieves the Variable named DaveSSN by searching both Custom and System Variables.
#>

function Get-PdqVariable {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        # The name of the Variable whose value you would like to retrieve.
        # Make sure to use single quotes with System Variables, otherwise PowerShell will throw an error!
        # '$(Date)'
        [String]$Name,

        [ValidateSet('Deploy', 'Inventory')]
        # The PDQ application you would like to execute this function against.
        [String]$Product,

        # The path to the currently active database will be retrieved by default.
        # You can use this parameter if you wish to run this function against a different database.
        [String]$DatabasePath
    )

    $VariableType = Test-PdqVariableName -Name $Name

    if ( $VariableType -eq 'Bare' ) {

        $CustomName = Convert-PdqVariableType -Name $Name -Type 'Custom'
        $CustomValue = Get-PdqVariable -Name $CustomName -Product $Product -DatabasePath $DatabasePath

        $SystemName = Convert-PdqVariableType -Name $Name -Type 'System'
        $SystemValue = Get-PdqVariable -Name $SystemName -Product $Product -DatabasePath $DatabasePath

        # Make sure only 1 value was returned.
        if ( $CustomValue -and $SystemValue ) {

            throw "'$Name' exists as both a Custom and System variable. Please use the Variable naming convention to specify which one you want."

        } else {

            $CustomValue
            $SystemValue

        }

        break

    } else {

        # Strip the formatting characters out of the name.
        # The database doesn't store the names with the formatting characters.
        $Name = Convert-PdqVariableType -Name $Name -Type 'Bare'

    }
    
    $Table = $VariableType + 'Variables'
    $Query = "SELECT Value FROM $Table WHERE Name = '$Name' COLLATE NOCASE;"
    Invoke-PdqSqlQuery -Product $Product -DatabasePath $DatabasePath -Query $Query -QueryType 'Scalar'

}