bepug - powershell and rest - expanding your horizon
TRANSCRIPT
![Page 1: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/1.jpg)
PowerShell and REST Expanding your Horizon
Luc Dekens, PowerShell MVP, vExpert
![Page 2: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/3.jpg)
Agenda
• REST
• Cmdlet
• Simple Run
• Issues
• Tools
• Star Wars
![Page 4: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/9.jpg)
Running the cmdlet 2
![Page 10: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/10.jpg)
Running the cmdlet 3
• Authentication - Basic
![Page 11: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/12.jpg)
Issues – DateTime 2
• Solution 1
![Page 13: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/14.jpg)
Issues – Depth 1
• ConvertTo-Json : default Depth is 2 !
![Page 15: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/15.jpg)
Issues – Status
• Invoke-RestMethod only returns data
• Status/Error code is not included
![Page 16: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/16.jpg)
Tools 1
• The REST API Reference for the application
• Come in all sizes and formats
![Page 17: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/17.jpg)
Tools 2
• A simulator
• Advanced REST Client for Chrome
![Page 18: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/18.jpg)
Tools 3
• A Capturing Tool
• Fiddler4
• PostMan
![Page 19: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/19.jpg)
Tools 4
• Send script REST API traffic through Fiddler4
![Page 20: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/20.jpg)
Tools 5
• Keep track of what you are implementing (think module)
![Page 21: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/21.jpg)
Star Wars 1
![Page 22: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/22.jpg)
Star Wars 2
![Page 23: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/23.jpg)
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 24: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/24.jpg)
References
• Invoke-Restmethod
• RESTful API Tutorial
• RESTful Web services: The basics
• API Directory
• GitHub
![Page 25: BePUG - PowerShell and REST - Expanding your Horizon](https://reader034.vdocuments.net/reader034/viewer/2022050614/5a6dd7f97f8b9a505f8b49a3/html5/thumbnails/25.jpg)
Q&A