Showing posts with label REST API. Show all posts
Showing posts with label REST API. Show all posts

Thursday, December 19, 2019

Working with iDRAC9 Redfish API using PowerShell - Part 4


In this article, I will explain how to use iDRAC Redfish API to Power On and Graceful Shutdown a server using PowerShell. This is applicable to all Dell EMC servers having iDRAC. It can be a general-purpose PowerEdge rack server, Ready Node, Appliance, etc. I've tested on iDRAC9.

Note: In a production environment please make sure to follow proper shutdown or reboot procedure (if any) before performing any system reset actions on the server.

[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [String]$idrac_ip,

    [Parameter(Mandatory)]
    [ValidateSet('On''GracefulShutdown')]
    [String]$ResetType
)

#To fix the connection issues to iDRAC REST API
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
        }
    }
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11

#Get iDRAC creds
$Credentials = Get-Credential -Message "Enter iDRAC Creds"

$JsonBody = @{"ResetType" = $ResetType} | ConvertTo-Json
$u1 = "https://$($idrac_ip)/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset"

Invoke-RestMethod -Uri $u1 -Credential $Credentials -Method Post -UseBasicParsing -ContentType 'application/json' -Body $JsonBody -Headers @{"Accept"="application/json"} -Verbose


Hope it was useful. Cheers!

Related posts



References


iDRAC9 Redfish API guide

Tuesday, December 3, 2019

Working with iDRAC9 Redfish API using PowerShell - Part 3

In this article, I will explain how to access the iDRAC Redfish API using session-based authentication.


[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [String]$idrac_ip
)

#To fix the connection issues to iDRAC REST API
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
        }
    }
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11

#Get iDRAC creds
$Credentials = Get-Credential -Message "Enter iDRAC Creds"

$creds_json = '{"UserName": "$($Credentials.UserName)", "Password": "$($Credentials.GetNetworkCredential().Password)"}'
$creds_json = $ExecutionContext.InvokeCommand.ExpandString($creds_json)

#Using Invoke-WebRequest
try {
    $result1 = Invoke-WebRequest -Uri "https://$($idrac_ip)/redfish/v1/Sessions " -Method POST -ContentType 'application/json' -Headers @{"Accept"="application/json"} -Body $creds_json -Verbose
}
catch {
    Write-Error -Message "Failed to invoke the API! Incorrect creds!"
    $PSCmdlet.ThrowTerminatingError($PSItem)
}

$auth_head = @{
"X-Auth-Token" = $result1.Headers.'X-Auth-Token'
"accept" = "application/json" }

#URI to get basic system info
$u1 = "https://$($idrac_ip)/redfish/v1/Systems/System.Embedded.1"

#Using Invoke-RestMethod
try {
    $output = Invoke-RestMethod -Uri $u1 -Method Get -Headers $auth_head -ContentType 'application/json' -Verbose
}
catch {
    Write-Error -Message "Failed to invoke the API! Incorrect creds!"
    $PSCmdlet.ThrowTerminatingError($PSItem)
}

Write-Output "`nBasic System Info:" $output



Hope it was useful. Cheers!

Related posts

Working with iDRAC9 Redfish API using PowerShell - Part 1

Working with iDRAC9 Redfish API using PowerShell - Part 2


References

iDRAC9 Redfish API guide

Saturday, September 15, 2018

Working with iDRAC9 Redfish API using PowerShell - Part 2

In this article I will explain briefly about the JSON response from iDRAC and how you can navigate through the Redfish API tree structure to get all the required information. Now, lets have a look at the URIs. 

Query the computer system collection:
$result1 = Invoke-RestMethod -Uri "https://$($idrac_ip)/redfish/v1/Systems" -Credential $Credentials -Method Get -UseBasicParsing -ContentType 'application/json'

Response: 

You can see one member with URI /redfish/v1/Systems/System.Embedded.1

Below is a sample screen shot of JSON output when you try to query the above listed member system. 


You can get some of the basic information straight away from the above JSON response. And these are organized in hierarchy where you can drill down to each object and get the required details. Below diagram shows basic iDRAC Redfish API tree structure.


Example: You can get details/ health status of  storage controller as shown below.

Query:
$result2 = Invoke-RestMethod -Uri "https://$($idrac_ip)/redfish/v1/Systems/System.Embedded.1/Storage /Controllers/NonRAID.Integrated.1-1" -Credential $Credentials -Method Get -UseBasicParsing -ContentType 'application/json'

Sunday, August 26, 2018

Working with iDRAC9 Redfish API using PowerShell - Part 1

Redfish is a industry standard protocol and specification defined by Distributed Management Task Force (DMTF) for performing systems/ IT infrastructure management actions using RESTful methodology. It is a next generation systems management interface standard which is simple, secure, scalable. Redfish uses JSON data format and transports payload over HTTPS. Initial releases of Redfish focused primarily on systems management and was targeted to be a replacement for IPMI over LAN protocol. Now the capabilities have been extended over the past few years providing a rich set of features and support for network, memory, storage devices etc. Over time the scope of Redfish is being expanded to fit more use cases as the forum is working with several partner organizations. Promoters of this standard include several companies like Broadcom, Cisco, Dell, HP, VMware, Intel, Microsoft etc.

Now, lets have a look at how to connect to iDRAC Redfish API using PowerShell. Redfish provides two authentication methods. Basic authentication and Session-based authentication. Here I will explain basic authentication using username and password for each Redfish API request to iDRAC.

#To fix the connection issues to iDRAC REST API
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
        }
    }
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11

#Get iDRAC creds
$Credentials = Get-Credential -Message "Enter iDRAC Creds"

#URI to get basic system info
$u1 = "https://192.168.10.11/redfish/v1/Systems/System.Embedded.1"

