Windows-powershell / PowerShell-master /.github /instructions /start-native-execution.instructions.md
metadata
applyTo:
- '**/*.ps1'
- '**/*.psm1'
Using Start-NativeExecution for Native Command Execution
Purpose
Start-NativeExecution is the standard function for executing native commands (external executables) in PowerShell scripts within this repository. It provides consistent error handling and better diagnostics when native commands fail.
When to Use
Use Start-NativeExecution whenever you need to:
- Execute external commands (e.g.,
git,dotnet,pkgbuild,productbuild,fpm,rpmbuild) - Ensure proper exit code checking
- Get better error messages with caller information
- Handle verbose output on error
Basic Usage
Start-NativeExecution {
git clone https://github.com/PowerShell/PowerShell.git
}
With Parameters
Use backticks for line continuation within the script block:
Start-NativeExecution {
pkgbuild --root $pkgRoot `
--identifier $pkgIdentifier `
--version $Version `
--scripts $scriptsDir `
$outputPath
}
Common Parameters
-VerboseOutputOnError
Captures command output and displays it only if the command fails:
Start-NativeExecution -VerboseOutputOnError {
dotnet build --configuration Release
}
-IgnoreExitcode
Allows the command to fail without throwing an exception:
Start-NativeExecution -IgnoreExitcode {
git diff --exit-code # Returns 1 if differences exist
}
Availability
The function is defined in tools/buildCommon/startNativeExecution.ps1 and is available in:
build.psm1(dot-sourced automatically)tools/packaging/packaging.psm1(dot-sourced automatically)- Test modules that include
HelpersCommon.psm1
To use in other scripts, dot-source the function:
. "$PSScriptRoot/../buildCommon/startNativeExecution.ps1"
Error Handling
When a native command fails (non-zero exit code), Start-NativeExecution:
- Captures the exit code
- Identifies the calling location (file and line number)
- Throws a descriptive error with full context
Example error message:
Execution of {git clone ...} by /path/to/script.ps1: line 42 failed with exit code 1
Examples from the Codebase
Git Operations
Start-NativeExecution {
git fetch --tags --quiet upstream
}
Build Operations
Start-NativeExecution -VerboseOutputOnError {
dotnet publish --configuration Release
}
Packaging Operations
Start-NativeExecution -VerboseOutputOnError {
pkgbuild --root $pkgRoot --identifier $pkgId --version $version $outputPath
}
Permission Changes
Start-NativeExecution {
find $staging -type d | xargs chmod 755
find $staging -type f | xargs chmod 644
}
Anti-Patterns
Don't do this:
& somecommand $args
if ($LASTEXITCODE -ne 0) {
throw "Command failed"
}
Do this instead:
Start-NativeExecution {
somecommand $args
}
Best Practices
- Always use Start-NativeExecution for native commands to ensure consistent error handling
- Use -VerboseOutputOnError for commands with useful diagnostic output
- Use backticks for readability when commands have multiple arguments
- Don't capture output unnecessarily - let the function handle it
- Use -IgnoreExitcode sparingly - only when non-zero exit codes are expected and acceptable
Related Documentation
- Source:
tools/buildCommon/startNativeExecution.ps1 - Blog post: https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right/