PowerShell and REST Expanding your Horizon
Luc Dekens, PowerShell MVP, vExpert
Bio
• Luc Dekens - Belgium
• PowerShell MVP (2013-2015)
• vExpert (2009-2015)
• Co-author PowerCLI Reference
• LucD on VMware VMTN
• http://lucd.info
• @LucD22
Agenda
• REST
• Cmdlet
• Simple Run
• Issues
• Tools
• Star Wars
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
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
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
Cmdlet Parameters
MethodUseBasicParsing
URIWebSession
SessionVariable
Credential
UseDefaultCredentials
CertificateThumbprint
CertificateUserAgent
DisableKeepAliveTimeoutSec
HeadersMaximumRedirectionProxy
ProxyCredential
ProxyUseDefaultCredentials
Body
ContentType
TransferEncoding
Infile
Outfile
Passthru
Authentication CmdletRequest Session
Running the cmdlet 1
• Minimal run
• Default REST Method is Get
• Format of return data
• Filetype-like extension on URI
• Header - ContentType
Running the cmdlet 2
Running the cmdlet 3
• Authentication - Basic
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)
Issues – DateTime 2
• Solution 1
Issues – DateTime 3
• Solution 2
• Formula: convert to UTC, subtract “01/01/70 00:00”
• … and back
Issues – Depth 1
• ConvertTo-Json : default Depth is 2 !
Issues – Status
• Invoke-RestMethod only returns data
• Status/Error code is not included
Tools 1
• The REST API Reference for the application
• Come in all sizes and formats
Tools 2
• A simulator
• Advanced REST Client for Chrome
Tools 3
• A Capturing Tool
• Fiddler4
• PostMan
Tools 4
• Send script REST API traffic through Fiddler4
Tools 5
• Keep track of what you are implementing (think module)
Star Wars 1
Star Wars 2
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
References
• Invoke-Restmethod
• RESTful API Tutorial
• RESTful Web services: The basics
• API Directory
• GitHub
Q&A