Testing/Run-BCTests.ps1

function Run-BCTests
{
    Param(
        [Parameter(Mandatory=$false)]
        [string]$ResultPath = 'C:\Temp\Results.xml',
        [Parameter(Mandatory=$false)]
        [string]$ContainerName = '',
        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]$Credential
    )

    if ((Get-IsALRepo) -and ($ContainerName -eq '')) {
        $ContainerName = (Split-Path (Get-Location) -Leaf)
    }

    $TempPath = Create-TempDirectory

    if (Test-Path $ResultPath)
    {
        Remove-Item $ResultPath
    }

    if (($ContainerName -ne '') -and ($Credential -eq $null)) {
        $Password = ConvertTo-SecureString (Get-EnvironmentKeyValue -KeyName 'password') -AsPlainText -Force
        $Credential = New-Object System.Management.Automation.PSCredential((Get-EnvironmentKeyValue -KeyName 'user'), $Password)
    }

    if ($ContainerName -ne '') {
        Write-Host -ForegroundColor Green "Running tests for $ContainerName"
    }
    else {
        Write-Host -ForegroundColor Green "Running tests for $ServerInstance"
    }

    $BuildHelperApp = Get-AppFromLastSuccessfulBuild -ProjectName 'TFS Tools' -RepositoryName BuildHelper
    Publish-NavContainerApp -containerName $ContainerName -appFile $BuildHelperApp.FullName -install -sync

    if ((Get-NavContainerCountry $ContainerName) -eq 'dk') { 
        $CompanyName = (Get-CompanyInNavContainer $ContainerName).Item(1).CompanyName
    }
    else {
        if ((Get-CompanyInNavContainer -containerName $ContainerName).Count -eq $null) {
            $CompanyName = (Get-CompanyInNavContainer $ContainerName).CompanyName
        }
        else {
            $CompanyName = (Get-CompanyInNavContainer $ContainerName).Item(0).CompanyName
        }
    }

    $AutomatedTestMgt = New-WebServiceProxy -Uri ("http://{0}:7047/NAV/WS/{1}/Codeunit/AutomatedTestMgt" -f (Get-NavContainerIpAddress -containerName $ContainerName), $CompanyName) -Credential $Credential
    $AutomatedTestMgt.GetTests()

    Write-Host 'Executing tests in client'

    Run-TestsInNavContainer -containerName $ContainerName -credential $Credential -XUnitResultFileName "C:\ProgramData\NavContainerHelper\Extensions\$ContainerName\my\Results.xml" -detailed

    cpi "C:\ProgramData\NavContainerHelper\Extensions\$ContainerName\my\Results.xml" $ResultPath -Force
}

Export-ModuleMember -Function Run-BCTests