HP Scripting Tools for Windows PowerShell manual Return objects and error handling

Page 19

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

Image 19
Contents HP Scripting Tools for Windows PowerShell User Guide Acknowledgments Contents Windows PowerShell Introduction to HP Scripting Tools for Windows PowerShellFeatures Supported operating systems InstallationInstalling HP iLO Cmdlets for Windows PowerShell System prerequisitesClick Start and select Run Uninstalling HP iLO Cmdlets for Windows PowerShellCmdlet Help HP Scripting Tools for Windows PowerShell cmdletsGets the current Enforce AES/3DES Encryption status Obtain more precise and complete asset data Post IPv6 supportUsing the Find-HPiLOcmdlet PowerShell script Piping output from one command to anotherGuid Using iLO cmdlets on multiple targets Using the Update-HPiLOFirmware cmdletHostname Input2.csv Server 192.168.1.1 Log processing examples PowerShell script Return objects and error handling Script writing methodology General issues TroubleshootingContacting HP Support and other resourcesSupport Information Windows PowerShell resources Documentation feedback Index

Scripting Tools for Windows PowerShell specifications

HP Scripting Tools for Windows PowerShell is a powerful suite designed to enhance the management and automation of HP devices and services within a Windows environment. With a robust approach to system administration, it allows IT professionals and system administrators to streamline their workflows, manage resources more effectively, and reduce manual interventions.

One of the main features of HP Scripting Tools is the integration of HP's extensive hardware and software management capabilities into PowerShell. By leveraging PowerShell's scripting abilities, users can automate the configuration, monitoring, and reporting of HP devices at scale. This is particularly beneficial in enterprise settings, where managing a large fleet of devices can become cumbersome and prone to human error.

Among the technologies utilized by HP Scripting Tools is the HP Management Interface, which provides a comprehensive set of cmdlets tailored for handling various HP hardware components, such as servers, printers, and storage solutions. These cmdlets facilitate operations such as retrieving device status, updating firmware, and managing device configurations. This integration simplifies the process of interacting with HP hardware compared to traditional methods, enabling users to perform complex tasks with simple scripts.

Another critical characteristic of HP Scripting Tools is its emphasis on ease of use and accessibility. The tools provide detailed documentation and examples to guide users in writing scripts that address specific needs. Whether automating routine maintenance tasks or deploying configurations across multiple devices, users can rely on the resources provided to optimize their scripting efforts.

Furthermore, HP Scripting Tools supports the management of HP’s software solutions, including HP Smart Admin and HP System Software Manager. This allows for the central management of software updates and configurations, ensuring that systems remain up-to-date and secure with minimal effort.

Additionally, HP Scripting Tools promote collaboration by enabling scripts to be shared across teams, facilitating consistent administration practices. This collaborative environment ensures that best practices are followed and reduces the redundancy of script-writing efforts.

In conclusion, HP Scripting Tools for Windows PowerShell is an invaluable asset for organizations utilizing HP devices, offering essential features to automate management tasks, enhance operational efficiency, and optimize resource utilization. With its powerful cmdlets and seamless integration into PowerShell, it empowers IT professionals to take full control of their HP equipment in a more productive and efficient manner.