Skip to content

Instantly share code, notes, and snippets.

@pldmgg
Created February 14, 2018 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pldmgg/9434e09384d8280e0d0fa395cfa89c4c to your computer and use it in GitHub Desktop.
Save pldmgg/9434e09384d8280e0d0fa395cfa89c4c to your computer and use it in GitHub Desktop.
PowerShell Error Handling Output Testing
function Test-Func {
try {
$NewVagrantBoxResult = New-VagrantBox -VagrantBox "centos/7" -ErrorVariable NVBErr -ErrorAction SilentlyContinue
if (!$NewVagrantBoxResult) {throw "The New-VagrantBox function failed!"}
$NewVagrantBoxResult
}
catch {
Write-Error $_
Write-Error $($NVBErr | Out-String)
#Write-Error $($NVBErr.ScriptStackTrace | Out-String)
$global:FunctionResult = "1"
return
}
}
function New-VagrantBox {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$False)]
[string]$VagrantBox
)
if (![bool]$(Get-Command vagrant -ErrorAction SilentlyContinue)) {
try {
$InstallProgramResult = Install-Program -ProgramName "vagrant" -ErrorVariable IPErr -ErrorAction SilentlyContinue
if (!$InstallProgramResult) {throw "The Install-Program function failed!"}
}
catch {
Write-Error $_
Write-Error $($IPErr | Out-String)
#Write-Error $($IPErr.ScriptStackTrace | Out-String)
$global:FunctionResult = "1"
return
}
}
Write-Output "New-VagrantBox Success"
}
function Install-Program {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$False)]
[string]$ProgramName
)
if (![bool]$(Get-Command choco -ErrorAction SilentlyContinue)) {
try {
$InstallChocoResult = Install-ChocolateyCmdLine -ErrorVariable ICCErr -ErrorAction SilentlyContinue
if (!$InstallChocoResult) {throw "The Install-ChocolateyCmdLine function failed!"}
}
catch {
Write-Error $_
Write-Error $($ICCErr | Out-String)
#Write-Error $($ICCErr.ScriptStackTrace | Out-String)
$global:FunctionResult = "1"
return
}
}
Write-Output "Install-Program Success"
}
function Install-ChocolateyCmdLine {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$False)]
[switch]$UseChocoOrgInstallScript
)
try {
1/0
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
Write-Output "Install-ChocolateyCmdLine Success"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment