Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell HPE ILO4 Rest API automation examples

Using Microsoft PowerShell to consume and automate any Restful API is very easy. In my earlier article I showed how to automate VMware product to automation rest api - Powershell Using vRealize Log Insight Rest API. In this article I am using HPE ILO4 Rest API to write automation scripts and how to use it. 

Powershell Configure ILO5 using RedFish Restful API

HPE has very good documentation guide provided for  iLO RESTful API Data Model Reference (iLO 4) in below urls, It has complete step by step guide worth to have a look once to understand how to consume Rest API.

Hewlett packard Enterprise user administration invoke-webrequest powershell restful api redfish examples automation hpe ilo4 ilo5 restapi.png

The very first step is to login (authentication and authorization) into ILO4. I have gathered all details in PowerShell with variables. Whenever you supply username and password it should be passed in Json format in the body. Api url starts with /rest/v1 and sessions is the API information for login as shown in the example. Once you execute Invoke-WebRequest with required parameters, you get login token key inside content property.

#ILO details and credentials
$hpeilo = '1921.68.34.25'
$username = 'Administrator'
$password = 'P@ssw0rd'

#ILO4 - LogIn
$credBody = @{UserName = $username; Password=$password} | ConvertTo-Json
$loginUri = "https://$hpeilo/rest/v1/Sessions"
$session = Invoke-WebRequest -Uri $loginUri -Method Post -Body $credBody -ContentType 'application/json' -UseBasicParsing

Microsoft Powershell HPE ILO Rest API Invoke-restmethod Invoke-WebRequest method post get body credentialos header json api authorization credentials example curl.png

Once successfully authenticated on ILO, authorization token can be found inside session >> header >> X-Auth-Token. The GUID value is used in further header parameter. Users information restful API url contains /AccountService/Accounts. Method verb is used Get.

The collected information is in Content property which is in Json format, which can be viewed in PowerShell object format with ConverFrom-Json

#ILO4 - Get users list
$authHeader = @{'X-Auth-Token' = $session.Headers.'X-Auth-Token'}
$accountsAPI = "https://$hpeilo/rest/v1/AccountService/Accounts"
$accountList = Invoke-WebRequest -Uri $accountsAPI -Method Get -Headers $authHeader
($accountList.Content | ConvertFrom-Json).Items | Select-Object -Property UserName, Id

#ILO4 - Delete user with user ID
$iloUserId = '15'
$deleteAccountAPI ="https://$hpeilo/rest/v1/AccountService/Accounts/$iloUserId"
$authHeader = @{"X-Auth-Token"} = $sessions.Headers.'X-Auth-Token'}
$deleteAccount = Invoke-WebRequest -Uri $deleteAccountAPI -Method Delete -Headers $authHeader -UseBasicParsing

#ILO4 - Recheck and Get users list
$authHeader = @{'X-Auth-Token' = $session.Headers.'X-Auth-Token'}
$accountsAPI = "https://$hpeilo/rest/v1/AccountService/Accounts"
$accountList = Invoke-WebRequest -Uri $accountsAPI -Method Get -Headers $authHeader
($accountList.Content | ConvertFrom-Json).Items | Select-Object -Property UserName, Id

Next example is about deleting user. Deleting user requires ID number of account. API uri example for deleting user conatins /AccountService/Accounts/15. Token information is same used in Header generated earlier. 

To verify whether user is deleted successfully run the getting Account users list again.

Microsoft HPE ILO4 Rest API PowerShell Invoke-Webrequest content session login Invoke-restMethod authentication header bearer tocken account service body method put get delete convertfrom-json select-object.png

Download this script from or it is also available here.

Useful Articles
Resolved: Esxi Join domain failed - Error in Active Directory Operations
Join domain ESXi to an Active Directory OU : Powercli

Esxi reset root password, reset windows administrator passwordReset forgotten ESXi root password on Domain joined Esxi using vSphere web client and Powercli
Reset ESXi root password using Host Profiles on vCenter server: VMWare vSphere Web client
Resolved: Reset Esxi forgotten root password using hiren bootCD step by step

Go Back


Blog Search

Page Views


Follow me on Blogarama