Stop-Execution modified

This commit is contained in:
Dmitry Teselkin 2013-02-27 15:49:32 +04:00
parent 49cffcdde4
commit 03d67d4084
2 changed files with 103 additions and 75 deletions

View File

@ -24,6 +24,5 @@ $script:__ImportModulesExplicitely = $true
$script:__ImportModulesErrorAction = "Stop" $script:__ImportModulesErrorAction = "Stop"
$global:__StopExecutionThrowsExeption__ = $true $global:__StopExecutionPreference__ = "Exit"
$global:__StopExecutionExitsSession__ = $false

View File

@ -8,61 +8,68 @@ Function break script execution with error code provided. Error code may be 0 in
It also tries to parse ErrorRecord or Exception object (if provided) and logs this information. It also tries to parse ErrorRecord or Exception object (if provided) and logs this information.
#> #>
[CmdletBinding(DefaultParameterSetName="Exception")]
param ( param (
[Parameter(Position=1,ParameterSetName="Exception")]
$InputObject = $null, $InputObject = $null,
[Parameter(ParameterSetName="ErrorString")]
[String] $ExitString = "", [String] $ExitString = "",
[Int] $ExitCode = 1,
[Parameter(ParameterSetName="ErrorString")]
[Int] $ExitCode = 0,
[Parameter(ParameterSetName="ErrorString")]
[Switch] $Success [Switch] $Success
) )
Function Do-ExitFailure { Function Exit-Function {
Write-LogFatal "STOP ($ExitCode): $ExitString" if ($ExitCode -eq 0) {
if ($__StopExecutionThrowsExeption__) { Write-LogInfo ( "STOP ({0}):`n{1}" -f $ExitCode, $ExitString )
throw $InputObject
}
elseif ($__StopExecutionExitsSession__) {
exit $ExitCode
} }
else { else {
break Write-LogFatal ( "STOP ({0}):`n{1}" -f $ExitCode, $ExitString )
} }
}
Write-Log "__StopExecutionPreference__ = '$__StopExecutionPreference__'"
Function Do-ExitSuccess { switch ("$__StopExecutionPreference__") {
Write-LogInfo "STOP (0): $ExitString" "Exit" {
if ($__StopExecutionThrowsExeption__) { exit $ExitCode
exit 0 }
"ThrowIfException" {
if ($InputObject -eq $null) {
exit $ExitCode
}
else {
throw $InputObject
}
}
"ThrowAlways" {
throw $InputObject
}
default {
throw "Unknown value for __StopExecutionPreference__: '$__StopExecutionPreference__'"
}
} }
elseif ($__StopExecutionExitsSession__) { }
exit 0
}
else {
break
}
}
if ($Success -eq $true) { switch($PSCmdlet.ParameterSetName) {
if ($ExitString -eq "") { "Exception" {
$ExitString = "Script stopped with NO ERROR." #----------
} if ($InputObject -eq $null) {
Do-ExitSuccess $ExitString = "***** SCRIPT INTERRUPTED *****"
} $ExitCode = 255
Exit-Function
if ($ExitString -ne "") { }
Do-ExitFailure #----------
}
#----------
if ($InputObject -eq $null) { try {
$ExitString = "***** SCRIPT INTERRUPTED *****" $ErrorRecord = [System.Management.Automation.ErrorRecord] $InputObject
Do-ExitFailure <#
} $ExitString = @"
if ($ExitString -eq "") {
try {
$ErrorRecord = [System.Management.Automation.ErrorRecord] $InputObject
$ExitString = @"
$($ErrorRecord.ToString()) $($ErrorRecord.ToString())
*** Invocation Info *** *** Invocation Info ***
@ -78,39 +85,61 @@ $($ErrorRecord.FullyQualifiedErrorId.ToString())
$($ErrorRecord.ScriptStackTrace.ToString()) $($ErrorRecord.ScriptStackTrace.ToString())
*** *** *** *** *** ***
"@ "@
} #>
catch { $ExitString = Out-String -InputObject $InputObject
$ErrorRecord = $null $ExitCode = 255
Write-LogWarning "Unable to cast InputObject to [System.Management.Automation.ErrorRecord]" Exit-Function
} }
} catch {
$ErrorRecord = $null
Write-LogWarning "Unable to cast InputObject to [System.Management.Automation.ErrorRecord]"
}
#----------
#----------
try {
$Exception = [System.Exception] $InputObject
#$ExitString = $Exception.ToString()
$ExitString = Out-String -InputObject $InputObject
$ExitCode = 255
Exit-Function
}
catch {
$Exception = $null
Write-LogWarning "Unable to cast InputObject to [System.Exception]"
}
#----------
if ($ExitString -eq "") { #----------
try { try {
$Exception = [System.Exception] $InputObject $ExitString = Out-String -InputObject $InputObject
$ExitString = $Exception.ToString() $ExitCode = 255
} Exit-Function
catch { }
$Exception = $null catch {
Write-LogWarning "Unable to cast InputObject to [System.Exception]" Write-LogWarning "Unable to cast InputObject of type [$($InputObject.GetType())] to any of supported types."
} }
} #----------
}
"ErrorString" {
if ($ExitString -eq "") { if ($Success) {
try { $ExitString = "Script stopped with NO ERROR."
$ExitString = [String] $InputObject $ExitCode = 0
} }
catch {
Write-LogWarning "Unable to cast InputObject of type [$($InputObject.GetType())] to any of supported types." Exit-Function
} }
} }
Do-ExitFailure $ExitString = "Unknown error occured in Stop-Execution"
$ExitCode = 255
Exit-Function
} }
Function Set-ComputerName { Function Set-ComputerName {
param ( param (
[String] $Name [String] $Name