beautypg.com

Return objects and error handling – HP Scripting Tools for Windows PowerShell User Manual

Page 19

background image

Return objects and error handling

The iLO cmdlets return PowerShell custom objects (PSObject) as the default.

2

Values for the

returned objects can be accessed and used as any other objects in PowerShell. If you have $rt
set to the returned object from iLO, it should contain either $null (no value is returned) or contain
a returned object. For example, to access the HOST_POWER property in the returned object, use
$rt.HOST_POWER

.

As in the preceding examples, when there is an error or warning message returned from an iLO,
it is indicated by a property in the returned object called STATUS_TYPE. Enclosing iLO cmdlets
in try blocks and using catch for errors is a good practice, but it does not handle a returned
iLO error or warning. Three values can be returned in STATUS_TYPE: OK, WARNING, or ERROR.

The following script modifies one of the preceding examples by adding error handling.

PowerShell script:

$path = ".\input1.csv"
$csv = Import-Csv $path
try {
$rt = $csv | Set-HPiLOHostPower
if ($rt -ne $null) {
foreach ($iloreturn in $rt) {
switch ($iloreturn.STATUS_TYPE) {
#OK status is not returned in a Set cmdlet
#but you can get a warning or error
'WARNING' { "I have been warned by " + $iloreturn.IP +
" that: " + $iloreturn.STATUS_MESSAGE}
'ERROR' { "Somthing bad returned by " + $iloreturn.IP +
": " + $iloreturn.STATUS_MESSAGE}
}
}
}
$rt = $csv | Get-HPiLOHostPower
$rt | Format-List
}
catch {
#code for however you want to handle a PowerShell error in the try block
exit
}

Script output:

I have been warned by 192.168.1.1 that: Host power is already ON.
I have been warned by 192.168.1.3 that: Host power is already ON.

IP : 192.168.1.1
HOSTNAME : ilohostbc.company.net
STATUS_TYPE : OK
STATUS_MESSAGE : OK
HOST_POWER : ON

IP : 192.168.1.3
HOSTNAME : isabella-vp2.company.net
STATUS_TYPE : OK
STATUS_MESSAGE : OK
HOST_POWER : ON

Because PowerShell errors print the error and continue, it might be sufficient to leave out the try
– catch handling unless you want to exit, or perform some other handling such as logging the
error.

2. XML and RIBCL output types can also be selected with the –OutputType parameter.

Return objects and error handling

19