<<< Back to Search

Objects ::: Custom Object ::: Result Object

This snippet create a custom object using "Passthru" parameter which allow create multiple properties within the object on the same command.

Sample can be used as a standard to create result objects useful to connect/communicate different functions/methods in a flow, where a result object can return all the information processed.

Core Snippet

# Objects ::: Custom Object ::: Result Object -_-
# www.PowerShellExamples.com

#Create Result Object -_-
$resultObj = New-Object -TypeName PSObject
$resultObj | Add-Member -MemberType NoteProperty -Name TargetElement -Value "" -PassThru |
Add-Member -MemberType NoteProperty -Name ExitCode -Value "" -PassThru |
Add-Member -MemberType NoteProperty -Name ExitMessage -Value ""

#Assign Result Object
$resultObj.TargetElement = "Target"

$resultObj.ExitCode = 2
$resultObj.ExitMessage = "Error. Usually stop execution"

$resultObj.ExitCode = 1
$resultObj.ExitMessage = "Warning. Execution Can continue but user should be informed"

$resultObj.ExitCode = 0
$resultObj.ExitMessage = "Operation was Successful"

# Write Object
$resultObj | Out-String | Write-Host -ForegroundColor Green
    

Practical Sample

# Objects ::: Custom Object ::: Result Object
# www.PowerShellExamples.com

#Create Result Object -_-
$resultObj = New-Object -TypeName PSObject
$resultObj | Add-Member -MemberType NoteProperty -Name TargetElement -Value "" -PassThru |
Add-Member -MemberType NoteProperty -Name ExitCode -Value "" -PassThru |
Add-Member -MemberType NoteProperty -Name ExitMessage -Value ""


# Result successfull "Happy Path"
$resultObj.ExitCode = 0     
$resultObj.ExitMessage = "" # For flow purposes is common leave empty when result is Ok

# Evaluate Result and take action
if($resultObj.ExitCode -ne 0){
    Write-Host "This is an Error or a Warning" -ForegroundColor Yellow
}else{
    Write-Host "Result Ok" -ForegroundColor Yellow
}

# Write Object Result
Write-Host "`r`nWrite to host object contents"
$resultObj | Out-String | Write-Host -ForegroundColor Green


# Result: Fatal Error
$resultObj.ExitCode = 2
$resultObj.ExitMessage = "Error. Usually stop execution"

# Evaluate Result and take action
if($resultObj.ExitCode -ne 0){
    Write-Host "This is an Error or a Warning" -ForegroundColor Red
}else{
    Write-Host "Result Ok" -ForegroundColor Yellow
}

# Write Object Result
Write-Host "`r`nWrite to host object contents"
$resultObj | Out-String | Write-Host -ForegroundColor Green


# Result: Warning
$resultObj.ExitCode = 1
$resultObj.ExitMessage = "Warning. Execution Can continue but user should be informed"

# Evaluate Result and take action
if($resultObj.ExitCode -ne 0){
    Write-Host "This is an Error or a Warning" -ForegroundColor Yellow
}else{
    Write-Host "Result Ok" -ForegroundColor Yellow
}

# Write Object Result
Write-Host "`r`nWrite to host object contents"
$resultObj | Out-String | Write-Host -ForegroundColor Green
 


Result

Result Ok

Write to host object contents

TargetElement ExitCode ExitMessage
------------- -------- -----------
                     0            



This is an Error or a Warning

Write to host object contents

TargetElement ExitCode ExitMessage                  
------------- -------- -----------                  
                     2 Error. Usually stop execution



This is an Error or a Warning

Write to host object contents

TargetElement ExitCode ExitMessage                                                
------------- -------- -----------                                                
                     1 Warning. Execution Can continue but user should be informed