Api/Get-NavContainerApiCompanyId.ps1

<#
 .Synopsis
  Invoke Api in Container
 .Description
  Invoke an Api in a Container.
 .Parameter containerName
  Name of the container in which you want to invoke an api
 .Parameter tenant
  Name of the tenant in which context you want to invoke an api
 .Parameter Credential
  Credentials for the user making invoking the api (do not specify if using Windows auth)
 .Parameter CompanyName
  CompanyName for which you want to get the Company Id (leave empty to get company Id for the default company)
 .Example
  $companyId = Get-NavContainerApiCompanyId -containerName $containerName -credential $credential
 .Example
  $companyId = Get-NavContainerApiCompanyId -containerName $containerName -credential $credential -CompanyName 'CRONUS International Ltd.'
#>

function Get-NavContainerApiCompanyId {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $containerName, 
        [Parameter(Mandatory=$false)]
        [string] $tenant = "default",
        [Parameter(Mandatory=$false)]
        [PSCredential] $credential = $null,
        [Parameter(Mandatory=$false)]
        [string] $APIVersion = "beta",
        [Parameter(Mandatory=$false)]
        [string] $CompanyName = "",
        [switch] $silent
    )

    if (!($CompanyName)) {

        $customConfig = Get-NavContainerServerConfiguration -ContainerName $containerName
    
        if ($credential) {
            $username = $credential.UserName
        } else {
            $username = whoami
        }
        
        $user = Get-NavContainerNavUser -containerName $containerName -tenant $tenant | Where-Object { $_.Username -eq $Username }
        if (!($CompanyName)) {
            if ($user) {
                $CompanyName = $user.Company
            }
        }
        if (!($CompanyName)) {
            $CompanyName = $customConfig.ServicesDefaultCompany
        }
        if (!($CompanyName)) {
            $Company = Invoke-ScriptInNavContainer -containerName $containerName -scriptblock { Param($tenant)
                Get-NavCompany -ServerInstance $serverInstance -tenant default
            } -argumentList $tenant | Where-Object { $_ -isnot [System.String] }
            $CompanyName = $Company | Select-Object -First 1 -ExpandProperty CompanyName
        }
    }

    $companyFilter = [Uri]::EscapeDataString("name eq '$CompanyName'")
    
    $result = Invoke-NavContainerApi -containerName $containerName -tenant $tenant -APIVersion $APIVersion -Query "companies?`$filter=$companyFilter" -credential $credential -silent:$silent
    $result.value | Select-Object -First 1 -ExpandProperty id
}
Set-Alias -Name Get-BCContainerApiCompanyId -Value Get-NavContainerApiCompanyId
Export-ModuleMember -Function Get-NavContainerApiCompanyId -Alias Get-BCContainerApiCompanyId