diff --git a/build_scripts/windows/scripts/azps.ps1 b/build_scripts/windows/scripts/azps.ps1 index 7e644639bf6..651c1822d4a 100644 --- a/build_scripts/windows/scripts/azps.ps1 +++ b/build_scripts/windows/scripts/azps.ps1 @@ -1,5 +1,11 @@ $env:AZ_INSTALLER="MSI" -& "$PSScriptRoot\..\python.exe" -IBm azure.cli $args +if ($MyInvocation.ExpectingInput) { + $input | & "$PSScriptRoot\..\python.exe" -IBm azure.cli $args +} +else { + & "$PSScriptRoot\..\python.exe" -IBm azure.cli $args +} +exit $LASTEXITCODE # SIG # Begin signature block # MIInuQYJKoZIhvcNAQcCoIInqjCCJ6YCAQExDzANBglghkgBZQMEAgEFADB5Bgor diff --git a/build_scripts/windows/scripts/test_msi_installation.ps1 b/build_scripts/windows/scripts/test_msi_installation.ps1 index c65bb99676c..a277a31a6ea 100644 --- a/build_scripts/windows/scripts/test_msi_installation.ps1 +++ b/build_scripts/windows/scripts/test_msi_installation.ps1 @@ -74,3 +74,44 @@ if ($installed_version -ne $artifact_version){ # Test bundled pip with extension installation & $az_full_path extension add -n account & $az_full_path self-test + +# --------------------------------------------------------------------- +# azps.ps1 wrapper regression tests +# --------------------------------------------------------------------- +$azps_full_path = Join-Path (Split-Path $az_full_path -Parent) "azps.ps1" +if (-not (Test-Path $azps_full_path)) { + Write-Output "azps.ps1 was not installed at $azps_full_path" + Exit 1 +} + +# Baseline: the wrapper runs and --version returns success. +& $azps_full_path --version +if ($LASTEXITCODE -ne 0) { + Write-Output "azps.ps1 --version returned $LASTEXITCODE (expected 0)" + Exit 1 +} + +# A .ps1 wrapper must propagate non-zero exit codes from the +# child process. Unknown commands exit with code 2; an unfixed wrapper +# would exit 0 here. +& $azps_full_path this-command-does-not-exist-xyz *> $null +if ($LASTEXITCODE -eq 0) { + Write-Output "azps.ps1 did not propagate a failure exit code" + Exit 1 +} + +# When pwsh.exe invokes a .ps1 via +# -Command, the script's exit code is only surfaced if the script +# itself calls 'exit N'. Skip this check if pwsh.exe is unavailable +# so we don't false-pass on a stale $LASTEXITCODE value. +if (Get-Command pwsh.exe -ErrorAction SilentlyContinue) { + $global:LASTEXITCODE = 0 + & pwsh.exe -NoProfile -Command "& '$azps_full_path' this-command-does-not-exist-xyz *> `$null; exit `$LASTEXITCODE" + if (-not $? -or $LASTEXITCODE -eq 0) { + Write-Output "azps.ps1 exit code swallowed when invoked via 'pwsh.exe -Command'" + Exit 1 + } +} +else { + Write-Output "pwsh.exe not found; skipping pwsh.exe -Command regression check" +} diff --git a/src/azure-cli/azps.ps1 b/src/azure-cli/azps.ps1 index 9b6de0d8d0a..3dc41dcb92c 100644 --- a/src/azure-cli/azps.ps1 +++ b/src/azure-cli/azps.ps1 @@ -1,14 +1,26 @@ $env:AZ_INSTALLER="PIP" if (Test-Path "$PSScriptRoot\python.exe") { - # Perfer python.exe in venv - & "$PSScriptRoot\python.exe" -m azure.cli $args + # Prefer python.exe in venv + if ($MyInvocation.ExpectingInput) { + $input | & "$PSScriptRoot\python.exe" -m azure.cli $args + } + else { + & "$PSScriptRoot\python.exe" -m azure.cli $args + } } else { # Run system python.exe - python.exe -m azure.cli $args + if ($MyInvocation.ExpectingInput) { + $input | python.exe -m azure.cli $args + } + else { + python.exe -m azure.cli $args + } } +exit $LASTEXITCODE + # SIG # Begin signature block # MIInqgYJKoZIhvcNAQcCoIInmzCCJ5cCAQExDzANBglghkgBZQMEAgEFADB5Bgor # BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG