azure-sdk-for-cpp/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1
Azure SDK Bot 744450472d
Sync eng/common directory with azure-sdk-tools for PR 6919 (#4988)
* Add autorest-preview pipeline

* Add emitternpminstall

* Always install

* Use shorter leg name

* Add short circuiting to emitter install

* Use language matrix function

* Don't look for subfolders in the matrix package folders

* Revert unnecessary eng/common changes

* Rewrite GetPullRequestUrl to Get-BuildSourceDescription

* Remove alias from Invoke-LoggedCommand

* Use invoke-expression instead of shelling out

* Add better job splitting

* Replace Folder with Directory

---------

Co-authored-by: Patrick Hallisey <pahallis@microsoft.com>
2023-09-25 12:35:11 -07:00

43 lines
945 B
PowerShell

function Invoke-LoggedCommand($Command, $ExecutePath, [switch]$GroupOutput)
{
$pipelineBuild = !!$env:TF_BUILD
$startTime = Get-Date
if($pipelineBuild -and $GroupOutput) {
Write-Host "##[group]$Command"
} else {
Write-Host "> $Command"
}
if($ExecutePath) {
Push-Location $ExecutePath
}
try {
Invoke-Expression $Command
$duration = (Get-Date) - $startTime
if($pipelineBuild -and $GroupOutput) {
Write-Host "##[endgroup]"
}
if($LastExitCode -ne 0)
{
if($pipelineBuild) {
Write-Error "##[error]Command failed to execute ($duration): $Command`n"
} else {
Write-Error "Command failed to execute ($duration): $Command`n"
}
}
else {
Write-Host "Command succeeded ($duration)`n"
}
}
finally {
if($ExecutePath) {
Pop-Location
}
}
}