#Using Invoke-RestMethod
$result1 = Invoke-RestMethod -Uri $u1 -Credential $Credentials -Method Get -UseBasicParsing -ContentType 'application/json' -Headers @{"Accept"="application/json"}

Output:


Hope it was useful. Cheers!

References:
iDRAC9 Redfish API reference guide
github.com/dell/iDRAC-Redfish-Scripting

Wednesday, May 30, 2018

Creating HTML report of ScaleIO cluster using PowerShell

This post is a reference to a small reporting script for ScaleIO environments. The project will generate a brief HTML report of your ScaleIO Ready Node SDS infrastructure (with AMS - Automated Management Services) by making use of ScaleIO Ready Node AMS REST APIs and PowerShell. The report provides information about MDM cluster state, overall cluster capacity, system objects, alerts, and health state of all disks in the cluster. Here the API is available as part of ScaleIO Ready Node AMS. These AMS REST API allows you to query information and perform actions related to ScaleIO software and ScaleIO Ready Node hardware components. To access the API you need to provide AMS username and password. Responses returned by AMS server are formatted in JSON format.

Project referencehttps://github.com/vineethac/sio_report

Use case
: This script can be used/ leveraged as part of daily cluster health/ stats reporting process, or something similar; so that monitoring Engineers or whoever responsible can have a look at it on a daily basis to make sure everything is healthy and working normal. 

Related references:

Hope this was helpful. Cheers!

Tuesday, March 20, 2018

Working with ScaleIO REST API using PowerShell - Part 3

As you all know disk drives are the most important and basic building blocks of a storage system. In order to ensure data availability, the disk drives in your storage system should always stay healthy. Unhealthy drives not only cause risk to your data but also contribute to degraded performance. In this article, I will show how to query and get disk health-related information in a ScaleIO cluster using REST API and PowerShell. 

Please refer my previous posts for API authentication, token generation and 'Invoke-RestMethod'.


#Query all devices in the ScaleIO cluster
$all_devices = (Invoke-RestMethod -Uri "https://192.168.11.15:443/api/types/Device/instances " -Method Get -Headers $ScaleIOAuthHeaders)  

#Select necessary properties and sort
$all_devices = (Invoke-RestMethod -Uri "https://192.168.11.15:443/api/types/Device/instances " -Method Get -Headers $ScaleIOAuthHeaders) | select sdsId, storagePoolId, name, deviceCurrentPathName, errorState, deviceState, ssdEndOfLifeState, temperatureState, aggregatedState  | sort sdsId | ft -AutoSize


#Sample output

Hope this was helpful. Please refer to ScaleIO 2.5 API reference guide for more details. Cheers!

Wednesday, March 7, 2018

Working with ScaleIO REST API using PowerShell - Part 2

In my previous post, I explained how to connect and authenticate with ScaleIO REST API, and use Invoke-RestMethod. In this article, I will explain how to query selected statistics/ properties with POST method. 

First, you have to define the set of properties that you would like to query, and then pass it to "invoke-restmethod". You can get the list of available properties of the respective object from the ScaleIO REST API Reference Guide. 

Example 1: Query and collect MDM cluster information
Here I am collecting the below-mentioned properties of "MDMCluster" object. 

#selected properties to query

$param1 = @'
{
    "properties":["name", "clusterMode", "master", "clusterState", "virtualIps"]
}
'@

#invoke-restmethod with respective uri

$MDM_cluster_stats = (Invoke-RestMethod -Uri "https://192.168.11.15:443/api/instances/System/queryMdmCluster " -Body $param1 -ContentType "application/json" -Headers $ScaleIOAuthHeaders -Method Post)

#organize output in a hash table

$mdm_props = @{

    'Cluster name'      = ($MDM_cluster_stats).name
    'Mode'              = ($MDM_cluster_stats).clusterMode
    'Cluster state'     = ($MDM_cluster_stats).clusterState
    'Master MDM IP'     = ($MDM_cluster_stats.master).managementIPs[0]
    'Cluster VIP01'     = ($MDM_cluster_stats).virtualIps[0]
    'Cluster VIP02'     = ($MDM_cluster_stats).virtualIps[1]
}

Write-Output $mdm_props

#sample output screenshot given below


Example 2: Query and collect overall ScaleIO system capacity details
Here I am collecting some properties of "System Statistics" object. 

#selected properties to query

$param2 = @'
{
    "properties":["maxCapacityInKb", "capacityInUseInKb", "spareCapacityInKb", "failedCapacityInKb", "degradedFailedCapacityInKb"]
}
'@

#invoke-restmethod with respective uri

$system_overall_stats = (Invoke-RestMethod -uri "https://192.168.11.15:443/api/types/System/instances/action/querySelectedStatistics " -Body $param2 -ContentType "application/json" -Headers $ScaleIOAuthHeaders -Method Post)

#organize output in a hash table


$system_capacity_props = @{

    "System max capacity (TB)"                = (($system_overall_stats.maxCapacityInKb)/1024/1024/1024)
    "System capacity in use (TB)"             = (($system_overall_stats.capacityInUseInKb)/1024/1024/1024)
    "System spare capacity (TB)"              = (($system_overall_stats.spareCapacityInKb)/1024/1024/1024)
    "System failed capacity (TB)"             = (($system_overall_stats.failedCapacityInKb)/1024/1024/1024)
    "System degraded failed capacity (TB)"    = (($system_overall_stats.degradedFailedCapacityInKb)/1024/1024/1024)
}

Write-Output $system_capacity_props

#sample output screenshot given below


Hope this was useful. Please refer Dell EMC ScaleIO Ready Node Version 2.5 API Reference Guide for more details.