Using Windows PowerShell as a REST Client

The Windows PowerShell Invoke-RestMethod cmdlet can also be used as a REST client. As in any request, the headers must be passed in the request.

In this topic:

Ignoring self-signed certificates

Examples using Windows PowerShell

Ignoring Self-signed Certificates

When using Windows PowerShell as a client, to avoid SSL Certificate trust issues if using HTTPS, enter this function in the PowerShell window:

function Ignore-SelfSignedCerts
{
    try
    {
        Write-Host "Adding TrustAllCertsPolicy type." -ForegroundColor White
        Add-Type -TypeDefinition  @"
        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;
            }
        }
"@

        Write-Host "TrustAllCertsPolicy type added." -ForegroundColor White
      }
    catch
    {
        Write-Host $_ -ForegroundColor "Yellow"
    }

    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}

Ignore-SelfSignedCerts

Examples Using Windows PowerShell

Example 1

This example gets data for pools in the server group. Before sending the request ServerHost and Authorization headers are added to the variable $headers, which will be used in the request. One object is returned for each pool.

Example Request

PS D:\> $headers = @{}
PS D:\> $headers.Add("ServerHost", "Server1")
PS D:> $headers.Add("Authorization", "Basic Administrator MyPassword!")
PS D:\> Invoke-RestMethod -Method GET -Headers $headers -Uri https://Server1/RestService/rest.svc/1.0/pools

Example Response

Caption            : heavy-pool-1
ExtendedCaption    : heavy-pool-1 on Server1
Id                 : 8D9704E7-1197-43F9-AA85-FAD42FF591B5:{66c61d00-66bb-11e4-80df-002590570c2f}
Internal           : False
SequenceNumber     : 1042
Alias              : heavy-pool-1
AutoTieringEnabled : True
ChunkSize          : @{Value=134217728}
Description        :
InSharedMode       : False
IsAuthorized       : False
MaxTierNumber      : 3
PoolMode           : 1
PoolStatus         : 5
PresenceStatus     : 1
SMPAApproved       : False
ServerId           : 8D9704E7-1197-43F9-AA85-FAD42FF591B5
SharedPoolId       :
TierReservedPct    : 0
Type               : 0

Caption            : disk pool 1
ExtendedCaption    : disk pool 1 on Server1
Id                 : 8D9704E7-1197-43F9-AA85-FAD42FF591B5:{118f8d48-7449-11e4-80e3-002590570c2f}
Internal           : False
SequenceNumber     : 1043
Alias              : disk pool 1
AutoTieringEnabled : True
ChunkSize          : @{Value=134217728}
Description        :
InSharedMode       : False
IsAuthorized       : False
MaxTierNumber      : 3
PoolMode           : 1
PoolStatus         : 5
PresenceStatus     : 1
SMPAApproved       : False
ServerId           : 8D9704E7-1197-43F9-AA85-FAD42FF591B5
SharedPoolId       :
TierReservedPct    : 0
Type               : 0

Example 2

This example gets data for a pool named "Disk pool 1" in the server group. Before sending the request ServerHost and Authorization headers are added to the variable $headers, which will be used in the request. One object is returned.

Example Request

PS D:\> $headers = @{}
PS D:\> $headers.Add("ServerHost", "Server1")
PS D:> $headers.Add("Authorization", "Basic Administrator MyPassword!")
PS D:\> Invoke-RestMethod -Method GET -Headers $headers -Uri https://Server1/RestService/rest.svc/1.0/pools?pool=A3E78CFA-DBEC-44E1-A23C-E9E262EC4551:{d68b8a36-0ec4-11e5-80ba-00155d651622}

Example Response

Caption            : Disk pool 1

ExtendedCaption    : Disk pool 1 on StorageSvr001

Id                 : A3E78CFA-DBEC-44E1-A23C-E9E262EC4551:{d68b8a36-0ec4-11e5-80ba-00155d651622}

Internal           : False

SequenceNumber     : 7571

Alias              : Disk pool 1

AutoTieringEnabled : True

ChunkSize          : @{Value=134217728}

Description        :

InSharedMode       : False

IsAuthorized       : True

MaxTierNumber      : 3

PoolMode           : 1

PoolStatus         : 0

PresenceStatus     : 1

SMPAApproved       : False

ServerId           : A3E78CFA-DBEC-44E1-A23C-E9E262EC4551

SharedPoolId       :

TierReservedPct    : 0

Type               : 0