25
PowerShell and REST Expanding your Horizon Luc Dekens, PowerShell MVP, vExpert

BePUG - PowerShell and REST - Expanding your Horizon

Embed Size (px)

Citation preview

Page 1: BePUG - PowerShell and REST - Expanding your Horizon

PowerShell and REST Expanding your Horizon

Luc Dekens, PowerShell MVP, vExpert

Page 2: BePUG - PowerShell and REST - Expanding your Horizon

Bio

• Luc Dekens - Belgium

• PowerShell MVP (2013-2015)

• vExpert (2009-2015)

• Co-author PowerCLI Reference

• LucD on VMware VMTN

• http://lucd.info

• @LucD22

Page 3: BePUG - PowerShell and REST - Expanding your Horizon

Agenda

• REST

• Cmdlet

• Simple Run

• Issues

• Tools

• Star Wars

Page 4: BePUG - PowerShell and REST - Expanding your Horizon

REST

• Representational State Transfer• (Alternative to SOAP)

• Not a protocol, but an architecture !

• Constraints: Client-Server, Stateless, Cacheable,

Layered, Uniform Interface…

• HTTP/HTTPS

• HTTP Verbs (GET, POST, PUT, DELETE…)

• URI (Uniform Resource Identifiers)

• RESTful APIs

Page 5: BePUG - PowerShell and REST - Expanding your Horizon

REST

Method + Base-Uri + Endpoint

GET https://cloud.ravellosystems.com/api/v1/applications

GET http://whois.arin.net/rest/nets;q=207.46.13.63

Page 6: BePUG - PowerShell and REST - Expanding your Horizon

Cmdlet

Invoke-RestMethod

• Since PowerShell v3

• Improved in subsequent versions, but still some annoying “issues” present

• Tries to detect the returned datatype + converts (ATOM, XML, JSON, Text)

• Parameters = 23 (PS v5 build 10.0.10586.0) + Common parameters

Page 7: BePUG - PowerShell and REST - Expanding your Horizon

Cmdlet Parameters

MethodUseBasicParsing

URIWebSession

SessionVariable

Credential

UseDefaultCredentials

CertificateThumbprint

CertificateUserAgent

DisableKeepAliveTimeoutSec

HeadersMaximumRedirectionProxy

ProxyCredential

ProxyUseDefaultCredentials

Body

ContentType

TransferEncoding

Infile

Outfile

Passthru

Authentication CmdletRequest Session

Page 8: BePUG - PowerShell and REST - Expanding your Horizon

Running the cmdlet 1

• Minimal run

• Default REST Method is Get

• Format of return data

• Filetype-like extension on URI

• Header - ContentType

Page 9: BePUG - PowerShell and REST - Expanding your Horizon

Running the cmdlet 2

Page 10: BePUG - PowerShell and REST - Expanding your Horizon

Running the cmdlet 3

• Authentication - Basic

Page 11: BePUG - PowerShell and REST - Expanding your Horizon

Issues – DateTime 1

• JSON uses Unix epoch (POSIX time)

• Type is long (int64)

• Converting DateTime issue

• DateTime: ScriptProperty (added by ETS)

• DisplayHint: NoteProperty (added by Get-Date)

Page 12: BePUG - PowerShell and REST - Expanding your Horizon

Issues – DateTime 2

• Solution 1

Page 13: BePUG - PowerShell and REST - Expanding your Horizon

Issues – DateTime 3

• Solution 2

• Formula: convert to UTC, subtract “01/01/70 00:00”

• … and back

Page 14: BePUG - PowerShell and REST - Expanding your Horizon

Issues – Depth 1

• ConvertTo-Json : default Depth is 2 !

Page 15: BePUG - PowerShell and REST - Expanding your Horizon

Issues – Status

• Invoke-RestMethod only returns data

• Status/Error code is not included

Page 16: BePUG - PowerShell and REST - Expanding your Horizon

Tools 1

• The REST API Reference for the application

• Come in all sizes and formats

Page 17: BePUG - PowerShell and REST - Expanding your Horizon

Tools 2

• A simulator

• Advanced REST Client for Chrome

Page 18: BePUG - PowerShell and REST - Expanding your Horizon

Tools 3

• A Capturing Tool

• Fiddler4

• PostMan

Page 19: BePUG - PowerShell and REST - Expanding your Horizon

Tools 4

• Send script REST API traffic through Fiddler4

Page 20: BePUG - PowerShell and REST - Expanding your Horizon

Tools 5

• Keep track of what you are implementing (think module)

Page 21: BePUG - PowerShell and REST - Expanding your Horizon

Star Wars 1

Page 22: BePUG - PowerShell and REST - Expanding your Horizon

Star Wars 2

Page 23: BePUG - PowerShell and REST - Expanding your Horizon

Why REST

• It’s (relatively) easy !

• Many vendors/products offer a Restful API

• MSFT: Azure, Exchange, SharePoint…

• VMW: vRealize Suite

• EMC: Avamar

• … and many more

• Common (most of the time) methodology

• To watch (?): Odata + OAuth2

Page 25: BePUG - PowerShell and REST - Expanding your Horizon

Q&A